4

In version 5.4.2 of itextsharp I was able to use: (fragment in VB)

Dim pdfWriter As iTextSharp.text.pdf.PdfCopy 
pdfwriter = New iTextSharp.text.pdf.PdfCopy(outputPDFDocument, New FileStream(destfname, FileMode.Create))
pdfWriter.CopyAcroForm(reader) 

to copy a form from one document to another.

In 5.4.4 CopyAcroForm is no longer there under PdfCopy or anywhere else - what is the alternative?

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
user3024825
  • 61
  • 1
  • 5
  • According to this thread (http://itextsharp.10939.n7.nabble.com/RE-iText-questions-PdfCopy-copyAcroForm-wrong-method-getReaderFile-tt18.html), `CopyAcroForm` was considered obsolete back in 2005 already. I think the reason that it was obsolete was that it didn't do what everyone expected it to do every time. Instead, read Mark's post here that explains a couple of alternatives. http://stackoverflow.com/a/6333751/231316 – Chris Haas Nov 23 '13 at 16:13

3 Answers3

15

Please read the release notes for iText 5.4.4. It is now possible to use PdfCopy to merge PDFs containing AcroForm forms by using the addDocument() method. This method is much better than the copyAcroForm() method as it also preserves the structured tree root. This is important if your forms are made accessible (cf Section 508 or the PDF/UA standard).

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • +1; reading the release notes can be quite an advantage during third-party library updates... – mkl Nov 24 '13 at 19:42
2

AddDocument() method is cool. Here is my code that read and merge multiple PDFs from SQL server in asp.net . document.Close() is required to flush the content to memory stream.

enter code here
    Document document = new Document();
    MemoryStream output = new MemoryStream();
    PdfCopy writer = new PdfCopy(document, output); // Initialize pdf writer    
    writer.SetMergeFields(); 
    document.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
         PdfReader reader = new PdfReader((Byte[])dr["ImageFile"]);
         writer.AddDocument(reader);
    }
    dr.Close();
    document.Close();
David J
  • 41
  • 4
-1

It looks like you also need to call .SetMergeFields() or it won't work:

reader = new PdfReader(path);
using (var document = new Document(reader.GetPageSizeWithRotation(1))) {
    using (var outputStream = new FileStream(...)) {
        using (var writer = new PdfCopy(document, outputStream)) {
            writer.SetMergeFields();
            document.Open();

            //all pages:
            writer.AddDocument(reader);
            //Particular Pages:
            //writer.AddDocument(reader, new List<int> { pageNumber });
        }
    }
}
colinbashbash
  • 996
  • 2
  • 9
  • 19