I am migrating an existing application from Java to C #. The original application used the IText library to populate PDF form templates and save them as new PDF files. My C # code (example) is below:
string templateFilename = @"C:\Templates\test.pdf"; string outputFilename = @"C:\Output\demo.pdf"; using (var existingFileStream = new FileStream(templateFilename, FileMode.Open)) { using (var newFileStream = new FileStream(outputFilename, FileMode.Create)) { var pdfReader = new PdfReader(existingFileStream); var stamper = new PdfStamper(pdfReader, newFileStream); var form = stamper.AcroFields; var fieldKeys = form.Fields.Keys; foreach (string fieldKey in fieldKeys) { form.SetField(fieldKey, "REPLACED!"); } stamper.FormFlattening = true; stamper.Close(); pdfReader.Close(); } }
Everything works well only if I lower
stamper.FormFlattening = true;
but the forms are visible as ... forms. When I add this line, any values set for the form fields are lost, which leads to an empty form. I would really appreciate any advice.
source share