gpt4 book ai didi

pdf - 创建 PDF/A-3 : Embedded file shall contain valid Params key

转载 作者:行者123 更新时间:2023-12-04 17:22:07 26 4
gpt4 key购买 nike

我正在尝试使用 itextpdf-5.4.5 和 itext-pdfa-5.4.5 创建 PDF/A-3。当我设置 PdfFileSpecification 时,出现以下异常:

com.itextpdf.text.pdf.PdfAConformanceException: Embedded file shall contain valid Params key.

这是我创建 PdfDictionary 的部分:

PdfDictionary params = new PdfDictionary();
params.put(PdfName.MODDATE, new PdfDate());
PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(
writer, "./src/main/resources/com/itextpdf/invoice.xml", "invoice.xml", null, false, "text/xml", params);

我找到了进行检查的方法,但我没有找到任何解决方案:

com.itextpdf.text.pdf.internal.PdfA3Checker.checkEmbeddedFile

protected void checkEmbeddedFile(PdfDictionary embeddedFile) {
PdfDictionary params = getDirectDictionary(embeddedFile.get(PdfName.PARAMS));
if (params == null) {
throw new PdfAConformanceException(embeddedFile, MessageLocalization.getComposedMessage("embedded.file.shall.contain.valid.params.key"));
}

有什么想法吗?提前致谢!

最佳答案

一般情况

PDF/A-3对嵌入文件有一些特殊要求,其中

An embedded file's stream dictionary should contain a Params key whose value shall be a dictionary containing at least a ModDate key whose value shall be the latest modification date of the source file.

(Annex E.1 of ISO 19000-3)

iText 支持说明 PDF/A-3 with iText如何从头开始创建符合 PDF/A-3 标准的 PDF,他们还演示了如何以符合 PDF/A-3 标准的方式嵌入文件,他们的示例代码:

PdfDictionary parameters = new PdfDictionary();
parameters.put(PdfName.MODDATE, new PdfDate());
PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(
writer, "./src/main/resources/com/itextpdf/invoice.xml",
"invoice.xml", null, "application/xml", parameters, 0);
fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data"));
writer.addFileAttachment("invoice.xml", fileSpec);
PdfArray array = new PdfArray();
array.add(fileSpec.getReference());
writer.getExtraCatalog().put(new PdfName("AF"), array);

此示例还将关联的文件条目 (AF) 添加到目录中,这是另一个要求。

顺便说一句,严格来说 Params 字典仅在应该 基础上是必需的,而不是应该。因此,这实际上是一个建议,可能存在没有此条目的带附件的有效 PDF/A-3 文档。

虽然没有明显的理由说明 iText 在创建 PDF/A-3 文件时不遵循此建议会更好,但其检查的严格解释是可以的。

iText 5.4.5 中的问题

最近介绍了 OP 发现的检查。在将文件存储在 PDF 文件中时已触发此检查。可惜此时params字典还没有赋值,只保留了一个引用。因此,检查失败,即使在字典被写入后不久也是如此。

来自 PdfFileSpecification.java:

        stream.put(PdfName.TYPE, PdfName.EMBEDDEDFILE);
stream.flateCompress(compressionLevel);
PdfDictionary param = new PdfDictionary();
if (fileParameter != null) {
param.merge(fileParameter);
}
if (!param.contains(PdfName.MODDATE)) {
param.put(PdfName.MODDATE, new PdfDate());
}
if (fileStore != null) {
param.put(PdfName.SIZE, new PdfNumber(stream.getRawLength()));
stream.put(PdfName.PARAMS, param);
}
else
stream.put(PdfName.PARAMS, refFileLength);

if (mimeType != null)
stream.put(PdfName.SUBTYPE, new PdfName(mimeType));

ref = writer.addToBody(stream).getIndirectReference();

在此操作期间,检查发生(并失败)。

        if (fileStore == null) {
stream.writeLength();
param.put(PdfName.SIZE, new PdfNumber(stream.getRawLength()));
writer.addToBody(param, refFileLength);
}

在这里,紧随其后,将写入参数。

解决方法

PdfFileSpecification.fileEmbedded 允许您将数据显示为 byte[] 而不是文件系统中的文件。正如您在上面的源代码中看到的那样,处理在这种情况下有所不同:fileStore 包含 byte[] 参数。如果您跟进那里的 fileStore 使用,您会看到对于它的非 null 值,params 字典被写为直接对象,因此, 出现在测试中。

因此,如果您将文件作为 byte[] 实例提供,则可以将 iText 5.4.5 用于 PDF/A-3 文件附件。

关于pdf - 创建 PDF/A-3 : Embedded file shall contain valid Params key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21018428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com