【问题标题】:iText mergeFields in PdfCopy creates invalid pdfPdfCopy 中的 iText mergeFields 创建无效的 pdf
【发布时间】: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,我找不到有关它的文档。
  • 假设上一个问题的答案是肯定的,我的代码有什么问题吗? 谢谢

【问题讨论】:

标签: java pdf itext acrofields


【解决方案1】:

您对PdfCopy.setMergeFields() 的使用是正确的,您的合并代码也很好。

您描述的问题是由于 5.4.5 中出现的错误。它们应该以rev固定。 6152 并且修复将包含在下一个版本中。

感谢您提请我们注意。

【讨论】:

    【解决方案2】:

    我们使用 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
    

    【讨论】:

    • 这是一个全新的问题。因此,请将其本身作为一个 stackoverflow 问题。
    【解决方案3】:

    只是说我们有同样的问题:PdfCopy 中的 iText mergeFields 创建无效的 pdf。所以在5.5.3.0

    这个版本中还没有修复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 2012-12-08
      • 2018-09-08
      • 1970-01-01
      相关资源
      最近更新 更多