I want to add a piece of text to every page of a PDF file. This answer in SO works fine. But, the text is added to the top of the page. I would like to add my text to the bottom
If you want to automatically add content to every page, you need a page event. This is explained in chapter 5 of my book" iText in Action - Second Edition". If you don't own a copy of the book, you can consult the examples here. You can also find solutions by looking for the keyword Header / Footer.
The example you're referring to doesn't look correct at first sight. Sure, you can use "two passes", one to create the content, and another to add headers or footers, but the suggested solution is different from the recommended solution: http://itextpdf.com/examples/iia.php?id=118
You are copying the mistake in your question: why on earth would you import the document you've just created into a new document, thus throwing away all possible interactivity you've added to that document? It just doesn't make sense. It's unbelievable that this answer received that many up-votes. I'm the original developer of iText and I'm not at all happy with that answer!
In your case, there may be no need to create the document in memory first and to add the footer afterwards. Just take a look at http://itextpdf.com/examples/iia.php?id=104
You need to create a PdfPageEvent
implementation (for instance using PdfPageEventHelper
) and you need to implement the onEndPage()
method.
Documented caveats:
onStartPage()
to add content,Document
object passed to the page event,x = 0; y = 0
. You need to take that into account when adding the footer. The y-value for the footer is lower than the y-value for the header.For more info: consult my book.