创建PDF / A-3:嵌入式文件应包含有效的Params密钥 [英] Creating PDF/A-3: Embedded file shall contain valid Params key

查看:108
本文介绍了创建PDF / A-3:嵌入式文件应包含有效的Params密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用itextpdf-5.4.5和itext-pdfa-5.4.5创建一个PDF / A-3。
当我设置PdfFileSpecification时,我得到以下例外:

im trying to create a PDF/A-3 with itextpdf-5.4.5 and itext-pdfa-5.4.5. When i set the PdfFileSpecification, i get the following Exception:

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

这是我创建PdfDictionary的部分:

This is the part where i create the 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);

我找到了检查发生的方法,但我找不到任何解决方案:

I found the method where the check happens, but i dont find any solution:

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

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

Any idea? Thank you in advance!

推荐答案

一般



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

In general

PDF/A-3 has some special requirements concerning embedded files, among them


嵌入文件的流字典应包含 Params 键,其值应为至少包含 ModDate 键的字典,其值应为源文件的最新修改日期。

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.

(ISO 19000-3附件E.1)

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

iText support explains on PDF/A-3 with iText how to create PDF/A-3 compliant PDFs from scratch, and they also demonstrate there how to embed files in a PDF/A-3 compliant manner, their sample code:

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 )添加到目录这是另一项要求。

This example also adds the associated file entry (AF) to the Catalog which is another requirement.

BTW,严格来说 Params 字典仅在上需要 base,而不是 。因此,这实际上是一个推荐,并且可以存在带有附件的有效PDF / A-3文档,这些文档没有此条目。

BTW, strictly speaking the Params dictionary is only required on a should base, not shall. Thus, this is actually a recommendation and there can be valid PDF/A-3 documents with attachments in the wild which do not have this entry.

因为没有明显的为什么iText最好不要在创建PDF / A-3文件时不遵循这个建议,但是,它的检查的严格解释是可以的。

As there is no apparent reason why iText would be better off not following this recommendation when creating PDF/A-3 files, though, the strict interpretation by its checks is ok.

最近推出了OP发现的检查。在将文件存储在PDF文件中时,已经触发了此检查。不幸的是,此时尚未分配params字典,仅保留了引用。因此,检查失败,即使在编写字典后不久。

The check the OP found has been introduced recently. This check already is triggered while storing the file in the PDF file. Unfortunately at this time the params dictionary has not yet been assigned, only a reference has been reserved. Thus, the check fails, even though shortly after the dictionary would have been written.

来自 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();

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

During this operation the check happens (and fails).

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

此后,直接写入params。

And here, directly thereafter, the params would have been written.

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

PdfFileSpecification.fileEmbedded allows you to present the data as a byte[] instead of as a file in the file system. As you can see in the source above the processing differs in that case: fileStore contains that byte[] argument. If you follow up on the fileStore use in there, you'll see that for a non-null value of it the params dictionary is written as a direct object and, therefore, present in the test.

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

Thus, you can use iText 5.4.5 for PDF/A-3 file attachments if you supply the files as byte[] instances instead.

这篇关于创建PDF / A-3:嵌入式文件应包含有效的Params密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