copy pdf form with PdfCopy not working in itextsharp 5.4.5.0

后端 未结 1 554
执念已碎
执念已碎 2020-12-06 21:17

In the release notes of iText 5.4.4 it says:

From now on you can now merge forms and preserve the tagged PDF structure when using the addDocument()

相关标签:
1条回答
  • 2020-12-06 21:46

    There are several issues here.

    1) You have to enable form field merging for PdfCopy:

    // ...
    var copy = new PdfCopy(document, outputPdfStream);
    copy.SetMergeFields();
    document.Open();
    // ...
    

    This works for iText 5.4.5 (Java), but for iTextSharp Reader/Acrobat complain about an embedded font when displaying page 2 of the merged document. This is probably a porting issue.

    2) EV_Original.pdf doesn't have appearances ("visualizations") for the form fields. Instead it has the NeedAppearances flag set. This indicates the PDF viewer needs to generate appearances when displaying the document.

    PdfCopy doesn't process NeedAppearances correctly at the moment, so it isn't set in the output document. This needs to be fixed in iText. As a workaround, you could set NeedAppearances on your output document after merging:

    PdfReader postreader = new PdfReader("combined4.pdf");
    PdfStamper poststamper = new PdfStamper(postreader, new FileStream("combined4-needappearances.pdf", FileMode.Create));
    poststamper.AcroFields.GenerateAppearances = true;
    poststamper.Close();
    

    But taking into account the porting bug in iTextSharp 5.4.5, I'd suggest to use PdfCopyFields until PdfCopy is fixed in the next release. Memory usage for PdfCopyFields and PdfCopy are similar when merging Acroforms. This is inherent to Acroform merging: more information need to be kept in memory. That's why it has to be explicitly enabled in PdfCopy using SetMergeFields().

    0 讨论(0)
提交回复
热议问题