Working With PDF AcroForms | C# | itext7

Aksh Suri
3 min readApr 29, 2021

What are AcroForms?

“AcroForm” usually refers to the PDF form that is authored with Adobe Acrobat Pro/Standard and that is not a special type of static or dynamic XFA form authored with Adobe LiveCycle Designer.

AcroForms are PDF files that contain form fields. Data can be entered into these fields (manually or through an automated process) by the end users or the author of the form. Internally AcroForms are annotations or fields applied to a PDF document. AcroForms are easily filled using formatted ASCII or FDF files that contain key:value pairs that define the field names and associated values that are used to populate a form

You can fill out an AcroForm in Acrobat DC mobile products (for iOS and Android) or programmatically using the example illustrated below.

Creating an AcroForm

To create an AcroForm, first you need to install Acrobat DC. After Installing Acrobat DC, you can create a PDF with fields and use “Options” table to select the “Prepare Form” from the list of options available. Create placeholders for text, images, checkboxes, radio buttons etc. and define X-PATH or field names for these placeholders with dynamic content programmatically in the code.

Simple PDF with FormFields

In the attached Screenshot, A simple AcroForm is created with three form fields and one Image filed.

Also the fields have been tagged with a field name for mapping the value to it.

Programmatically Replacing the placeholders with values

Open Visual Studio and create a new Console application.

Install itext7 library from Nuget Package Manager.

Import the libraries in your class files to use the itext7 methods in your program.

Code Section

I have created a “Helper.cs class” to perform most of the logics to read the pdf, save the pdf, process the pdf and to fill the form fields. This is how the code looks like : -

Helper.cs

* Also the modified “Program.cs” class now looks like this : -

Program.cs

Please read the comments and description in code to better understand the logics and the methods used.

Note : — Images are read as PDF Buttons Form Fields. You can directly set the path of Image in SetImage method or if you have the base64 Image string then you can set the same using SetValue method. I went through the code in itext7 library and looks like in SetImage Method :

1. Reads the PDF and converts it into byte stream.

2. Convert the byte stream to base64 Image string.

3. Sets the base64 Image string to SetValue method.

So you can skip the first 2 steps and directly use the SetValue if you have base64 Image String (Refer to the comments and “ConvertImageToBase64” method in Helper.cs).

Output PDF

Result

You can find the completed code on my GitHub Repository here : —

https://github.com/akshsuri/PdfAcroForm.git

Aksh Suri
Senior Engineer
suriaksh1@gmail.com

--

--

Aksh Suri

Senior .NET Developer. Passionate and a keen learner.