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?
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 How to add a page number in the header of a PDF/A Level A file? if you want to see how to answer this question in iText 5.