Click here to Skip to main content
Email Password   helpLost your password?

Introduction

This article describes a quick and simple approach to programmatically completing a PDF document through the use of the iTextSharp DLL. The article also discusses how one might go about using the iTextSharp DLL to discover and map the fields available within an existing PDF if the programmer has only the PDF but, does not have Adobe Designer or even a list of the names of the fields present in the PDF.

screenShotSmall.JPG

Figure 1: Resulting PDF after Filling in Fields Programmatically

iTextSharp is a C# port of a Java library written to support the creation and manipulation of PDF documents. The project is available for download through SourceForge.net. With the iTextSharp DLL, it is possible to not only populate fields in an existing PDF document, but also to dynamically create PDFs. The examples here are limited to a description of the procedures associated with the completion of a PDF. The download will contain examples of PDF creation in both Visual Basic and C#.

The examples contained herein are dependent upon the availability of the iTextSharp DLL. Use the link provided previously in order to download the DLL locally to your development machine. In order to demonstrate filling out a PDF using the iTextSharp DLL, I downloaded a copy of the W-4 PDF form from the IRS website. The form contains controls and may be filled out programmatically so it serves as a good example.

PDF documents that do not contain controls, i.e. those meant to be printed and filled in with a pencil, cannot be completed using this approach. Of course, if you have access to Adobe tools (Adobe Professional, Adobe Designer), you can always create your own PDFs with controls or can add controls to existing PDFs. Further, although not demonstrated here, you can also use iTextSharp to create a PDF document with embedded controls.

Getting Started

In order to get started, fire up the Visual Studio 2005 IDE and open the attached solution. The solution consists of a single Windows Forms project with a single form. I have also included a PDF that will be used for demonstration purposes; this form is the IRS W-4 form completed by US taxpayers. However, any PDF with embedded controls (text boxes, check boxes, etc.) is fair game for this approach. Note that a reference to the iTextSharp DLL has been included in the project.

All of the project code is contained within the single Windows Form. The form itself contains only a docked textbox used to display all of the field names from an existing PDF document. The completed PDF is generated and stored in the local file system; the PDF is not opened for display by the application.

The application uses the existing PDF as a template and from that template, it creates and populates the new PDF. The template PDF itself is never populated and it is used only to define the format and contents of the completed PDF.

f2.jpg

Figure 2: Solution Explorer

The Code: Main Form

As was previously mentioned, all of the code used in the demonstration application is contained entirely in the project’s single Windows Form. The following section will describe the contents of the code file.

The file begins with the appropriate library imports needed to support the code. Note that the iTextSharp libraries have been included into the project. The namespace and class declaration are in the default configuration.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.IO;

namespace PdfGenerator
{
    public partial class Form1 : Form
    {

The next section of code contains the default constructor and the Form1 load event handler. During form load, two functions are called. Those functions are used to display all of the fields present in the template PDF and to create a new PDF populated with a set of field values.

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ListFieldNames();
            FillForm();
        }

The next section of code contained in the demo application defines a function used to collect the names of all of the fields from the target PDF. The field names are displayed in a text box contained in the application’s form.

        /// <summary>
        /// List all of the form fields into a textbox. The
        /// form fields identified can be used to map each of the
        /// fields in a PDF.
        /// </summary>
        private void ListFieldNames()
        {
            string pdfTemplate = @"c:\Temp\PDF\fw4.pdf";
            // title the form

            this.Text += " - " + pdfTemplate;
            // create a new PDF reader based on the PDF template document

            PdfReader pdfReader = new PdfReader(pdfTemplate);
            // create and populate a string builder with each of the 
            // field names available in the subject PDF

            StringBuilder sb = new StringBuilder();
            foreach (DictionaryEntry de in pdfReader.AcroFields.Fields)
            {
                sb.Append(de.Key.ToString() + Environment.NewLine);
            }
            // Write the string builder's content to the form's textbox

            textBox1.Text = sb.ToString();
            textBox1.SelectionStart = 0;
        }

Figure 3 shows the field names collected from the target PDF using the ListFieldNames function call. In order to map these fields to specific fields in the PDF, one need only copy this list and pass values to each of the fields to identify them. For example, if the form contains ten fields, setting the value (shown next) to a sequential number will result in the display of the numbers 1 to 10 in each of the fields. One can then track that field value back to the field name using this list as the basis for the map. Once the fields have been identified, the application can be written to pass the correct values to the related field.

Checkbox controls may be a little more challenging to figure out. I tried passing several values to the checkbox controls before lining up a winner. In this example, I tried pass 0, 1, true, false, etc. to the field before figuring out that yes sets the check.

f3.jpg

Figure 3: The Available PDF Fields

The next section of code in the demo project is used to fill in the mapped field values. The process is simple enough. The first thing that happens is that that the template file and new file locations are defined and passed to string variables. Once the paths are defined, the code creates an instance of the PDF reader which is used to read the template file, and a PDF stamper which is used to fill in the form fields in the new file. Once the template and target files are set up, the last thing to do is create an instance of AcroFields, which is populated with all of the fields contained in the target PDF. After the form fields have been captured, the rest of the code is used to fill in each field using the field’s SetField function.

In this example, the first worksheet and the W-4 itself are populated with meaningful values whilst the second worksheet is populated with sequential numbers that are then used to map those fields to their location on the PDF. After the PDF has been filled out, the application reads values from the PDF (the first and last names) in order to generate a message indicating that the W-4 for this person was completed and stored.

