Click here to Skip to main content
15,563,352 members
Articles / Desktop Programming / Windows Forms
Posted 7 Nov 2013

Tagged as


22 bookmarked

Filling PDF Form using iText PDF Library

Rate me:
Please Sign up or sign in to vote.
4.85/5 (15 votes)
7 Nov 2013CPOL3 min read
This tip describes how to fill the PDF Form Template programmatically


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

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:

  1. Text Box
  2. Push Button
  3. Check Box
  4. Radio Button
  5. List Box
  6. Combo Box
  7. Signature

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 Dictionary variable 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 Class is used to read a PDF file - 
//there are 12 methods provided by this class to read PDF file
//Among those methods, the simplest way is to just give a 
//file name along with its complete path (as shown below)
PdfReader rdr = new PdfReader("pdf_file_path");
//PDFStamper class allow to manipulate the content of existing PDF file.
//There are 3 methods provided to create an object of PDFStamper Class.
//Among which I choose, is required 2 arguments - 
//a PDFReader object and a new Output File as a Stream.
PdfStamper stamper = new PdfStamper(rdr, 
new System.IO.FileStream("output_file_path","file_mode"));

The SetField method of class AcroFields is used to assign the values for fields in PDF form. AcroFields exists in both PDFReader and 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 PDFStamper class.

Application GUI

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.

//returns a Type of License choose for a Software
        string getLicense(){
		string license = "";
		if (rbDemo.Checked == true)		license = "Demo";
		if (rbFreeware.Checked == true)		license = "Freeware";
		if (rbShareware.Checked == true)	license = "Shareware";
	return license;}
//Assign value for 'Software Name' Field
stamper.AcroFields.SetField("softwarename", txtSoftware.Text);

//Assign value for 'Software Type' Field
type = "web-based | standalone | cloud-based"
stamper.AcroFields.SetField("softwaretype", type);

//Assign value for 'Licensed As' Field
stamper.AcroFields.SetField("license", getLicense());

//Assign value for 'Category' Field
stamper.AcroFields.SetField("category", cbCategory.SelectedItem.ToString());

//Assign value for 'Supported Platforms' Field
foreach (string item in lbPlatform.SelectedItems) platforms += item + ", ";
stamper.AcroFields.SetField("platforms", platforms);

//Select Output PDF Type
if (rbEdit.Checked == true)
    stamper.FormFlattening = false; //Editable PDF Form 
else stamper.FormFlattening = true; //Non-Editable PDF Form

stamper.Close();//Close a PDFStamper Object
rdr.Close();    //Close a PDFReader Object


Image 2

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
India India
I’m Mayur B. Dighe.

Comments and Discussions

QuestionThank you! Pin
Member 978369630-Jul-15 15:15
Member 978369630-Jul-15 15:15 
AnswerRe: Thank you! Pin
MayurDighe11-Nov-15 21:01
professionalMayurDighe11-Nov-15 21:01 
Questionfill pdf document Pin
marekpetr27-Jul-15 5:51
marekpetr27-Jul-15 5:51 
AnswerRe: fill pdf document Pin
MayurDighe11-Nov-15 21:44
professionalMayurDighe11-Nov-15 21:44 
QuestionPDF Form Fill using C Pin
Member 1175866922-Jun-15 22:23
Member 1175866922-Jun-15 22:23 
AnswerRe: PDF Form Fill using C Pin
MayurDighe13-Jul-15 9:30
professionalMayurDighe13-Jul-15 9:30 
QuestionNot editable after creation but with the filled text Pin
Bruno Maddalozzo16-Jun-15 8:18
Bruno Maddalozzo16-Jun-15 8:18 
Member 1142221324-May-15 20:17
Member 1142221324-May-15 20:17 
MayurDighe26-May-15 21:17
professionalMayurDighe26-May-15 21:17 
GeneralMy vote of 5 Pin
Manuel Paz1-Apr-15 6:48
Manuel Paz1-Apr-15 6:48 
GeneralRe: My vote of 5 Pin
MayurDighe6-May-15 12:14
professionalMayurDighe6-May-15 12:14 
QuestionPDF forms filling from a template PDF Pin
stixoffire3-Nov-14 11:52
stixoffire3-Nov-14 11:52 
Questionnot editable after creation Pin
FabioSantos28-Mar-14 8:15
FabioSantos28-Mar-14 8:15 
AnswerRe: not editable after creation Pin
MayurDighe29-Mar-14 23:35
professionalMayurDighe29-Mar-14 23:35 
QuestionGetting an exception Pin
Member 106570529-Mar-14 22:54
Member 106570529-Mar-14 22:54 
AnswerRe: Getting an exception Pin
MayurDighe9-Mar-14 23:37
professionalMayurDighe9-Mar-14 23:37 
Questionhow to create the template "Software" and how to assingn variables? to it Pin
Member 106413523-Mar-14 23:48
Member 106413523-Mar-14 23:48 
AnswerRe: how to create the template "Software" and how to assingn variables? to it Pin
MayurDighe9-Mar-14 23:44
professionalMayurDighe9-Mar-14 23:44 
QuestionForce non-editable pdf fields to be displayed. Pin
sysmith9-Dec-13 14:34
sysmith9-Dec-13 14:34 
QuestionFields come up blank when FormFlattening = true Pin
sysmith9-Dec-13 12:05
sysmith9-Dec-13 12:05 
AnswerRe: Fields come up blank when FormFlattening = true Pin
pix81755-Feb-18 6:25
pix81755-Feb-18 6:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.