Header and footer iText 5

how can i add a pdf header to my page and footer ? I want a table with 3 columns in the header and another table, 3 columns in the footer. My page can be A3 or A4, as well as a landscape or portrait.

Can anyone help me? I cannot find good examples on the Internet.

Thanks!

Tommaso

+4
source share
2 answers
  • Create a class MyPageEventListener that extends PdfPageEventHelper
  • Add a page event listener to a PdfWriter object
  • In the onEndPage method of the MyPageEventListener class, place the code for the header / footer

Example:

public class MyPageEventListener extends PdfPageEventHelper { . . . @Override public void onEndPage(PdfWriter writer, Document document) { //code skeleton to write page header PdfPTable tbl = new PdfPTable(3); tbl.addCell("1st cell"); tbl.addCell("2nd cell"); tbl.addCell("3rd cell"); float x = document.leftMargin(); float hei = getMyHeaderHeight(); //custom method that return header height //align bottom between page edge and page margin float y = document.top() + hei; //write the table tbl.writeSelectedRows(0, -1, x, y, writer.getDirectContent()); } } 

to register a listener just do

 writer.setPageEvent(new MyPageEventListener()); 
+4
source

The easiest way to do this is to first create the contents of the entire PDF file in memory, and then, once all the pages have been created, you need to open the PDF file in memory in pdfStamper and iterate over all the pages added to the header and footer objects are the correct coordinates.

If you do a quick Google search for adding page numbers to itextPDF, you will find a number of examples that you can quickly adapt to your needs.

The key is that this is done after the pdf is created, not before.

0
source

Source: https://habr.com/ru/post/1394418/


All Articles