Page events for headers and footers

31st May 2016
admin-marketing

Switch code for this example

TableFooter.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/22122340/creating-table-with-2-rows-in-pdf-footer-using-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.events.Event;
  16. import com.itextpdf.kernel.events.IEventHandler;
  17. import com.itextpdf.kernel.events.PdfDocumentEvent;
  18. import com.itextpdf.kernel.geom.PageSize;
  19. import com.itextpdf.kernel.geom.Rectangle;
  20. import com.itextpdf.kernel.pdf.PdfDocument;
  21. import com.itextpdf.kernel.pdf.PdfPage;
  22. import com.itextpdf.kernel.pdf.PdfWriter;
  23. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  24. import com.itextpdf.layout.Canvas;
  25. import com.itextpdf.layout.Document;
  26. import com.itextpdf.layout.element.AreaBreak;
  27. import com.itextpdf.layout.element.Cell;
  28. import com.itextpdf.layout.element.Paragraph;
  29. import com.itextpdf.layout.element.Table;
  30. import com.itextpdf.samples.GenericTest;
  31. import com.itextpdf.test.annotations.type.SampleTest;
  32.  
  33. import org.junit.experimental.categories.Category;
  34.  
  35. import java.io.File;
  36.  
  37. @Category(SampleTest.class)
  38. public class TableFooter extends GenericTest {
  39.     public static final String DEST = "./target/test/resources/sandbox/events/table_footer.pdf";
  40.  
  41.     public static void main(String[] args) throws Exception {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new TableFooter().manipulatePdf(DEST);
  45.     }
  46.  
  47.     @Override
  48.     protected void manipulatePdf(String dest) throws Exception {
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  50.         Document doc = new Document(pdfDoc, PageSize.A4);
  51.         doc.setMargins(36, 36, 72, 36);
  52.  
  53.         Table table = new Table(1);
  54.         table.setWidth(523);
  55.  
  56.         Cell cell = new Cell().add(new Paragraph("This is a test doc"));
  57.         cell.setBackgroundColor(Color.ORANGE);
  58.         table.addCell(cell);
  59.         cell = new Cell().add(new Paragraph("This is a copyright notice"));
  60.         cell.setBackgroundColor(Color.LIGHT_GRAY);
  61.         table.addCell(cell);
  62.  
  63.         pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new TableFooterEventHandler(table));
  64.  
  65.         for (int i = 0; i < 150; i++) {
  66.             doc.add(new Paragraph("Hello World!"));
  67.         }
  68.         doc.add(new AreaBreak());
  69.         doc.add(new Paragraph("Hello World!"));
  70.         doc.add(new AreaBreak());
  71.         doc.add(new Paragraph("Hello World!"));
  72.  
  73.         doc.close();
  74.     }
  75.  
  76.  
  77.     protected class TableFooterEventHandler implements IEventHandler {
  78.         private Table table;
  79.  
  80.         public TableFooterEventHandler(Table table) {
  81.             this.table = table;
  82.         }
  83.  
  84.         @Override
  85.         public void handleEvent(Event event) {
  86.             PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  87.             PdfDocument pdfDoc = docEvent.getDocument();
  88.             PdfPage page = docEvent.getPage();
  89.             PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
  90.             new Canvas(canvas, pdfDoc, new Rectangle(36, 20, page.getPageSize().getWidth() - 72, 50))
  91.                     .add(table);
  92.         }
  93.     }
  94. }
TableHeader.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/35449606
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.kernel.events.Event;
  15. import com.itextpdf.kernel.events.IEventHandler;
  16. import com.itextpdf.kernel.events.PdfDocumentEvent;
  17. import com.itextpdf.kernel.geom.PageSize;
  18. import com.itextpdf.kernel.geom.Rectangle;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfPage;
  21. import com.itextpdf.kernel.pdf.PdfWriter;
  22. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  23. import com.itextpdf.layout.Canvas;
  24. import com.itextpdf.layout.Document;
  25. import com.itextpdf.layout.element.AreaBreak;
  26. import com.itextpdf.layout.element.Paragraph;
  27. import com.itextpdf.layout.element.Table;
  28. import com.itextpdf.layout.layout.LayoutArea;
  29. import com.itextpdf.layout.layout.LayoutContext;
  30. import com.itextpdf.layout.renderer.TableRenderer;
  31. import com.itextpdf.samples.GenericTest;
  32. import com.itextpdf.test.annotations.type.SampleTest;
  33.  
  34. import org.junit.experimental.categories.Category;
  35.  
  36. import java.io.ByteArrayOutputStream;
  37. import java.io.File;
  38.  
  39. @Category(SampleTest.class)
  40. public class TableHeader extends GenericTest {
  41.     public static final String DEST = "./target/test/resources/sandbox/events/table_header.pdf";
  42.  
  43.     public static void main(String[] args) throws Exception {
  44.         File file = new File(DEST);
  45.         file.getParentFile().mkdirs();
  46.         new TableHeader().manipulatePdf(DEST);
  47.     }
  48.  
  49.     @Override
  50.     protected void manipulatePdf(String dest) throws Exception {
  51.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  52.         Document doc = new Document(pdfDoc, PageSize.A4);
  53.  
  54.         TableHeaderEventHandler handler = new TableHeaderEventHandler(doc);
  55.         pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, handler);
  56.         doc.setMargins(20 + handler.getTableHeight(), 36, 36, 36);
  57.         for (int i = 0; i < 50; i++) {
  58.             doc.add(new Paragraph("Hello World!"));
  59.         }
  60.         doc.add(new AreaBreak());
  61.         doc.add(new Paragraph("Hello World!"));
  62.         doc.add(new AreaBreak());
  63.         doc.add(new Paragraph("Hello World!"));
  64.  
  65.         doc.close();
  66.     }
  67.  
  68.  
  69.     public class TableHeaderEventHandler implements IEventHandler {
  70.         protected Table table;
  71.         protected float tableHeight;
  72.         protected Document doc;
  73.  
  74.         public TableHeaderEventHandler(Document doc) {
  75.             this.doc = doc;
  76.             table = new Table(1);
  77.             table.setWidth(523);
  78.             table.addCell("Header row 1");
  79.             table.addCell("Header row 2");
  80.             table.addCell("Header row 3");
  81.             TableRenderer renderer = (TableRenderer) table.createRendererSubTree();
  82.             renderer.setParent(new Document(new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))).getRenderer());
  83.             tableHeight = renderer.layout(new LayoutContext(new LayoutArea(0, PageSize.A4))).getOccupiedArea().getBBox().getHeight();
  84.         }
  85.  
  86.         @Override
  87.         public void handleEvent(Event event) {
  88.             PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  89.             PdfDocument pdfDoc = docEvent.getDocument();
  90.             PdfPage page = docEvent.getPage();
  91.             PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
  92.             Rectangle rect = new Rectangle(pdfDoc.getDefaultPageSize().getX() + doc.getLeftMargin(),
  93.                     pdfDoc.getDefaultPageSize().getTop() - doc.getTopMargin(), 100, getTableHeight());
  94.             new Canvas(canvas, pdfDoc, rect)
  95.                     .add(table);
  96.         }
  97.  
  98.         public float getTableHeight() {
  99.             return tableHeight;
  100.         }
  101.     }
  102. }
