iText mergeFields in PdfCopy creates invalid pdf

1.7k Views Asked by At

I am working on the task of merging some input PDF documents using iText 5.4.5. The input documents may or may not contain AcroForms and I want to merge the forms as well.

I am using the example pdf files found here and this is the code example:

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();
 }
}
  • With input files containing forms I get a NullPointerException with or without compression enabled.
  • With standard input docs, the output file is created but when I open it with Acrobat it says there was a problem (14) and no content is displayed.
  • With standard input docs AND compression disabled the output is created and Acrobat displays it.
Questions
  • I previously did this using PdfCopyFields but it's now deprecated in favor of the boolean flag mergeFields in the PdfCopy, is this correct? There's no javadoc on that flag and I couldn't find documentation about it.
  • Assuming the answer to the previous question is Yes, is there anything wrong with my code? Thanks
3

There are 3 best solutions below

0
On BEST ANSWER

Your usage of PdfCopy.setMergeFields() is correct and your merging code is fine.

The issues you described are because of bugs that have crept into 5.4.5. They should be fixed in rev. 6152 and the fixes will be included in the next release.

Thanks for bringing this to our attention.

1
On

We are using PdfCopy to merge differents files, some of files may have fields. We use the version 5.5.3.0. The code is simple and it seems to work fine, BUT sometimes the result file is impossible to print! Our code :

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
0
On

Its just to say that we have the same probleme : iText mergeFields in PdfCopy creates invalid pdf. So it is still not fixed in the version 5.5.3.0