简体   繁体   中英

getting extra page count in pdf using itext api with Java

I have an issue with the page count in a PDF.

Suppose that we have a pdf with page count 3, but in the footer it's returning "Page 1 Of 4". We are getting the 4 from the Eventer class.

Jar : com.lowagie.text-2.1.7.jar

Eventer class : PdfPageEventHelper.java

在此处输入图片说明

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. When newPage() 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 the onCloseDocument() 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM