【发布时间】:2018-09-08 06:30:52
【问题描述】:
我使用 iText 5.5.13 编写了一个函数,将文件作为附件嵌入到 PDF/A-3a 文档中(使用来自 iText tutorials 的说明)。
如果我使用 PdfCopy 类附加文件,结果是正确的 PDF 文件,但它并不声称是 PDF/A(也许它符合所有要求,但它没有说明)。
如果我使用 PdfACopy 做同样的事情,我会得到一个错误构建的文档:
InvalidPdfException:重建失败:未找到预告片。;原版的 消息:未找到 PDF startxref。
这是我稍微简化的代码。注释的是使用 PdfCopy 代替的行。
public static File embedFile(File inputPdf) {
File outputPdf = new File("./test.pdf");
PdfReader reader = new PdfReader(inputPdf.getAbsolutePath());
Document document = new com.itextpdf.text.Document();
OutputStream os = new FileOutputStream(outputPdf.getAbsolutePath());
PdfACopy copy = new PdfACopy(document, os, PdfAConformanceLevel.PDF_A_3A); // Output doc doesn't work
// PdfCopy copy = new PdfCopy(document, os); // Output doc works but doesn't claim to be PDF/A
document.open();
copy.addDocument(reader);
// Include attachment (extactly as in the sample tutorial)
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);
os.flush();
reader.close();
document.close();
os.close();
copy.close();
return outputPdf;
}
输入文件已经是 PDF/A-3a 文档,所以我想我不需要重新定义所有必需的东西,比如嵌入字体、输出意图……
在使用 PdfCopy 不需要的 PdfACopy 时,是否可能缺少必需的步骤?
尝试使用 iText 7 会有帮助吗?
非常感谢!
【问题讨论】:
-
您有一个 PDF/A-3a 文档,其 a 为 accessibility。直到 iText 7 并且您使用的是 iText 5,才实现连接可访问的 PDF。
-
不是我想连接,但我认为这是向现有 PDF 添加附件的唯一方法。您知道是否可以在 iText 5 中添加附件而不进行连接?如果没有,我会尝试使用 iText 7
-
它在 iText 7 中非常有用。非常感谢@BrunoLowagie!
标签: java pdf itext pdf-generation pdfa