iText page number in header within PDF/A

泪湿孤枕 提交于 2019-11-29 15:51:42

Please take a look at this line:

table.addCell(String.format("Page %d of", writer.getPageNumber()));

In this line, you add a String to the table directly. Internally, a PdfPCell is created, as well as a Phrase object. As no font is defined, the default font (Helvetica, unembedded) is used.

Please change the above line into:

table.addCell(new Phrase(String.format("Page %d of", writer.getPageNumber()), fontArial);

This will solve already one problem (unless I've overlooked other instances where the default font is introduced). There will be other exceptions if you want PDF/A Level A, though.

I've written an example that creates a PDF/A document based on a CSV file. I'm adding a footer to this document reusing your 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).

See http://itextpdf.com/sandbox/pdfa/PdfA1A for the full source code.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!