We have a C# application that must print complex forms. Things like multi-page government compliance forms that must be in a specific format. We can get PDF copies of these fo
If your form is based on the AcroForm technology: Just use the itext7 to accomplish this task. Add it to your project by executing following command in your NuGet Package Manager Console:
Install-Package itext7
To write a specific form field, use code similar to this:
PdfReader reader = new PdfReader(src);
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(reader, writer);
var form = PdfAcroForm.GetAcroForm(pdfDoc, true);
var fields = form.GetFormFields();
fields.Get(key).SetValue(value);
form.FlattenFields();
pdfDoc.Close();
In this snippet, src
is the source of a PDF file and dest
is the path to the resulting PDF. The key
corresponds with the name of a field in your template. The value
corresponds with the value you want to fill in. If you want the form to keep its interactivity, you need to remove the form.flattenFields();
otherwise all form fields will be removed, resulting in a flat PDF.
Caution
Be aware, that itext7 is licenced under AGPL and isn't free for commercial use or closed source. (special thanks to @da_berni for this necessary information)