iText 5 header and footer

后端 未结 2 822
野趣味
野趣味 2020-12-21 02:20

how I can add in my PDF page the header and the footer? I wanna a table with 3 column in header and other table, 3 column in the footer. My

相关标签:
2条回答
  • 2020-12-21 02:31
    1. Create a class MyPageEventListener that extends PdfPageEventHelper
    2. Add a page event listener to the PdfWriter object
    3. In the onEndPage method of MyPageEventListener class, put the code for 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's 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 the listener simply do

    writer.setPageEvent(new MyPageEventListener());
    
    0 讨论(0)
  • 2020-12-21 02:52

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

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

    The key is that it is done after you create the pdf, not before.

    0 讨论(0)
提交回复
热议问题