0
votes

I am trying to create tagged PDF using low level object of itext5 as below

    document.open();
    PdfStructureTreeRoot structureTreeRoot = writer.getStructureTreeRoot();
    PdfStructureElement top = new PdfStructureElement(structureTreeRoot, PdfName.DOCUMENT);
    PdfStructureElement element = new PdfStructureElement(top, PdfName.P);
    PdfContentByte cb = writer.getDirectContent();
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);
    cb.setLeading(16);
    cb.setFontAndSize(bf, 12);
    cb.beginText();
    cb.setTextMatrix(50, 700);
    cb.beginMarkedContentSequence(element);
    cb.newlineShowText("Hello There");
    cb.endMarkedContentSequence();
    cb.endText();
    document.close();`

Code generates pdf which is open in acrobat DC pro but when I check tags,acrobat is not responding. While checking structure tree in itext-rups I found StructureTreeRoot array contains null instead of StructureElement and also in XRef 2nd and 3rd objects are missing Structure tree in itext-rups

I am new to pdf generation and wrote code taking reference from Tagged PDF. It is mandatory to use itext5 low level object for project

1

1 Answers

0
votes

As you are using low level APIs here, there is quite a lot of work you explicitly have to do which otherwise is done automatically for you.

In the case at hand for example you have to explicitly add the PdfStructureElement to the document before closing it, i.e.

[...]
cb.endText();
writer.addToBody(element, element.getReference());
writer.addToBody(top, top.getReference());
document.close();