How to add text at an absolute position on the top of the first page?

Tags: PdfTemplateiText 5

I have a script that creates a PDF file and writes contents to it. After the execution is complete I need to write the status (fail, success) to the PDF, but the status should be on the top of the page. So the solution I came up with is to use absolute positioned text like this:

PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SaveState();
cb.BeginText();
cb.MoveText(700, 30);
cb.SetFontAndSize(bf, 12);
cb.ShowText("My status");
cb.EndText();
cb.RestoreState();
But as the PDF creates multiple pages, this text is added to the last page of the PDF. How can I add it to the 1st page?

Posted on StackOverflow on Nov 9, 2015 by BiJ

iText was written with internet applications in mind. It was designed to flush content from memory as soon as possible: if a page is finished, that page is sent to the OutputStream and there is no way to return to that page.

That doesn't mean your requirement is impossible. PDF has a concept known as Form XObject. In iText, this concept is implemented under the name PdfTemplate. Such a PdfTemplate is a rectangular canvas with a fixed size that can be added to a page without being part of that page.

An example should clarify what that means. Please take a look at the WriteOnFirstPage example. In this example, we create a PdfTemplate like this:

PdfContentByte cb = writer.getDirectContent();
PdfTemplate message = cb.createTemplate(523, 50);

This message object refers to a Form XObject. It is a piece of content that is external to the page content.

We wrap the PdfTemplate inside an Image object. By doing so, we can add the Form XObject to the document just like any other object:

Image header = Image.getInstance(message);
document.add(header);

Now we can add as much data as we want:

for (int i = 0; i < 100; i++) {
    document.add(new Paragraph("test"));
}

Adding 100 "test" lines will cause iText to create 3 pages. Once we're on page 3, we no longer have access to page 1, but we can still write content to the message object:

ColumnText ct = new ColumnText(message);
ct.setSimpleColumn(new Rectangle(0, 0, 523, 50));
ct.addElement(
    new Paragraph(
        String.format("There are %s pages in this document", writer.getPageNumber())));
ct.go();

If you check the resulting PDF write_on_first_page.pdf, you'll notice that the text we've added last is indeed on the first page.