Home > Net >  How to getCurrentPageNumber() in Itext 7
How to getCurrentPageNumber() in Itext 7

Time:02-22

In Itext 5, PdfWriter used to have a method called getCurrentPageNumber(). Itext 7 does not have it anymore.

How can I get the current page number since the entire pdf is going to use the same Document instance ( PdfDocument pdfDocument = new PdfDocument(writer))from start to end?

Use case: An apllication written using itext5 needs to be rewritten using itext7. Our app generates PDF files for our customer's monthly bills.

Structure of the pdf:

  1. Create document:

     PdfWriter writer = new PdfWriter(new FileOutputStream(filename));
     PdfDocument pdfDocument = new PdfDocument(writer);
     Document document = new Document(pdfDocument, PageSize.A4);
     document.setMargins(235, 35, 0, 38);
     buildBody(pdfData, document, writer);
    
  2. For every page I use an IEventHandler for the Start and another one for the End of the pages to add some data at the Header and Footer.

     pdfDocument.addEventHandler(PdfDocumentEvent.START_PAGE, new  
     StartPageHandler<>(pdfData, document, this));
     pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new  
     EndPageHandler<>(pdfData, document, this));
    

3.Each IEventHandler instance has this format(except the rectangle values):

Rectangle rectangle = new Rectangle(51, 50, 500, 60); (This is for the END_PAGE)
Rectangle[] columns = new Rectangle[] {rectangle};
document.setRenderer(new ColumnDocumentRenderer(document, columns));
document.add(lineSeparatorHeader);
document.add(paragraphInfo); // information used in the Header/Footer
  1. The buildBody(pdfData, document, writer) method (from step 1) was using the writer.getCurrentPageNumber() method in order to check if the content we wanted to add has exceeded the space on the page and has passed on another page. If it passed on another page, I would have to add some extra information (somehow as a title in the body of the page) before the rest of the content is added. (It used to work with itext5)

CodePudding user response:

Considering that the code you want to port apparently used the iText 5 PdfDocument/PdfWriter pattern, the current page number in that code always was the currently last page number, i.e. the current number of pages in the document being created.

In iText 7 these numbers are not automatically the same; if you port the code keeping the general architecture, though, those numbers most likely will remain the same in your iText 7 code, too.

In iText 7 you can get the current number of pages from the PdfDocument in question:

PdfDocument pdfDocument = ...;
...
int currentNamberOfPages = pdfDocument.getNumberOfPages();

Currently your buildBody method signature does not include the PdfDocument (which made sense in iText 5). For iText 7 you should consider changing the signature to include the PdfDocument. Most likely, though, you won't need the PdfWriter anymore which you then can exclude.

If you need to keep that method signature for some obscure reason, you can retrieve the PdfDocument from the Document using the getPdfDocument method.

  • Related