How to read PDF form data using iTextSharp?

I am trying to find out if it is possible to read PDF Form data (Forms filled in and saved with the form) using iTextSharp. How can I do this?

24.3k 16 16 gold badges 91 91 silver badges 135 135 bronze badges asked Jul 29, 2010 at 22:13 1,553 4 4 gold badges 25 25 silver badges 50 50 bronze badges

6 Answers 6

You would have to find out the field names in the PDF form. Get the fields and then read their value.

string pdfTemplate = "my.pdf"; PdfReader pdfReader = new PdfReader(pdfTemplate); AcroFields fields = pdfReader.AcroFields.Fields; string val = fields.GetField("fieldname"); 

Obviously in the code above, field name is the name of the PDF form field and the GetField method returns a string representation of that value. Here is an article with example code that you could probably use. It shows how you can both read and write form fields using iTextSharp.

5,961 4 4 gold badges 39 39 silver badges 47 47 bronze badges answered Jul 30, 2010 at 0:34 cecilphillip cecilphillip 11.5k 5 5 gold badges 37 37 silver badges 40 40 bronze badges

this works like a charm. I wonder why I haven't looked into this function.. when I tried every other function :). Thanks a lot.. u saved my weekend.

Commented Jul 30, 2010 at 23:22

Public Service Announcement: The following code will not get you what you want: pdfReader.AcroFields.Fields["fieldName"].Value . I wasted a few hours before I found this post.

Commented Jun 21, 2013 at 13:55

Hi. I think there is an error in the third line of your example. The correct form would be: AcroFields fields = pdfReader.AcroFields;

Commented Jul 18, 2017 at 17:43 Why might a PDF list with zero fields, even though it does have fields? Commented Dec 9, 2022 at 14:37

Maybe the iTextSharp library has changed recently but I wasn't able to get the accepted answer to work. Here is my solution:

var pdf_filename = "pdf2read.pdf"; using (var reader = new PdfReader(pdf_filename)) < var fields = reader.AcroFields.Fields; foreach (var key in fields.Keys) < var value = reader.AcroFields.GetField(key); Console.WriteLine(key + " : " + value); >> 

A very subtle difference, due to reader.AcroFields.Fields returning an IDictionary instead of just an AcroFields object.

answered Oct 15, 2014 at 1:07 Adam Jones Adam Jones 2,420 5 5 gold badges 24 24 silver badges 41 41 bronze badges

this works, but is really slow, takes over a minute to read ~3000 fields. Anyone know a faster way to enumerate these? I tried doing it in parallel but that didn't seem to help.

Commented Oct 18, 2016 at 4:47 I can't get it to work with the PDF I am using. Shows no fields. Commented Dec 9, 2022 at 14:36

If you are using Powershell, the discovery code for fields is:

 Add-Type -Path C:\Users\Micah\Desktop\PDF_Test\itextsharp.dll $MyPDF = "C:\Users\Micah\Desktop\PDF_Test\something_important.pdf" $PDFDoc = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $MyPDF $PDFDoc.AcroFields.Fields 

That code will give you the names of all the fields on the PDF Document, "something_important.pdf".

This is how you access each field once you know the name of the field:

 $PDFDoc.AcroFields.GetField("Name of the field here") 
answered Aug 21, 2013 at 17:21 The Powershell Ninja The Powershell Ninja 779 5 5 silver badges 6 6 bronze badges

This worked for me! Note the parameters when defining stamper! '\0', true

string TempFilename = Path.GetTempFileName(); PdfReader pdfReader = new PdfReader(FileName); //PdfStamper stamper = new PdfStamper(pdfReader, new FileStream(TempFilename, FileMode.Create)); PdfStamper stamper = new PdfStamper(pdfReader, new FileStream(TempFilename, FileMode.Create), '\0', true); AcroFields fields = stamper.AcroFields; AcroFields pdfFormFields = pdfReader.AcroFields; foreach (KeyValuePair kvp in fields.Fields) < string FieldValue = GetXMLNode(XMLFile, kvp.Key); if (FieldValue != "") < fields.SetField(kvp.Key, FieldValue); >> stamper.FormFlattening = false; stamper.Close(); pdfReader.Close() 
18.9k 17 17 gold badges 78 78 silver badges 202 202 bronze badges answered Sep 2, 2013 at 23:44 57 2 2 bronze badges

The OP only wanted to read PDF Form data (and got a good answer for that). Your code shows how to change PDF form data.

Commented Sep 3, 2013 at 8:10

Sorry, actually posted the answer into the wrong thread. This meant to be explanation on how push values into the fields and preserve form editing when the file is opened again.