JayC
JayC

Reputation: 49

itextsharp setting the stamper FormFlatttening=true results in no output

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();

Upvotes: 0

Views: 1624

Answers (2)

bog500
bog500

Reputation: 41

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"

Upvotes: 2

Ryan C
Ryan C

Reputation: 582

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.

Upvotes: 0

Related Questions