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

Category: 
Tags: PDF/Atagged pdfmarked contentartifactiText 5

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 footer to this document using a simple page event implementation: PdfA1A

The problem you are experiencing can be explained as follows: when you tell the PdfWriter that it needs to create Tagged PDF (using writer.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 page event 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:

Image total = Image.getInstance(t);
total.setRole(PdfName.ARTIFACT);

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

PdfContentByte canvas = writer.getDirectContent();
canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
table.writeSelectedRows(0, -1, 36, 30, canvas);
canvas.endMarkedContentSequence();

I use this second approach for PdfPTable because if I don't, I would have to tag all sub-elements of the table (every single row) as an artifact. If I didn't, iText would introduce TR elements inside an artifact (and that would be wrong).