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

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);
total.setRole(PdfName.Artifact);

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

PdfCanvas canvas = new PdfCanvas(page);
canvas.beginText();
canvas.setFontAndSize(font, 12);
canvas.beginMarkedContent(PdfName.Artifact);
canvas.moveText(34, 575);
canvas.showText("Test");
canvas.moveText(703, 0);
canvas.showText(String.format("Page %d of", pageNum));
canvas.endText();
canvas.stroke();
canvas.addXObject(template, 0, 0);
canvas.endMarkedContent();

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.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now