Gonzalez,
Probably more than you need but here is an example:
package com.wrappedapps.boutique.view.admin;
import java.io.*;
import java.util.*;
import java.text.*;
import java.awt.Color;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.lowagie.text.*;
import com.lowagie.text.pdf.codec.GifImage;
import com.lowagie.text.pdf.PdfWriter;
import com.wrappedapps.boutique.Constants;
import com.wrappedapps.boutique.business.*;
import com.wrappedapps.boutique.persistancy.*;
public class BuildTransactionSearchPDFAction extends Action
{
// Logging support
private static final Log m_log =
LogFactory.getLog(BuildTransactionSearchPDFAction.class);
public ActionForward execute(ActionMapping mapping, ActionForm
form, HttpServletRequest request, HttpServletResponse response)
{
HttpSession session = request.getSession();
ServletContext context = servlet.getServletContext();
IDataAccess dao = DataAccessObject.getInstance();
BoutiqueConfig config = (BoutiqueConfig)
session.getAttribute(Constants.BOUTIQUE_CONFIG);
//Fetch transactions
ArrayList transIds =
(ArrayList)session.getAttribute(Constants.TRANSACTION_SEARCH_RESULT);
//Get requested Transactions and place Collection in
request
ArrayList transactions = new ArrayList();
for (int i = 0; i < transIds.size(); ++i)
{
Integer tI = (Integer) transIds.get(i);
int transId = tI.intValue();
TransactionRecord tr =
dao.getTransaction(transId);
transactions.add(tr);
}
//Create TransactionSearch pdf and print to
ByteArrayOutputStream
//step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50,
50, 50);
ByteArrayOutputStream baos = new
ByteArrayOutputStream();
try
{
// step 2:
PdfWriter.getInstance(document, baos);
// step 3: we open the document
document.open();
// Text fonts
Font boldWhite = new Font(Font.HELVETICA, 8,
Font.BOLD, Color.WHITE);
Font boldBlack = new Font(Font.HELVETICA, 8,
Font.BOLD, Color.BLACK);
//Table BG colors
Color even = new Color(92, 124, 177);
Color odd = new Color(64, 91, 134);
// step 4: Build header with branding logo, date
and # of transactions
Table hdrTbl = buildHeaderTable(config,
transactions.size(), boldBlack);
document.add(hdrTbl);
//Create a new Table presenting each
TransactionRecord
for(int i = 0; i < transactions.size(); ++i)
{
TransactionRecord tr =
(TransactionRecord)transactions.get(i);
CustomerInfo cust = tr.getCustomer();
boolean isEven = ((i % 2) == 0);
//Build encapsulating table
Table transT = new Table(2);
transT.setWidth(80);
transT.setWidths(new int[]{5, 75});
transT.setSpacing(1);
transT.setBorder(Rectangle.NO_BORDER);
transT.setAlignment(Element.ALIGN_CENTER);
transT.setTableFitsPage(true);
//Numbered Transaction Cell
Cell tnC = new Cell(new Phrase("" + (i +
1) + ".", boldBlack));
tnC.setBorder(Rectangle.NO_BORDER);
tnC.setVerticalAlignment(Element.ALIGN_TOP);
tnC.setHorizontalAlignment(Element.ALIGN_CENTER);
transT.addCell(tnC);
//Build TransactionRecord Table
Table trT = new Table(2, 7);
trT.setWidth(75); // percentage
trT.setSpacing(1);
trT.setWidths(new int[] {13, 62}); //
percentage
trT.setAlignment(Element.ALIGN_CENTER);
trT.setBorderWidth(0);
trT.setBorderColor(Color.BLACK);
trT.setBorder(1);
trT.setTableFitsPage(true);
//Date Row
//Label
Cell dateLbl = new Cell(new Phrase("Date
: ", boldWhite));
dateLbl.setHeader(true);
dateLbl.setBackgroundColor((isEven)?
even: odd);
trT.addCell(dateLbl);
//Value
Cell dateVal = new Cell(new
Phrase(tr.getDateStr(), boldBlack));
dateVal.setHeader(true);
dateVal.setBackgroundColor(Color.WHITE);
trT.addCell(dateVal);
//Reference # Row
//Label
Cell refLbl = new Cell(new
Phrase("Reference # : ", boldWhite));
refLbl.setHeader(true);
refLbl.setBackgroundColor((isEven)?
even: odd);
trT.addCell(refLbl);
//Value
Cell refVal = new Cell(new Phrase("" +
tr.getReferenceNo(), boldBlack));
refVal.setHeader(true);
refVal.setBackgroundColor(Color.WHITE);
trT.addCell(refVal);
//Account # Row
//Label
Cell acctLbl = new Cell(new
Phrase("Account # : ", boldWhite));
acctLbl.setHeader(true);
acctLbl.setBackgroundColor((isEven)?
even: odd);
trT.addCell(acctLbl);
//Value
Cell acctVal = new Cell(new Phrase("" +
tr.getAccountId(), boldBlack));
acctVal.setHeader(true);
acctVal.setBackgroundColor(Color.WHITE);
trT.addCell(acctVal);
//Serial Number Row
//Label
Cell serLbl = new Cell(new
Phrase("Serial # :", boldWhite));
serLbl.setHeader(true);
serLbl.setBackgroundColor((isEven)?
even: odd);
trT.addCell(serLbl);
//Value
Cell serVal = new Cell(new
Phrase(tr.getInstance().getId(), boldBlack));
serVal.setHeader(true);
serVal.setBackgroundColor(Color.WHITE);
trT.addCell(serVal);
//Amount Row
//Label
Cell amtLbl = new Cell(new
Phrase("Amount : ", boldWhite));
amtLbl.setHeader(true);
amtLbl.setBackgroundColor((isEven)?
even: odd);
trT.addCell(amtLbl);
//Value
Cell amtVal = new Cell(new
Phrase(tr.getAmountStr(), boldBlack));
amtVal.setHeader(true);
amtVal.setBackgroundColor(Color.WHITE);
trT.addCell(amtVal);
//Product Row
//Label
Cell prodLbl = new Cell(new
Phrase("Product : ", boldWhite));
prodLbl.setHeader(true);
prodLbl.setBackgroundColor((isEven)?
even: odd);
trT.addCell(prodLbl);
//Value
//Build Product Table
Table prodTbl = buildProductTable(tr,
boldBlack);
trT.insertTable(prodTbl);
//Customer Row
//Label
Cell custLbl = new Cell(new
Phrase("Customer : ", boldWhite));
custLbl.setHeader(true);
custLbl.setBackgroundColor((isEven)?
even: odd);
trT.addCell(custLbl);
//Value
Table custTbl = buildCustomerTable(tr,
boldBlack);
trT.insertTable(custTbl);
//Add TransactionRecord Table to
trasaction table
transT.insertTable(trT);
//Add Table to document
document.add(transT);
//Spacer
//document.add(new Phrase(" ",
boldWhite));
}
// step 5: we close the document
document.close();
//Now Write the PDF to OutputStream
//Remove any header info set by Struts primarily
the Cache-Control
response.reset();
response.setHeader("Content-Disposition","inline;filename=TransactionRep
ort.pdf");
response.setContentType("application/pdf");
response.setContentLength(baos.size());
ServletOutputStream out =
response.getOutputStream();
baos.writeTo(out);
out.flush();
}
catch (Exception e)
{
if(m_log.isErrorEnabled())
m_log.error("BUILDING TRANSACTION PDF: "
+ e);
}
return null;
}
private Table buildProductTable(TransactionRecord tr, Font font)
throws BadElementException, DocumentException
{
InstanceInfo inst = tr.getInstance();
Table prodTbl = new Table(2, 4);
prodTbl.setWidth(100);
prodTbl.setWidths(new int[]{10, 90});
prodTbl.setBorderWidth(1);
//Product Name Row
String pName = inst.getPackageName() + " (" +
inst.getTermName() + ")";
Cell prodName = new Cell(new Phrase(pName, font));
prodName.setColspan(2);
prodTbl.addCell(prodName);
//Service Provider Row
String sp = inst.getInstanceManager();
Cell im = new Cell(new Phrase(sp, font));
im.setColspan(2);
prodTbl.addCell(im);
//Hits Row
//Label
Cell hitsLbl = new Cell(new Phrase("Hits : ", font));
hitsLbl.setBorder(Rectangle.NO_BORDER);
prodTbl.addCell(hitsLbl);
//Value
Cell hitsVal = new Cell(new Phrase(tr.getHits(), font));
prodTbl.addCell(hitsVal);
//Term Row
//Label
Cell termLbl = new Cell(new Phrase("Term : ", font));
prodTbl.addCell(termLbl);
//Value
Cell termVal = new Cell(new Phrase(tr.getTerm(), font));
prodTbl.addCell(termVal);
return prodTbl;
}
private Table buildCustomerTable(TransactionRecord tr, Font
font)
throws BadElementException, DocumentException
{
CustomerInfo cust = tr.getCustomer();
Table custTbl = new Table(2, 8);
custTbl.setWidth(100);
custTbl.setWidths(new int[]{50,50});
custTbl.setBorderWidth(1);
//Cust Name
String cName = cust.getFirstName() + " " +
cust.getLastName();
Cell custName = new Cell(new Phrase(cName, font));
custName.setColspan(2);
custName.setHorizontalAlignment(Element.ALIGN_CENTER);
custTbl.addCell(custName);
//Company
Cell company = new Cell(new Phrase(cust.getCompany(),
font));
company.setColspan(2);
company.setHorizontalAlignment(Element.ALIGN_CENTER);
custTbl.addCell(company);
//Address
Cell address = new Cell(new Phrase(cust.getAddress(),
font));
address.setColspan(2);
address.setHorizontalAlignment(Element.ALIGN_CENTER);
custTbl.addCell(address);
//City/State
Cell city = new Cell(new Phrase(cust.getCity()+ " ," +
cust.getState(), font));
city.setColspan(2);
city.setHorizontalAlignment(Element.ALIGN_CENTER);
custTbl.addCell(city);
//Zip Code
Cell zip = new Cell(new Phrase(cust.getZip(), font));
zip.setColspan(2);
zip.setHorizontalAlignment(Element.ALIGN_CENTER);
custTbl.addCell(zip);
//Country
Cell country = new Cell(new Phrase(cust.getCountry(),
font));
country.setColspan(2);
country.setHorizontalAlignment(Element.ALIGN_CENTER);
custTbl.addCell(country);
//Phone
Cell phone = new Cell(new Phrase(cust.getPhone(),
font));
phone.setColspan(2);
phone.setHorizontalAlignment(Element.ALIGN_CENTER);
custTbl.addCell(phone);
//Email
Cell email = new Cell(new Phrase(cust.getEmail(),
font));
email.setColspan(2);
email.setHorizontalAlignment(Element.ALIGN_CENTER);
custTbl.addCell(email);
return custTbl;
}
private Table buildHeaderTable(BoutiqueConfig config, int
transactions,
Font font)
throws BadElementException, DocumentException,
IOException
{
Table hdrTbl = new Table(2, 2);
hdrTbl.setWidth(100);
hdrTbl.setBorder(Rectangle.NO_BORDER);
hdrTbl.setBorderWidth(0);
//Image and date Row
byte[] imgData = config.getBrandingImage();
//Logo Cell
Cell imgC = new Cell();
imgC.setBorder(Rectangle.NO_BORDER);
if(imgData != null)
{
GifImage logo = new GifImage(imgData);
Image img = logo.getImage(1);
imgC.addElement(img);
}
hdrTbl.addCell(imgC);
//Date Cell
SimpleDateFormat sdf = new
SimpleDateFormat(Constants.SHORT_DATE);
Date now = new Date(System.currentTimeMillis());
Phrase date = new Phrase(sdf.format(now), font);
Cell dateC = new Cell(date);
dateC.setBorder(Rectangle.NO_BORDER);
dateC.setVerticalAlignment(Element.ALIGN_TOP);
dateC.setHorizontalAlignment(Element.ALIGN_RIGHT);
hdrTbl.addCell(dateC);
//Transaction Label
Cell totalC = new Cell(new Phrase("Total Transactions :
" + transactions, font));
totalC.setBorder(Rectangle.NO_BORDER);
totalC.setColspan(2);
totalC.setHorizontalAlignment(Element.ALIGN_CENTER);
hdrTbl.addCell(totalC);
return hdrTbl;
}
}
HTH,
Greg
> -----Original Message-----
> From: Gonzalez Comesa�a [mailto:Gonzalez Comesa�a]
> Sent: Thursday, November 06, 2003 8:27 AM
> To: Struts Users Mailing List; [EMAIL PROTECTED]
> Subject: Struts + itext
>
> Someone have an example of struts with itext or itext with struts
???
> --
>
>
>
>
------------------------------------------------------------------------
--
> ------
> Aquest missatge electr�nic pot contenir informaci� confidencial o
> privilegiada.
> Si vost� no �s el destinatari del missatge, o l'ha rebut per error,
si us
> plau
> notifiqui-ho al remitent i destrueixi el missatge amb tot el seu
> contingut.
> Est� completament prohibida qualsevol c�pia, �s o distribuci� no
> autoritzada
> del contingut d'aquest missatge electr�nic.
>
------------------------------------------------------------------------
--
> ------
> Este mensaje electr�nico puede contener informaci�n confidencial o
> privilegiada.
> Si usted no es el destinatario de este mensaje o lo ha recibido por
> error, por
> favor notif�quelo al remitente y destruya el mensaje con todo su
> contenido.
> Queda expresamente prohibida cualquier copia, utilizaci�n o
> distribuci�n no
> autorizada del contenido de este mensaje electr�nico.
>
------------------------------------------------------------------------
--
> ------
> This e-mail may contain confidential and/or privileged information.
> If you are not the intended recipient (or have received this e-mail
in
> error)
> please notify the sender immediately and destroy this e-mail.
> Any unauthorized copying, disclosure or distribution of the
material
> in this
> e-mail is strictly forbidden.
>
------------------------------------------------------------------------
--
> ------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]