        private void FillForm()
        {
            string pdfTemplate = @"c:\Temp\PDF\fw4.pdf";
            string newFile = @"c:\Temp\PDF\completed_fw4.pdf";
            PdfReader pdfReader = new PdfReader(pdfTemplate);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
                newFile, FileMode.Create));
            AcroFields pdfFormFields = pdfStamper.AcroFields;
            // set form pdfFormFields
            // The first worksheet and W-4 form

            pdfFormFields.SetField("f1_01(0)", "1"); 
            pdfFormFields.SetField("f1_02(0)", "1");
            pdfFormFields.SetField("f1_03(0)", "1");
            pdfFormFields.SetField("f1_04(0)", "8");
            pdfFormFields.SetField("f1_05(0)", "0");
            pdfFormFields.SetField("f1_06(0)", "1");
            pdfFormFields.SetField("f1_07(0)", "16");
            pdfFormFields.SetField("f1_08(0)", "28");
            pdfFormFields.SetField("f1_09(0)", "Franklin A.");
            pdfFormFields.SetField("f1_10(0)", "Benefield");
            pdfFormFields.SetField("f1_11(0)", "532");
            pdfFormFields.SetField("f1_12(0)", "12");
            pdfFormFields.SetField("f1_13(0)", "1234");
            // The form's checkboxes
            pdfFormFields.SetField("c1_01(0)", "0");
            pdfFormFields.SetField("c1_02(0)", "Yes");
            pdfFormFields.SetField("c1_03(0)", "0");
            pdfFormFields.SetField("c1_04(0)", "Yes");
            // The rest of the form pdfFormFields
            pdfFormFields.SetField("f1_14(0)", "100 North Cujo Street");
            pdfFormFields.SetField("f1_15(0)", "Nome, AK 67201");
            pdfFormFields.SetField("f1_16(0)", "9");
            pdfFormFields.SetField("f1_17(0)", "10");
            pdfFormFields.SetField("f1_18(0)", "11");
            pdfFormFields.SetField("f1_19(0)", "Walmart, Nome, AK");
            pdfFormFields.SetField("f1_20(0)", "WAL666");
            pdfFormFields.SetField("f1_21(0)", "AB");
            pdfFormFields.SetField("f1_22(0)", "4321");
            // Second Worksheets pdfFormFields
            // In order to map the fields, I just pass them a sequential
            // number to mark them; once I know which field is which, I 
            // can pass the appropriate value
            pdfFormFields.SetField("f2_01(0)", "1");
            pdfFormFields.SetField("f2_02(0)", "2");
            pdfFormFields.SetField("f2_03(0)", "3");
            pdfFormFields.SetField("f2_04(0)", "4");
            pdfFormFields.SetField("f2_05(0)", "5");
            pdfFormFields.SetField("f2_06(0)", "6");
            pdfFormFields.SetField("f2_07(0)", "7");
            pdfFormFields.SetField("f2_08(0)", "8");
            pdfFormFields.SetField("f2_09(0)", "9");
            pdfFormFields.SetField("f2_10(0)", "10");
            pdfFormFields.SetField("f2_11(0)", "11");
            pdfFormFields.SetField("f2_12(0)", "12");
            pdfFormFields.SetField("f2_13(0)", "13");
            pdfFormFields.SetField("f2_14(0)", "14");
            pdfFormFields.SetField("f2_15(0)", "15");
            pdfFormFields.SetField("f2_16(0)", "16");
            pdfFormFields.SetField("f2_17(0)", "17");
            pdfFormFields.SetField("f2_18(0)", "18");
            pdfFormFields.SetField("f2_19(0)", "19");
            // report by reading values from completed PDF
            string sTmp = "W-4 Completed for " + 
                pdfFormFields.GetField("f1_09(0)") + " " +
                pdfFormFields.GetField("f1_10(0)");
            MessageBox.Show(sTmp, "Finished");
            // flatten the form to remove editting options, set it to false
            // to leave the form open to subsequent manual edits
    
            pdfStamper.FormFlattening = false;
            // close the pdf
    
            pdfStamper.Close();
        }
    }
}

