Please read the official documentation, and you'll discover that the example on the unoffical web site you consulted is wrong.
In chapter 5 of my book "iText in Action - Second Edition", you'll find the MovieCountries1 example. As you can see, we use writer.getPageNumber() - 1
in the onCloseDocument()
method.
public void onCloseDocument(PdfWriter writer, Document document) {
ColumnText.showTextAligned(total, Element.ALIGN_LEFT,
new Phrase(String.valueOf(writer.getPageNumber() - 1)),
2, 2, 0);
}
The reason for this is explained in the NOTE on page 152 of the book:
NOTE When the document is closed, the
newPage()
method is triggered to perform finilizations on the current page. WhennewPage()
is called, the page number is augmented, so you need to use(writer.getPageNumber() - 1)
if you want to add the total number of pages in theonCloseDocument()
method.
If you need another example, take a look at the FillTemplateHelper example on the official web site.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.