0

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 of each page. How do I do this?

Here is the relevant part of the code.

    while (iteratorPDFReader.hasNext()) {
        PdfReader pdfReader = iteratorPDFReader.next();

        // Create a new page in the target for each source page.
        while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
            document.newPage();
            pageOfCurrentReaderPDF++;
            currentPageNumber++;
            page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
            cb.addTemplate(page, 0, 0);

            document.add(new Paragraph("My Text here"));  //As per the SO answer

        }
        pageOfCurrentReaderPDF = 0;
    }

The code is part of a function which accepts a folder, reads the PDF files in it and merges them into one single file. So, I would like to add the text in the above loop itself, instead of iterating the file once again.

Community
  • 1
  • 1
janenz00
  • 3,315
  • 5
  • 28
  • 37

3 Answers3

5

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks for the detailed answer! I don't own the book, but I have been referring the examples. The code I pasted above was formed from the example code. As I explained, I have a few files which needs to be appended with some content (diffrent for each page) and then merged to a single PDF. Here is the pastebin for the entire function http://pastebin.com/Xn8JzrAz Is it using more resources than it should? – janenz00 Nov 20 '12 at 07:14
  • 1
    It's strange code. Please read http://www.manning.com/lowagie2/samplechapter6.pdf for a complete overview explaining how to concatenate PDFs correctly. In your code, you're throwing away all possible interactivity (links, annotations,...) that exists in the original documents. The example on p188 (under the title "Adding content with PdfCopy") solves the problem in a much better way. – Bruno Lowagie Nov 20 '12 at 07:32
  • ;) ok, while I posted it as an answer, @BrunoLowagie also referenced the sample best matching the requirements in his comment here... – mkl Nov 20 '12 at 07:39
  • Down vote for "I'm the original developer of iText". Comment your code. – user1566694 Feb 28 '17 at 18:45
2

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.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • ColumnText.showTextAligned line in the example gives me a nullpointer error. Any idea why? – janenz00 Nov 21 '12 at 01:52
  • The example should not fail. I will run it as soon as I'm in office to check. Are you trying the example as is or have you already adapted it to your use case? In the latter case: how? – mkl Nov 21 '12 at 06:35
  • I just ran the example (using the most current versions of iText and the samples from iText in Action, 2nd edition) and it executed flawlessly. Thus, there must be some problem with the way you ran it. – mkl Nov 21 '12 at 08:52
0

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;
hd1
  • 33,938
  • 5
  • 80
  • 91
  • That technique hasn't been documented since 2005. It was deliberately omitted in the book that was written in 2006 and the functionality was removed from iText in 2008 or 2009. – Bruno Lowagie Nov 20 '12 at 06:43
  • Yes, my jar doesn't have the HeaderFooter class. – janenz00 Nov 20 '12 at 07:17
  • 1
    @BrunoLowagie the linked document had your technique, but the snippet I put up seemed clearer. – hd1 Nov 21 '12 at 02:17