admin管理员组

文章数量:1278983

I am trying to make my PDF generated by the PDFBox Java library accessible, and this requires tagging the embedded images in the file so that it can be read by a screen reader.

After referencing a few online resources, I wrote the following code that makes use of PDFMarkedContent to give an alternative text of the image.

However, when I run the generated document through the PDF accessibility checker from PAC, it still shows that the QR code image is untagged. How can I debug/add the alternative text to the QR code image correctly? Thanks!

Here is my code snippet for drawing the image on each page of the PDF

for (int pageidx = 0; pageidx < totalPage; pageidx++) {
    PDPageContentStream contentstream = new PDPageContentStream(
        doc, doc.getPage(pageidx), PDPageContentStream.AppendMode.APPEND, true);
    PDPage page = doc.getPage(pageidx);

    COSDictionary dict = new COSDictionary();
    dict.setInt(COSName.MCID, mcid);
    mcid++;

    contentstream.beginMarkedContent(
        COSName.IMAGE, PDPropertyList.create(dict));
    contentstream.drawImage(image, x, y, px, py);
    contentstream.endMarkedContent();

    contentstream.close();

    PDStructureTreeRoot root = new PDStructureTreeRoot();
    doc.getDocumentCatalog().setStructureTreeRoot(root);

    PDStructureElement parent =
        new PDStructureElement(StandardStructureTypes.Figure, root);
    root.appendKid(parent);

    PDStructureElement element =
        new PDStructureElement(StandardStructureTypes.Figure, parent);
    element.setPage(doc.getPage(pageIndex));
    element.setAlternateDescription("alternate");

    dict.setString(COSName.ALT, "alternate");
    PDMarkedContent marked = new PDMarkedContent(COSName.IMAGE, dict);
    marked.addXObject(image);
    element.appendKid(marked);
}

本文标签: javaHow to add alternative text to image in PDF using PDFBoxStack Overflow