trunkc trunkc - 2 months ago 29
Java Question

How to add total page number on every page with iText?

How to add the total page number on every page with iText?

Answer
  1. Process the output from a PdfWriter to a bytestream first with a dummy page count.
  2. Create a PdfReader from that bytestream, calling PdfReader.getNumberOfPages to get the actual page count.
  3. Recreate the PDF output, knowing what the page count will be, changing the footer accordingly.

It's messy, but there's no easy way to know the page count without a two-pass approach. See the example code for details on manipulating PDFs.