iText - add content to the bottom of an existing page

后端 未结 3 1062
Happy的楠姐
Happy的楠姐 2021-01-27 09:28

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

3条回答
  •  北海茫月
    2021-01-27 09:44

    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:

    • Do not use onStartPage() to add content,
    • Do not add anything to the Document object passed to the page event,
    • Unless you specified a different page size, the lower-left corner has the coordinate 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.

提交回复
热议问题