用PDFBox标记PDF

Leo*_*yka 6 java accessibility pdfbox tagged-pdf

是否可以使用PDFBox创建标记PDF(PDF/UA)?看起来PDFBox有一个API(包org.apache.pdfbox.pdmodel.documentinterchange.taggedpdf),但我找不到任何教程或代码示例.

使用下面的代码,我生成了一个包含图像的PDF文件,屏幕阅读器NVDA(在我的例子中)识别它并读取'... graphic Alternate Description'.但是,辅助功能检查器PAC 2显示错误:"图像对象未标记".

        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);
        PDDocumentCatalog documentCatalog = doc.getDocumentCatalog();

        PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
        PDPageContentStream contents = new PDPageContentStream(doc, page);
        contents.drawImage(pdImage, 100, 600, pdImage.getWidth() / 2, pdImage.getHeight() / 2);
        contents.close();

        PDStructureTreeRoot treeRoot = new PDStructureTreeRoot();
        PDStructureElement structureElement = new PDStructureElement(StandardStructureTypes.Figure, treeRoot);
        structureElement.setPage(page);

        PDMarkedContent markedImg = new PDMarkedContent(COSName.IMAGE, new COSDictionary());
        markedImg.addXObject(pdImage);

        structureElement.appendKid(markedImg);
        structureElement.setAlternateDescription("Alternate Description");
        treeRoot.appendKid(structureElement);
        documentCatalog.setStructureTreeRoot(treeRoot);
        // ....
        doc.save(fileName);
Run Code Online (Sandbox Code Playgroud)

您能否就此主题提供一些解释或/和代码示例?

mlo*_*ell 5

我提出了一个工作示例,演示使用 PDFBox 2 创建可访问的 PDF: https ://github.com/martinlovell/accessible-pdfbox-example

问题中的代码缺少一些内容。标记的内容需要替代文本,我相信您需要该标记内容的 mcid。

示例项目更详细地演示了您的需求。

它会是这样的:

PDPageContentStream contents = new PDPageContentStream(doc, page);

// the content in the stream needs an id
int mcid = 5;
COSDictionary dictionary = new COSDictionary();
dictionary = new COSDictionary();
dictionary(COSName.MCID, mcid);

// wrap image drawing in marked content
contents.beginMarkedContent(COSName.IMAGE, PDPropertyList.create(dictionary));
contents.drawImage(pdImage, 100, 600, pdImage.getWidth() / 2, pdImage.getHeight() / 2);
contents.endMarkedContent();

contents.close();

PDStructureTreeRoot treeRoot = new PDStructureTreeRoot();
documentCatalog.setStructureTreeRoot(treeRoot);
PDStructureElement structureElement = new PDStructureElement(StandardStructureTypes.Figure, treeRoot);
structureElement.setPage(page);
structureElement.setAlternateDescription("Alternate Description");

// Set alt text on marked content for structure.  
// This is the dictionary with the mcid used in beginMarkedContent.
dictionary.setString(COSName.ALT, "Alternate Description");
PDMarkedContent markedImg = new PDMarkedContent(COSName.IMAGE, dictionary);
markedImg.addXObject(pdImage);
structureElement.appendKid(markedImg);
Run Code Online (Sandbox Code Playgroud)

  • 指向可访问的 PDF 框示例的链接是 404,您能帮助我们找到您将其移动到的位置吗?具体来说,我有兴趣插入一些标记为忽略的文本(可访问性标准似乎是忽略页码)谢谢! (3认同)