javapdfpdfboxtagged-pdf

PDFBox I need to wrap the link tags with reference tag


I need to wrap the link tags with the reference tag.

private static PDStructureElement wrapInElement(String structType, PDStructureElement pdStructureElement) {
    PDStructureNode parentNode = pdStructureElement.getParent();
    PDStructureElement structureElement = null;
    if (parentNode!=null && parentNode instanceof PDStructureElement)  {
        structureElement = new PDStructureElement(structType,parentNode);
        structureElement.setElementIdentifier("wrap"+noteID++);
        parentNode.insertBefore(structureElement,pdStructureElement);
        structureElement.setParent(parentNode);
        structureElement.appendKid(pdStructureElement);
        parentNode.removeKid(pdStructureElement);
    }
    return structureElement;
}

And it does the structural changes. But in PAC3 it shows below error,

"Link" annotation is not nested inside a "Link" structure element

Please help me to solve the problem. Sample PDF here.


Solution

  • parentNode.removeKid(pdStructureElement);

    Should be done before "appendkid".

    private static PDStructureElement wrapInElement(String structType, PDStructureElement pdStructureElement) {
                PDStructureNode parentNode = pdStructureElement.getParent();
                PDStructureElement structureElement = null;
                if (parentNode!=null && parentNode instanceof PDStructureElement)  {
                    structureElement = new PDStructureElement(structType,parentNode);
                    parentNode.insertBefore(structureElement,pdStructureElement);
                    structureElement.setParent(parentNode);
                    parentNode.removeKid(pdStructureElement);
                    structureElement.appendKid(pdStructureElement);
                }
                return structureElement;
            }