How to add a page number in the header of a PDF/A Level A file?

Tags: PDF/Atagged pdfmarked contentartifactiText 7

I am trying to create a document that conforms with PDF/A-1 level A. As long as I don't introduce a header or a footer using a page event, the code works fine. However, when I introduce text that is added at an absolute position, iText throws an exception telling me that the content I'm adding isn't tagged. How do I solve this problem?

Posted on StackOverflow on Dec 16, 2014 by Praan

I've written an example that creates a PDF/A document based on a CSV file. I'm adding a header to this document using a simple HeaderHandler implementation: PdfA1A

The problem you are experiencing can be explained as follows: when you tell the that you need to receive Tagged PDF using pdfDoc.setTagged(), iText will make sure that the appropriate tags are created when adding Element objects to the document. As long as you stick to using high-level objects, this will work.

However, the moment you introduce objects that are added at absolute positions, you take responsibility to correctly tag any content you add. In your case, you are adding a footer. Footers are not part of the "real content", hence they need to be marked as "artifacts".

In my example, I have adapted your implementation in a way that allows me to explain two different approaches:

In the first approach, I set the role at the level of the object:

total = new Image(template);

In the second approach, I mark content as an artifact at the lowest level:

PdfCanvas canvas = new PdfCanvas(page);
canvas.setFontAndSize(font, 12);
canvas.moveText(34, 575);
canvas.moveText(703, 0);
canvas.showText(String.format("Page %d of", pageNum));
canvas.addXObject(template, 0, 0);

I use this second approach to add several elements.

Click this link if you want to see how to answer this question in iText 5.