To finish up the PDF, it is necessary to determine whether or not additional edits will be permitted to the PDF after it has been programmatically completed. This task is accomplished by setting the FormFlattening value to true or false. If the value is set to false, the resulting PDF will be available for edits, but if the value is set to true, the PDF will be locked against further edits. Once the form has been completed, the PDF stamper is closed and the function terminated.

That wraps up the discussion of the form-based demo project.

Summary

This article described an approach to populating a PDF document with values programmatically. This functionality was accomplished using the iTextSharp DLL. Further, the article described an approach for mapping the fields contained in a PDF and may be useful if one is dealing with a PDF authored elsewhere and if the programmer does not have access to Adobe Professional or Adobe Designer.

The iTextSharp library is a powerful DLL that supports authoring PDFs, as well as using them in the manner described in this document. However, when authoring a PDF, it seems that it would be far easier to produce a nice document using the visual environment made available through the use of Adobe tools. Having said that, if one is dynamically creating PDFs with variable content, the iTextSharp library does provide the tools necessary to support such an effort. With the library, one can create and populate a PDF on the fly.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralWorks like a Charm - Thanks!
planetregin
9:34 18 Sep '09  
thanks so much... It's great and has helped me a lot!
GeneralThis document enabled extended features in Adobe Reader, The document has been changes since it was created and use of extended features is no longer available.
backupfortress
17:06 8 Sep '09  
This is great code. Very easy to follow. I think my problem is something new with Acrobat Reader 9. After I update the fields, I get the following error. I am completing a tax form f941.When I open the form with Abode, I can click OK to the pop-up error and see the form. I just can't make any more changes. I was hoping to field out most of the form, send it to the user, and then let them finish filling out the form. Anyway to keep this from happening?

Adobe Reader Error:
This document enabled extended features in Adobe Reader, The document has been changes since it was created and use of extended features is no longer available. Please contact the author for the original version of this document.
GeneralRe: This document enabled extended features in Adobe Reader, The document has been changes since it was created and use of extended features is no longer available.
Goran Djuric
10:05 26 Jan '10  
I Am Getting the same error, Does anyone Have fix for the Adobe Acrobat Reader 9 ( his document enabled extended features in Adobe Reader, The document has been changes since it was created and use of extended features is no longer available.)

BTW THE CODE IS Really GOOD

NICE WORK.

Goran
GeneralAny idea or library for Visual C++?
Hadi Dayvary
3:53 3 Aug '09  
Hi
Any idea or free library for Visual C++?

www.logicsims.ir

GeneralSimple & Great
romangd
17:00 15 Apr '09  
Hi. Fisrt of all, Congratulations !! Thumbs Up
The code is simple but it's very usefull.

I have observed the Fields Form of PDF result could be changed by the user from the Reader Program.
To prevent this I change the PDF template: I set the fisrt Field Form as ReadOnly.
Then I test the sample again. The new PDF resulting looks the same, but the fisrt Field Form cannot be change by the user from the Reader Program.

Thanks,
RomanGD
GeneralFields issues
ZachCoder
17:55 28 Mar '09  
Hi

I followed the example closely with test out on my pdf file.
For unclear reasons, some check boxes are filled while others are not on my pdf, after executing FillForm().
I then ran only ListFieldNames() and came to the conclusion that those same check boxes, for some of them, just would not "check" even if I put "Yes" values.

Any help is appreciated.
GeneralText is doing a dance
dokmanov
19:49 31 Jan '09  
When open up a pdf file filled using this method and you select a textbox that was prefilled using the library, the text does a dance and sometime the anount of text that appears in the text box (especially for multi-line text boxes) changes.

Please view the screen capture:

[Screen sesson capture]
GeneralUse PDFStamper to merge PDF's
Tarang Pandya
7:00 12 Jan '09  
Hi,

can u help me out? i have list of pdfs having writable fields in them. Now i have to write those fields in each pdfs and need to merge all the pdfs in to one as well let the final pdf to be writable.
i tried using pdfstamper , but the contents are overlapping each one.

do u have any other idea?

Thanks
Tarang Pandya

GeneralHi Salysle !
harsha318_
1:38 22 Dec '08  
Hi ,
Can any one make the PDFForm text fields visible and in-visible.
Thanks In Advance.
QuestionWrite PdfStamp result to Byte Array [] instead of localFile
rafael.net
8:59 10 Sep '08  
I want to open the pdf file, fill some form fields (with pdfStamp) and save the result into array of bytes instead of save into localFile. I have a limitiation in my platform and I cant save directly to disk.

