最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to properly associate PDAnnotationLink with PDStructureElement for PDFUA compliance? - Stack Overflow

programmeradmin2浏览0评论

I'm using PDFBox 3.0.2 to generate an accessible PDF with structured elements, but I am failing the PAC (PDF Accessibility Checker) test because I cannot properly associate a PDAnnotationLink with its PDStructureElement.

Here's the relevant code I am using:

// Step 1: Create structure element
PDStructureElement element = new PDStructureElement(StandardStructureTypes.LINK, parentElement);

// Step 2: Create the accessible link annotation
PDAnnotationLink link = new PDAnnotationLink();
link.setPage(page);
PDRectangle linkRect = new PDRectangle(100, 100, 120, 15);
link.setRectangle(linkRect );
link.setContents(layoutInfo.text());
link.getCOSObject().setBoolean("IsMap", false);

PDObjectReference objRef = new PDObjectReference();
objRef.setReferencedObject(link);
element.appendKid(objRef);

link.setStructParent(structParentCounter);

page.getAnnotations().add(link);

PDActionGoTo action = new PDActionGoTo();

PDPageXYZDestination destination = new PDPageXYZDestination();
destination.setPage(page);
destination.setTop((int) 83);
action.setDestination(destination);
link.setAction(action);

page.getAnnotations().add(link);

// Step 3: Associate the marked content reference with the structure element
PDMarkedContentReference mcr = new PDMarkedContentReference();
mcr.setMCID(mcidCounter);
element.appendKid(mcr);

// Step 4: Define marked content properties
COSDictionary markedContentDictionary = new COSDictionary();
markedContentDictionary.setInt(COSName.MCID, mcidCounter);
markedContentDictionary.setString(COSName.CONTENTS, "*");

try {
    PDPageContentStream contentStream = getCurrentLayoutState().contentStream();
    contentStream.setLeading(LINIE_SPACING);
    
    // Step 5: Begin marked content
    contentStream.beginMarkedContent(COSName.getPDFName("link"), PDPropertyList.create(markedContentDictionary ));
    contentStream.beginText();
    contentStream.newLineAtOffset(100, 100);
    contentStream.showText("*");
    contentStream.endText();
    contentStream.endMarkedContent();

    // Step 6: Move to next MCID
    mcidCounter++;
    structureElementArray.add(element);

} catch (IOException e) {
    e.printStackTrace();
}

Even though I have structured the content, PAC still does not recognize the PDAnnotationLink as part of the structure. The annotation is visible in the PDF, but it does not appear as part of the Link structure in accessibility tools.

How can I properly associate PDAnnotationLink with its corresponding PDStructureElement so that PAC recognizes the link annotation as part of the structure?

Additional notes

  • PDFBox Version: 3.0.2
  • Goal: PDF/UA Compliance
  • Issue: PDAnnotationLink not connected to PDStructureElement

What I've tried: using appendKid(), PDMarkedContentReference, and setting a COSDictionary, but the link is still not detected correctly.

发布评论

评论列表(0)

  1. 暂无评论