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

  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.

Leave a Comment