This is the complete code that I have used:

public class PDFTest2 extends PdfPageEventHelper
{

   private final static String reportTitle = "pdfText.pdf";

   public PdfTemplate tpl;
   public BaseFont helv;

   public static void main(String[] args)
   {
       Document document = new Document(PageSize.A4, 50, 50, 50, 100);
       try
       {
           PdfWriter writer = PdfWriter.getInstance(document,
                   new FileOutputStream(reportTitle));
           writer.setPageEvent(new PDFTest2());
           document.open();
           String text = "Lots of text. ";
           for (int k = 0; k < 10; ++k)
               text += text;
           document.add(new Paragraph(text));
           document.close();
       }
       catch (Exception de)
       {
           de.printStackTrace();
       }
   }

   public void onOpenDocument(PdfWriter writer, Document document)
   {
       try
       {
           // initialization of the template
           tpl = writer.getDirectContent().createTemplate(100, 100);
           tpl.setBoundingBox(new Rectangle(-20, -20, 100, 100));

helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);

       }
       catch (Exception e)
       {
           throw new ExceptionConverter(e);
       }
   }

   public void onEndPage(PdfWriter writer, Document document)
   {

       try
       {
           Rectangle page = document.getPageSize();

           // Header
           PdfPTable headerTable = new PdfPTable(1);
           PdfPCell cell = new PdfPCell(new Paragraph("Header Text"));
           headerTable.addCell(cell);
headerTable.setTotalWidth(page.getWidth() - document.leftMargin()
                   - document.rightMargin());
           headerTable.writeSelectedRows(0, -1, document.leftMargin(), page
                   .getHeight()
- document.topMargin() + headerTable.getTotalHeight() + 10,
                   writer.getDirectContent());

           // Footer
           Image img = Image.getInstance(tpl);
           PdfPTable footTable = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell(new Paragraph("First cell of footer"));
           PdfPCell cell2 = new PdfPCell(img);
           cell2.setVerticalAlignment(Element.ALIGN_TOP);
           footTable.addCell(cell1);
           footTable.addCell(cell2);
           footTable.setTotalWidth(page.getWidth() - document.leftMargin()
                   - document.rightMargin());
footTable.writeSelectedRows(0, -1, document.leftMargin(), document
                   .bottomMargin(), writer.getDirectContent());
       }
       catch (Exception e)
       {
           throw new ExceptionConverter(e);
       }
   }
public void onCloseDocument(PdfWriter writer, Document document) {
       tpl.beginText();
       tpl.setFontAndSize(helv, 12);
       tpl.setTextMatrix(0, 0);
       tpl.showText("Number of pages = " + (writer.getPageNumber() - 1));
       tpl.endText();
    }
}


And attached you can find how the pdf file looks like.

br,
Daniel

Bruno Lowagie escribió:
Daniel Fernández wrote:
But nothing is shown in the cell, so I don't know if I'm doing something wrong.

Show us the PDF.
The text is probably there, but not in a place that is visible.
br,
Bruno

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Attachment: pdfText.pdf
Description: Adobe PDF document

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to