How to add the total page number on every page with iText?
+2
A:
From a quick web search to remind myself of this; take a look at
The relevant methods are onEndPage()
to produce the "X" and onCloseDocument()
to produce the "Y" once you hit the end of the document.
Chris Clarke
2009-04-17 10:43:35
Seems to be the prefered way. But there's a problem with the calculated 'adjust' variable: it's calculated for exactly one character: 'helv.getWidthPoint("0", 12)'. :-(
bwalliser
2009-04-17 11:06:50
+2
A:
You need to process the output from a PdfWriter to a bytestream first with a dummy page count.
Then create a PdfReader from that bytestream, calling PdfReader.getNumberOfPages to get the actual page count.
Then recreate the PDF output, knowing what the page count will be, changing the footer accordingly.
See here for an example.
It's messy, I know, but there's no easy way to know the page count until after you've generated the PDF, hence the two-pass approach.
paxdiablo
2009-04-17 10:50:36
Agreed, it's really ugly. But it's also the only way to go with iText. If anybody knows a better Java PDF library (preferably free / OSS), I'd love to know about it.
Ian McLaird
2009-04-17 15:32:04