TextFooter.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27780756/adding-footer-with-itext-doesnt-work
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.events.Event;
  16. import com.itextpdf.kernel.events.IEventHandler;
  17. import com.itextpdf.kernel.events.PdfDocumentEvent;
  18. import com.itextpdf.kernel.font.PdfFontFactory;
  19. import com.itextpdf.kernel.geom.Rectangle;
  20. import com.itextpdf.kernel.pdf.PdfDocument;
  21. import com.itextpdf.kernel.pdf.PdfWriter;
  22. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  23. import com.itextpdf.layout.Document;
  24. import com.itextpdf.layout.element.AreaBreak;
  25. import com.itextpdf.layout.element.Paragraph;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. import java.io.File;
  32. import java.io.IOException;
  33.  
  34. @Category(SampleTest.class)
  35. public class TextFooter extends GenericTest {
  36.     public static final String DEST = "./target/test/resources/sandbox/events/text_footer.pdf";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new TextFooter().manipulatePdf(DEST);
  42.     }
  43.  
  44.     @Override
  45.     protected void manipulatePdf(String dest) throws Exception {
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  47.         Document doc = new Document(pdfDoc);
  48.         pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));
  49.         for (int i = 0; i < 3; ) {
  50.             i++;
  51.             doc.add(new Paragraph("Test " + i));
  52.             if (3 != i) {
  53.                 doc.add(new AreaBreak());
  54.             }
  55.         }
  56.         doc.close();
  57.     }
  58.  
  59.  
  60.     protected class TextFooterEventHandler implements IEventHandler {
  61.         protected Document doc;
  62.  
  63.         public TextFooterEventHandler(Document doc) {
  64.             this.doc = doc;
  65.         }
  66.  
  67.         @Override
  68.         public void handleEvent(Event event) {
  69.             PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  70.             PdfCanvas canvas = new PdfCanvas(docEvent.getPage());
  71.             Rectangle pageSize = docEvent.getPage().getPageSize();
  72.             canvas.beginText();
  73.             try {
  74.                 canvas.setFontAndSize(PdfFontFactory.createFont(FontConstants.HELVETICA_OBLIQUE), 5);
  75.             } catch (IOException e) {
  76.                 e.printStackTrace();
  77.             }
  78.             canvas.moveText((pageSize.getRight() - doc.getRightMargin() - (pageSize.getLeft() + doc.getLeftMargin())) / 2 + doc.getLeftMargin(), pageSize.getTop() - doc.getTopMargin() + 10)
  79.                     .showText("this is a header")
  80.                     .moveText(0, (pageSize.getBottom() + doc.getBottomMargin()) - (pageSize.getTop() + doc.getTopMargin()) - 20)
  81.                     .showText("this is a footer")
  82.                     .endText()
  83.                     .release();
  84.         }
  85.     }
  86. }
VariableHeader.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/21628429/itextsharp-how-to-generate-a-report-with-dynamic-header-in-pdf-using-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.events.Event;
  16. import com.itextpdf.kernel.events.IEventHandler;
  17. import com.itextpdf.kernel.events.PdfDocumentEvent;
  18. import com.itextpdf.kernel.font.PdfFontFactory;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.AreaBreak;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34.  
  35. @Category(SampleTest.class)
  36. public class VariableHeader extends GenericTest {
  37.     public static final String DEST = "./target/test/resources/sandbox/events/variable_header.pdf";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new VariableHeader().manipulatePdf(DEST);
  43.     }
  44.  
  45.     public static List getFactors(int n) {
  46.         List factors = new ArrayList();
  47.         for (int i = 2; i
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now