Reading PDF form fields using iTextSharp

Here’s a quick bit of code to help you extract data from a PDF with form fields:

Open the PDF:

var pdfReader = new PdfReader(filename);

 

Read the field names:

var fieldList = GetFormFieldNames(pdfReader);

Read the field names with values:

var fieldList = GetFormFieldNamesWithValues(pdfReader);

 

The code that does all of the work:

private static string GetFormFieldNames(PdfReader pdfReader)
{
    return string.Join("\r\n", pdfReader.AcroFields.Fields
                                   .Select(x => x.Key).ToArray());
}

private static string GetFormFieldNamesWithValues(PdfReader pdfReader)
{
    return string.Join("\r\n", pdfReader.AcroFields.Fields
                                   .Select(x => x.Key + "=" + 
                                    pdfReader.AcroFields.GetField(x.Key))
                                   .ToArray());
}
 

Both of these methods will return a list of fields and possibly values, each separated by carriage return/line feed so that each field is on it’s own line.

 

References:

http://stackoverflow.com/questions/4428183/concatenate-a-dictionary-to-a-string-of-key-valkey-val

http://stackoverflow.com/questions/3041883/how-do-i-enumerate-all-the-fields-in-a-pdf-file-in-itextsharp

Leave a Reply 4 comments