Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Filling PDF Form using iText PDF Library

0.00/5 (No votes)
7 Nov 2013 1  
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.

//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

Output

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here