itextsharp setting the stamper FormFlatttening=true results in no output

杀马特。学长 韩版系。学妹 提交于 2019-12-24 21:40:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!