【发布时间】:2014-02-07 00:35:11
【问题描述】:
我正在处理使用 iText 5.4.5 合并一些输入 PDF 文档的任务。输入文档可能包含也可能不包含 AcroForms,我也想合并表单。
我正在使用here 找到的示例 pdf 文件,这是代码示例:
public class TestForms {
@Test
public void testNoForms() throws DocumentException, IOException {
test("pdf/hello.pdf", "pdf/hello_memory.pdf");
}
@Test
public void testForms() throws DocumentException, IOException {
test("pdf/subscribe.pdf", "pdf/filled_form_1.pdf");
}
private void test(String first, String second) throws DocumentException, IOException {
OutputStream out = new FileOutputStream("/tmp/out.pdf");
InputStream stream = getClass().getClassLoader().getResourceAsStream(first);
PdfReader reader = new PdfReader(new RandomAccessFileOrArray(
new RandomAccessSourceFactory().createSource(stream)), null);
InputStream stream2 = getClass().getClassLoader().getResourceAsStream(second);
PdfReader reader2 = new PdfReader(new RandomAccessFileOrArray(
new RandomAccessSourceFactory().createSource(stream2)), null);
Document pdfDocument = new Document(reader.getPageSizeWithRotation(1));
PdfCopy pdfCopy = new PdfCopy(pdfDocument, out);
pdfCopy.setFullCompression();
pdfCopy.setCompressionLevel(PdfStream.BEST_COMPRESSION);
pdfCopy.setMergeFields();
pdfDocument.open();
pdfCopy.addDocument(reader);
pdfCopy.addDocument(reader2);
pdfCopy.close();
reader.close();
reader2.close();
}
}
- 对于包含表单的输入文件,我会得到一个
NullPointerException,无论是否启用压缩。 - 使用标准输入文档会创建输出文件,但是当我使用 Acrobat 打开它时,它说存在问题 (14),并且没有显示任何内容。
- 在禁用标准输入文档和压缩的情况下,将创建输出并由 Acrobat 显示。
- 我以前使用
PdfCopyFields执行此操作,但现在不推荐使用PdfCopy中的布尔标志mergeFields,这是正确的吗?该标志上没有 javadoc,我找不到有关它的文档。 - 假设上一个问题的答案是肯定的,我的代码有什么问题吗? 谢谢
【问题讨论】:
-
可能是 stackoverflow.com/questions/19839445/… 的副本(12 月 13 日修复的错误)。
-
实际上我刚刚使用 5.4.6-SNAPSHOT 进行了测试,其中包含您提到的修复程序,我得到了相同的结果。
-
这是@rhens stackoverflow.com/questions/20656540/…的问题
标签: java pdf itext acrofields