关于java:PDF/A代字体编码的问题 | 珊瑚贝

Problem about font encoding in PDF/A generation


所以这是我的问题:
我目前正在开发一个将文档归档为 PDF/A-1 的 java 应用程序。我正在使用 PdfBox 生成 pdf,但由于字体的原因,我无法生成有效的 PDF/A-1 pdf。字体嵌入在 pdf 文件中,但该网站:https://www.pdf-online.com/osa/validate.aspx 告诉我这不是有效的 PDF/A,因为:

The key Encoding has a value Identity-H which is prohibited.

我在互联网上查看了这个 Identity-H 编码是什么,它似乎是字体的编码方式,就像 ansi 编码一样。

我已经尝试过使用不同的字体,如 Helvetica 或 arial unicode Ms 但没有任何效果,总是有这种 Identity-H 编码。我对编码中的所有这些混乱感到有点迷茫,所以如果有人可以解释一下会很棒的。这也是我编写的在 pdf 中嵌入字体的代码:

1
2
3
4
5
6
7
8
 // load the font as this needs to be embedded
PDFont font = PDType0Font.load(doc, getClass().getClassLoader().getResourceAsStream(fontfile), true);

if (!font.isEmbedded())
        {
            throw new IllegalStateException(“PDF/A compliance requires that all fonts used for”
                    +” text rendering in rendering modes other than rendering mode 3 are embedded.”);
        }

感谢您的帮助:)

  • 这很奇怪 – 使用 CreatePDFA 示例生成的 PDF 在那里验证。
  • 与问题无关:不要使用 “getClassLoader()”,这会给某些 java 版本带来问题。问题.apache.org/jira/browse/PDFBOX-4428
  • 您能否 1) 自己尝试 CreatePDFA 示例,2) 共享未验证的文件?
  • @TilmanHausherr 我使用了 Apache 的示例,它可以工作!我不知道为什么,因为文件和代码是但是,如果它工作。我关于 PDF/A 生成的最后一个问题是处理嵌入式文件。我尝试制作 PDF/A3-a,但在验证它时出现了最后一个错误:”需要密钥 AFRelationship,但缺少”。我在网上查了一下,但没有人说如何在 pdfbox 中设置它。
  • 使用 dictionary.setItem(COSName.getPDFName(“AFRelationship”), COSName.getPDFName(“Supplement”)) 或任何正确的值。 (可以是 Source、Data、Alternative、Supplement、EncryptedPayload、FormData、Schema 或 Unspecified)
  • @TilmanHausherr 字典的对象类型是什么?
  • 一个COSDictionary。您通常通过在元素上调用 getCOSObject() 来获得它。 (该文件附件结构中的一些元素。最好是使用 PDFDebugger 打开现有的 PDF/A-3 文件以查看它是哪一个)
  • @TilmanHausherr 非常感谢它的工作!我有最后一个问题。我没有看到任何人在谈论它,所以如果你能帮助我,那就太好了!尝试验证 PDF/A 时出现此错误:”文件规范 \\’Test.xlsx\\’ 未与对象关联。”。我使用 PDF/A 的 3-a 版本来允许嵌入文件我没有看到任何解决方案来修复它。谢谢您帮忙 :)
  • 我没有直接的答案,但请参见此处:mail-archives.apache.org/mod_mbox/pdfbox-users/201312.mbox/… 和 github.com/veraPDF/veraPDF-validation-profiles/wiki/… ” PDF/A-3 文档中的每个文件附件都必须从 PDF 文档中的以下对象之一引用”如果这没有帮助我建议您创建一个新问题。
  • 回复原来的问题,我建议你自己回答。我仍然想知道您的 PDF 和示例 PDF 之间的区别是什么。


问题已解决:

我使用了 apache 的示例:CreatePDFA(我不知道为什么它会起作用,而不是我的代码):examples/src/main/java/org/apache/pdfbox/examples 中的示例

我添加以符合 PDF/A-3 要求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
doc.getDocumentCatalog().setLanguage(“en-US”);

PDMarkInfo mark = new PDMarkInfo(); // new PDMarkInfo(page.getCOSObject());
PDStructureTreeRoot treeRoot = new PDStructureTreeRoot();
doc.getDocumentCatalog().setMarkInfo(mark);
doc.getDocumentCatalog().setStructureTreeRoot(treeRoot);
doc.getDocumentCatalog().getMarkInfo().setMarked(true);

PDDocumentInformation info = doc.getDocumentInformation();
info.setCreationDate(date);
info.setModificationDate(date);
info.setAuthor(“KairosPDF”);
info.setProducer(“KairosPDF”);
info.setCreator(“KairosPDF”);
info.setTitle(“Generated PDf”);
info.setSubject(“PDF/A3-A”);

这是我将文件嵌入到 pdf 的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private final PDDocument doc = new PDDocument();
private final PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
private final PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
private final Map<String, PDComplexFileSpecification> efMap = new HashMap<>();

public void addFile(PDDocument doc, File child) throws IOException {
    File file = new File(child.getPath());

    Calendar date = Calendar.getInstance();

    //first create the file specification, which holds the embedded file
    PDComplexFileSpecification fs = new PDComplexFileSpecification();
    fs.setFileUnicode(child.getName());
    fs.setFile(child.getName());
    InputStream is = new FileInputStream(file);
    PDEmbeddedFile ef = new PDEmbeddedFile(doc, is);

    //Setting
    ef.setSubtype(“application/octet-stream”);
    ef.setSize((int) file.length() + 1);
    ef.setCreationDate(date);
    ef.setModDate(date);
    COSDictionary dictionary = fs.getCOSObject();
    dictionary.setItem(COSName.getPDFName(“AFRelationship”), COSName.getPDFName(“Data”));

    fs.setEmbeddedFile(ef);

    efMap.put(child.getName(), fs);
    efTree.setNames(efMap);

    names.setEmbeddedFiles(efTree);
    doc.getDocumentCatalog().setNames(names);
    is.close();
}

剩下的唯一问题是验证中的这个错误:

File specification ‘Test.txt’ not associated with an object.

希望对大家有所帮助。


来源:https://www.codenong.com/57159431/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?