java - 使用 PDFBox 标记的 PDF

标签 java accessibility pdfbox tagged-pdf

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

使用下面的代码,我生成了一个包含图像的 PDF 文件,屏幕阅读器 NVDA(在我的例子中)识别它并读取“...图形替代描述”。但是,可访问性检查器 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);

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

最佳答案

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

问题中的代码缺少一些东西。标记的内容需要替代文本,我相信您需要为该标记的内容添加 mcids。

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

应该是这样的:

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);

关于java - 使用 PDFBox 标记的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26552459/

相关文章:

java - 自定义对象上的数组为空

c++ - 包含 QMdiSubWindows 的 QMdiArea 对可访问性 API 不可见

javascript - 禁用 axe.min.js 的颜色对比

java - 用于编写 DSL 编译器的工具

java - 在运行 SonarQube 和扫描仪时,我得到 "insufficient memory for JRE to continue"

java - 未知故障(在 android.os.Binder.execTransact(Binder.java :674)) Error while Installing APKs

accessibility - 关闭CSS以进行以可访问性为中心的E2E测试?

java - PDFBox:将pdf页面转换为图像的问题

java - 将 PDFBox 从版本 2.08 更新到 2.12/2.16 后,使用 PDPageContentStream.drawImage 绘制一些 16 位透明图像时出现问题

java - PDFBox 为什么图像没有出现在 PDF 输出中?