Best Regards, RP
GeneralIOException not found as file or resource
Nav Mathur
10:00 30 Jul '08  
Hello -

First, thank you very much for posting this sample up.

This statement in the code is giving me an error:

PdfReader pdfReader = new PdfReader(pdfTemplate);

It gives me an IOException saying "C:\myPdf.pdf" not found as file or resource. Please note that I do have "myPdf.pdf" in the root directory.

Also, if I use a byte array for the new pdfReader like the lines below ...


string fileName = @"C:\myPdf.pdf";
FileStream fStream = File.OpenRead(fileName);
byte[] contents = new byte[fStream.Length];
PdfReader pdfReader = new PdfReader(contents);

... it finds the file but throws an exception with this message "PDF header signature not found."

I am at a complete loss for what to do next.

PLEASE HELP!!!!
GeneralRe: IOException not found as file or resource
umg
6:23 30 Dec '09  
Hello;
I have a same problem.
IO exception with file is not found in PDF filler.
Could you help?
Thank you very much.
GeneralHyperlink PDF
danasegaranea
21:55 3 Jul '08  
Hi How can read the URL Links in a PDF Page ?
Questioncreating pdf form using iTextSharp
priyanka_ag
1:50 30 Jun '08  
hi,
I have created a pdf form on fly using iTextSharp, which is supposed to be filled offline. but when i tried to save the changes made in the editable fields it was asking for Adobe Acrobat to be installed. Is it necessary to have Adobe Acrobat(professional or standard edition) to save the contents of the pdf form or we have some option available with iTextSharp which allows us to create a pdf form so that the changes made in the editable fields can be saved without having Adobe Acrobat???
plz help me out.
QuestionVB 6
Neal Katz
8:52 28 Apr '08  
I downloaded your .net object to fill in a PDF form. It works great. My question is, do you have an object that does the same thing but in VB6? I am looking forward to your response.

Thank you,

Neal
Generalgood job
cinamon
16:17 13 Mar '08  
good job with this article, for anyone interested in serious dynamicPDF creation & modification, I have to say the content used from the iTEXTsharp library in this article is only a very very small fraction and the library itself will blow anything else away, its a brilliant tool and can do almost anything with a PDF via code, get the book 'iText In Action' if you want to read more about this

well done!
GeneralIOException error
Member 670685
12:05 30 Jan '08  
Good morning when i run your applcation at this point ...

// create a new PDF reader based on the PDF template document
PdfReader pdfReader = new PdfReader(pdfTemplate);
the debug application return this error messsage:

System.IO.IOException was unhandled
Message="Rebuild failed: Formato della stringa di input non corretto.; Original message: Error reading string at file pointer 228112"
Source="itextsharp"
StackTrace:
in iTextSharp.text.pdf.PdfReader.ReadPdf()
in iTextSharp.text.pdf.PdfReader..ctor(String filename, Byte[] ownerPassword)
in iTextSharp.text.pdf.PdfReader..ctor(String filename)
in PdfGenerator.Form1.ListFieldNames() in E:\MySource\Software\PdfGenerator_CS\PdfGenerator\Form1.cs:riga 46
in PdfGenerator.Form1.Form1_Load(Object sender, EventArgs e) in E:\MySource\Software\PdfGenerator_CS\PdfGenerator\Form1.cs:riga 28
in System.Windows.Forms.Form.OnLoad(EventArgs e)
.....

what can i do?

Thanks
Paolo
GeneralRe: IOException error
salysle
16:23 30 Jan '08  
Paulo:

Put a break point on PdfReader pdfReader = new PdfReader(pdfTemplate) and check the file path for pdfTemplate; make sure that file exists and that it can be opened.
GeneralRe: IOException error
Member 670685
0:18 31 Jan '08  
Thanks a lot....the file was corrupted!!!!!

Sorry!

I promise the next time i'will do more attention...

Paolo.
GeneralRe: IOException error
salysle
1:39 31 Jan '08  
Thanks for the update; I am glad you got it sorted it out.
QuestionRe: IOException error
RJSummerfield
9:36 7 Dec '09  
Hi
This is probably really stupid - I would dearly love to implement your code for iTextSharp, but I'm getting kind of old and have just not made the transition from VB to vb.net. I suppose you have zero interest in VB anymore, but "asking doesn't cost extra" as the Germans say. So I thought I's ask if you might have realised the same procedures in vb or knew anybody who has.
Thanks,
Bob


Last Updated 22 Jan 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010