0

I am trying to merge PDF files that contain form data. I have tried several different iTextSharp examples found here on StackOverflow, but they all result in the same behavior: the first PDF document that is merged maintains its form data, but the subsequent PDF documents lose their form data. I also tried flattening the documents before merging by using the code below, but this just results in a complete loss of all form data.

public static byte[] FlattenPdfForm(byte[] bytes)
{
    PdfReader reader = new PdfReader(bytes);

    using (MemoryStream stream = new MemoryStream())
    {
        PdfStamper stamper = new PdfStamper(reader, stream) { FormFlattening = true };
        stamper.Close();
        reader.Close();
        return stream.ToArray();
    }
}

The PDF documents I am trying to merge are returned from the UPS API. Here is an example document: http://dl.dropbox.com/u/9005746/OriginalPDF.pdf

Do you have any recommendations on how I can merge multiple PDF files like the one above into a single PDF while maintaining the form data?

Jason
  • 1
  • 2
  • Tried merging the test document you linked above two different ways: [1] Flattening like you attempted, and [2] renaming the individual form fields. Came up with the same results as you. Do you have a PDF validator? iText[Sharp] doesn't deal with *border* PDFs well - PDFs that don't comply with the PDF specification. Your test PDF is the first one I've ever encountered that I *couldn't* merge with iTextSharp. – kuujinbo Feb 14 '12 at 16:27
  • Possible duplicate of [IText merge documents with acrofields](http://stackoverflow.com/questions/28566190/itext-merge-documents-with-acrofields) (if you want to merge forms and keep the interactivity) and [iText 5.5 fails to fill form](http://stackoverflow.com/questions/24183403/itext-5-5-fails-to-fill-form) if you wonder why the content of the fields gets lost after flattening. – Bruno Lowagie Jan 28 '16 at 16:07

2 Answers2

0

This code works well for me:

PdfCopyFields copier = new PdfCopyFields(new FileStream(tgtfilename, FileMode.Create));
int doc = 0;

foreach (string filename in srcfilenames)
{
    PdfReader reader = new PdfReader(filename);

    // This should ensure field names are unique across merged documents
    foreach (var item in reader.AcroFields.Fields)
        reader.AcroFields.RenameField(item.Key, String.Format("_D{0}_{1}", doc++, item.Key));

    copier.AddDocument(reader);
}

copier.Close();

I adapted it from this answer.

Community
  • 1
  • 1
PahJoker
  • 159
  • 2
  • 4
  • 13
0

slight change to the above code works for me

public static void CreateBulkPdfFile(string[] fileNames, string outFile)
{
  PdfCopyFields copier = new PdfCopyFields(new FileStream(outFile, FileMode.Create));
  int doc = 0;

  foreach (string filename in fileNames)
  {          
    PdfReader reader = new PdfReader(filename);

    // This should ensure field names are unique across merged documents
    foreach (var item in reader.AcroFields.Fields)
      reader.AcroFields.RenameField(item.ToString(), String.Format("_D{0}_{1}", doc++, item.ToString()));

    copier.AddDocument(reader);         
  }

  copier.Close();
}
thanksd
  • 54,176
  • 22
  • 157
  • 150
kola
  • 1