Java PdfCopy中的iText合并字段创建无效pdf
我正在使用iText 5.4.5合并一些输入PDF文档。输入文档可能包含也可能不包含AcroForms,我也想合并这些表单 我正在使用找到的示例pdf文件,以下是代码示例:Java PdfCopy中的iText合并字段创建无效pdf,java,pdf,itext,acrofields,Java,Pdf,Itext,Acrofields,我正在使用iText 5.4.5合并一些输入PDF文档。输入文档可能包含也可能不包含AcroForms,我也想合并这些表单 我正在使用找到的示例pdf文件,以下是代码示例: public class TestForms { @Test public void testNoForms() throws DocumentException, IOException { test("pdf/hello.pdf", "pdf/hello_memory.pdf"); } @Test
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
,对吗?那个标志上没有javadoc,我也找不到关于它的文档mergeFields
- 假设上一个问题的答案是肯定的,我的代码有什么问题吗? 谢谢
PdfCopy.setMergeFields()
是正确的,您的合并代码也很好
您所描述的问题是由于5.4.5中出现的bug造成的。它们应该固定在rev中。6152和修复程序将包含在下一版本中
感谢您提醒我们这一点。我们正在使用PdfCopy合并不同的文件,有些文件可能有字段。我们使用5.5.3.0版。代码很简单,似乎工作正常,但有时结果文件无法打印强> 我们的守则:
Public Shared Function MergeFiles(ByVal sourceFiles As List(Of Byte())) As Byte()
Dim document As New Document()
Dim output As New MemoryStream()
Dim copy As iTextSharp.text.pdf.PdfCopy = Nothing
Dim readers As New List(Of iTextSharp.text.pdf.PdfReader)
Try
copy = New iTextSharp.text.pdf.PdfCopy(document, output)
copy.SetMergeFields()
document.Open()
For fileCounter As Integer = 0 To sourceFiles.Count - 1
Dim reader As New PdfReader(sourceFiles(fileCounter))
reader.MakeRemoteNamedDestinationsLocal()
readers.Add(reader)
copy.AddDocument(reader)
Next
Catch exception As Exception
Throw exception
Finally
If copy IsNot Nothing Then copy.Close()
document.Close()
For Each reader As PdfReader In readers
reader.Close()
Next
End Try
Return output.GetBuffer()
End Function
这只是说我们有同样的问题:PdfCopy中的iText mergeFields会创建无效的pdf。因此,在版本5.5.3.0中它仍然没有被修复,可能是(12月13日修复的错误)的一个副本。实际上,我刚刚使用5.4.6-SNAPSHOT进行了测试,其中包括您提到的修复,我得到了相同的结果。这是@rhens的问题,这是一个全新的问题。因此,请让它成为一个独立的问题。