iText - add content to the bottom of an existing page

后端 未结 3 1063
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.

    0 讨论(0)
  • 2021-01-27 09:53

    Have a look at chapter 6 of iText in Action, 2nd edition, especially at subsection 6.4.1: Concatenating and splitting PDF documents.

    Listing 6.22, ConcatenateStamp.java, shows you how you should create a PDF from copies of pages (in your case: all pages) of multiple other PDFs; the sample additionally adds a new "Page X of Y" footer; this demonstrates how you can add content at given positions on the pages while merging the source files.

    0 讨论(0)
  • 2021-01-27 10:04

    Perhaps this may be of assistance here... I suspect you want to do something like the following:

    cb.addTemplate(page, 0, 0);
    
    document.add(new Paragraph("My Text here"));  
    document.setFooter(new HeaderFooter("Footnote goes here"));
    }
    pageOfCurrentReaderPDF = 0;
    
    0 讨论(0)
提交回复
热议问题