问题
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();
回答1:
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"
回答2:
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.
来源:https://stackoverflow.com/questions/28908081/itextsharp-setting-the-stamper-formflatttening-true-results-in-no-output