This tip shows the simple way to fill a PDF Form Template programmatically. I know that already lots of articles have been posted by other experts, software developers/programmers which can be easier to adopt for anyone. However, I posted this tip to enhance my programming skills and abilities.
For this, I use Microsoft Visual C# 2010 Express as the programming environment and iText PDF Library to fill PDF form programmatically.
iText PDF Library
The iText PDF Library is free and open source software, & there is a C# port - iTextSharp, used for creating and manipulating PDF documents programmatically. The iText is available on SourceForge.net.
Filling a PDF Form Template
I have created a sample PDF Form Template to fill it (see the download section above). Before filling a PDF Form, we should be aware of the fields used in the PDF Form so that we can fill the appropriate values in it.
The iText PDF Library allows 7 fields to be used programmatically as follows:
- Text Box
- Push Button
- Check Box
- Radio Button
- List Box
- Combo Box
From these 7 fields, this tip represents how to fill the Text Box field programmatically. The sample PDF Template contains 5 fields (softwarename, softwaretype, license, category, platforms).
Using the Code
To fill the PDF Form Template programmatically - the most important thing is that - one should know how many fields are present in that PDF form and their associated internal names, so that appropriate values can filled in it.
From the C# Port of iText - iTextSharp - The
Fields of class
AcroFields gives a list of field names & their associated values present in PDF Form Template. For demonstrating this, I created a Sample PDF Form Template using Open Office 4.0.1. Editable PDF Forms created through other than Adobe products does not allow user to save data into same PDF file.
Few lines of code in Actual Code are quite lengthy hence I put here pseudo code for Filling PDF Form (see source code file for actual code).
In order to fill the fields programmatically, first we need to include a namespace
iTextSharp.text.pdf and then create an object for reading and writing PDF document.
PdfReader rdr = new PdfReader("pdf_file_path");
PdfStamper stamper = new PdfStamper(rdr,
SetField method of class
AcroFields is used to assign the values for fields in PDF form.
AcroFields exists in both
PDFStamper class. In
PDFReader, fields retrieved are read-only, whereas in
PDFStamper it allows to manipulate the value of fields. Thus we must use the object of class
AcroFields along with the object of
Figure 1 : Application GUI
Figure 1 shows a GUI of Application to be filled programmatically into PDF Form. This application allows you to generate Editable and Non-editable form of PDF Form Template. In non-editable form, the PDF file is no longer able to fill the fields in both ways - manually or computerized. But in Editable form, we are still able to manipulate the fields of PDF file.
The following code snippet shows how to assign a particular value to fields of PDF form.
string license = "";
if (rbDemo.Checked == true) license = "Demo";
if (rbFreeware.Checked == true) license = "Freeware";
if (rbShareware.Checked == true) license = "Shareware";
type = "web-based | standalone | cloud-based"
foreach (string item in lbPlatform.SelectedItems) platforms += item + ", ";
if (rbEdit.Checked == true)
stamper.FormFlattening = false; else stamper.FormFlattening = true;
Figure 2 : Output Screenshot
Figure 2 is a screenshot of final output PDF form in non-editable form.
For successful execution of this program, you need to download "iTextSharp.dll" from here.
Using Demo: Place the "iTextSharp.dll" file into the same directory of this program.
Using Source Code: Add the "iTextSharp.dll" into "References" section present in Solution Explorer section of IDE. (I use Microsoft Visual C# 2010 Express as IDE for development of this program.)
The tip describes the simple method for filling fields in PDF Form template programmatically. The iText PDF Library is the powerful tool for creating and manipulating the PDF files programmatically. It provides ease to handle PDF files programmatically.