17

I'm using iTextSharp to merge a number of pdf files together into a single file.

I'm using method described in iTextSharp official tutorials, specifically here, which merges files page by page via PdfWriter and PdfImportedPage.

Turns out some of the files I need to merge are filled out PDF Forms and using this method of merging form data is lost.

I've see several examples of using PdfStamper to fill out forms and flatten them.

What I can't find, is a way to flatten already filled out PDF Form and hopefully merge it with the other files without saving it flattened out version first.

Thanks

andryuha
  • 1,526
  • 3
  • 15
  • 31
  • woof... you may have a problem there... – Jason Dec 21 '09 at 20:27
  • this may help someone else, as I was having an issue where free text wasn't being flattened, in which iTextSharp has a separate setting for this: `stamper.FreeTextFlattening = true;` – jtate Aug 19 '16 at 17:44

4 Answers4

14

Just setting .FormFlattening on PdfStamper wasn't quite enough...I ended up using a PdfReader with byte array of file contents that i used to stamp/flatten the data to get the byte array of that to put in a new PdfReader. Below is how i did it. works great now.

 private void AppendPdfFile(FileDTO file, PdfContentByte cb, iTextSharp.text.Document printDocument, PdfWriter iwriter) 
  {
     var reader = new PdfReader(file.FileContents);

     if (reader.AcroForm != null)
        reader = new PdfReader(FlattenPdfFormToBytes(reader,file.FileID));

     AppendFilePages(reader, printDocument, iwriter, cb);
  }

  private byte[] FlattenPdfFormToBytes(PdfReader reader, Guid fileID)
  {
     var memStream = new MemoryStream();
     var stamper = new PdfStamper(reader, memStream) {FormFlattening = true};
     stamper.Close();
     return memStream.ToArray();
  }
andryuha
  • 1,526
  • 3
  • 15
  • 31
10

When creating the files to be merged, I changed this setting: pdfStamper.FormFlattening = true;

Works Great.

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
7

I think this problem is same with this one: AcroForm values missing after flattening

Based on the answer, this should do the trick:

pdfStamper.FormFlattening = true;
pdfStamper.AcroFields.GenerateAppearances = true;
Moch Yusup
  • 1,266
  • 14
  • 14
  • This simple change of adding `pdfStamper.AcroFields.GenerateAppearances = true;` got mine working as well. Much easier than the other byte array solution. – FirstDivision Oct 12 '17 at 21:50
2

This is the same answer as the accepted one but without any unused variables:

private byte[] GetUnEditablePdf(byte[] fileContents)
{
    byte[] newFileContents = null;

    var reader = new PdfReader(fileContents);

    if (reader.AcroForm != null)
        newFileContents = FlattenPdfFormToBytes(reader);

    else newFileContents = fileContents;

    return newFileContents;
}

private byte[] FlattenPdfFormToBytes(PdfReader reader)
{
    var memStream = new MemoryStream();
    var stamper = new PdfStamper(reader, memStream) { FormFlattening = true };
    stamper.Close();
    return memStream.ToArray();
}
Gauravsa
  • 6,330
  • 2
  • 21
  • 30
  • is it safe to flatten all pdfs like this, or is there a way to detect which pdfs requires this and which ones does not require this? – Gerrie Pretorius Mar 26 '21 at 09:58