ITextSharp PDFTemplate FormFlattening deletes filled data

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.

+4
source share
3 answers

The problem using the previous version of ITextSharp (5.4.3) has been resolved. Not sure why, though ...

+3
source

Most likely, you will be able to solve this problem using iTextSharp 5.4.4 (or later) by causing iTextSharp to generate expressions for the form fields. In your code example:

 var form = stamper.AcroFields; form.GenerateAppearances = true; 
+17
source

I found for this a working solution for any new iTextSharp. How we do it: 1- Create a copy of the pdf-template. 2- fill in a copy with data. 3- FormFlatten = true and setFullCompression 4- Combining some PDF files into a new document. 5- Move the new combination document, and then delete the tempo.

So we got a problem with remote input, and if we skipped "formflatten", it looked ok.

However, when we moved “FormFlatten = true” from step 3 and added it as a separate step after the completion of the move, etc., it worked fine.

Hope I explained a little :)

0
source

All Articles