Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AcroForm values missing after flattening

I'm using LibreOffice 4.1.3.2 to produce a fillable PDF:

  1. Created a Writer document
  2. Set some text and test fields
  3. Exported to PDF

Opening pdf file with Acrobar Reader shows a correct fillable pdf.
Next I use iTextSharp 5.4.5 to fill fields and save flattened document:

var pdf = new PdfReader(srcFilename);
using (var fw = new FileStream(dstFilename, FileMode.Create))
{
    var stamper = new PdfStamper(pdf, fw);
    var f = stamper.AcroFields;

    f.SetField("field1", "John Doe");
    f.SetField("field2", "12/04/2013");
    stamper.FormFlattening = true;
    stamper.Close();
}
pdf.Close();

Problem is that filled fields values completely disappear in new document!
I thought fields were not found or filled, but discovered that commenting stamper.FormFlattening = true field values are there in saved pdf!!
Naturally I need a flattened pdf...

Is there a solution for this?

like image 451
Marco Avatar asked Dec 06 '22 04:12

Marco


1 Answers

When creating a form using Open Office, Open Office sets a flag telling iText not to create appearances. If you look at the FillDataSheet example, you'll see that we override this with the following line:

fields.setGenerateAppearances(true);

In your specific C# snippet, that would be:

f.GenerateAppearances = true;

It's important to set this value before setting the fields or the appearances won't be created.

like image 52
Bruno Lowagie Avatar answered Dec 10 '22 11:12

Bruno Lowagie