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

trunkc picture trunkc · Apr 17, 2009 · Viewed 77.9k times · Source

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

Answer

paxdiablo picture paxdiablo · Apr 17, 2009
  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.