Click here to Skip to main content
15,886,578 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

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 107.8K   8K   22   21
This tip describes how to fill the PDF Form Template programmatically

Introduction

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:

  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.

C#
//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.

C#
//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;}
C#
//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

Output

Image 2

Figure 2 : Output Screenshot

Figure 2 is a screenshot of final output PDF form in non-editable form.

Note

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.)

Summary

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.

License

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


Written By
Software Developer https://mayurdighe.me
India India
I’m Mayur B. Dighe.

Comments and Discussions

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