0

Using itextsharp v5.5.5.0 in VS2010 Setting the stamper FormFlattening = true no filed data is written to the output pdf. If set false the data is all present & correct but still editable (which I don't want)

PdfReader pdfTemplate = new PdfReader("..\\..\\pdf\\BFC-Template.pdf");
FileStream fileOutputStream = new FileStream("..\\..\\pdf\\BFC.pdf", FileMode.Create);
PdfStamper stamper = new PdfStamper(pdfTemplate, fileOutputStream);

stamper.AcroFields.SetField("FitID", "1234");
stamper.AcroFields.SetField("FitBy", "Fred Flintstone");
stamper.AcroFields.SetField("FitDate", "03/11/2015");
stamper.AcroFields.SetField("FitLocation", "Bedrock");

stamper.FormFlattening = true;
stamper.Close();
pdfTemplate.Close();
fileOutputStream.Close();
JayC
  • 49
  • 2

2 Answers2

2

Try adding :

stamper.AcroFields.GenerateAppearances = true;

EDIT:

If your form is a Dynamic Form. you might need to change

stamper.AcroFields.SetField("FitID", "1234");

to:

stamper.AcroFields.Xfa.DatasetsSom.Name2Node["FitID"].InnerText = "1234"
bog500
  • 41
  • 3
  • But this shouldn't be required. I've just tested your code with itextsharp v5.5.5.0 in VS2013 and it works fine. However, I've noticed that itextsharp doesn't always behave the same way depending on the PDF file you are trying to fill. Try filling this [file](http://help.adobe.com/en_US/Acrobat/9.0/Samples/interactiveform_enabled.pdf) with your code to see if it makes a difference. – bog500 Mar 06 '15 at 22:01
0

It shouldn't matter, but you could try instantiating a AcroFields object from the pdfStamper field.

PdfStamper stamper = new PdfStamper(pdfTemplate, fileOutputStream);
AcroFields pdfFields = pdfStamper.AcroFields;

Then you can just set each field using pdfFields:

pdfFields.SetField("FitID", "1234");
pdfFields.SetField("FitBy", "Fred Flintstone");
pdfFields.SetField("FitDate", "03/11/2015");
pdfFields.SetField("FitLocation", "Bedrock");
stamper.FormFlattening = true;
stamper.Close();

I have this exact setup and it works for me.

Ryan C
  • 572
  • 5
  • 18