C#/VB.NET: Extract Form Field Values from PDF

Form fields are often used in documents like surveys, registration forms, or feedback forms to collect data from users. Extracting form field values allows you to gather and consolidate the submitted data for further analysis or processing. In this article, we will demonstrate how to extract form field values from PDF documents in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Extract Form Field Values from PDF in C# and VB.NET

In a PDF document, you may encounter various types of form fields, such as textboxes, checkboxes, radio buttons, list boxes, and combo boxes (drop-down lists). Before extracting form field values, it is crucial to identify the specific type of each form field. Once identified, you can utilize corresponding properties tailored for each form field type to accurately extract their values. The detailed steps are as follows:

  • Initialize an instance of the PdfDocument instance.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Initialize an instance of the StringBuilder class for storing the extract form field values.
  • Get the form from the document using PdfDocument.Form property.
  • Iterate through all form fields in the form.
  • Determine the types of the form fields, then get the names and values of the form fields using the corresponding properties and append them to the StringBuilder instance.
  • Write the content of the StringBuilder instance into a text file.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;
using System.IO;
using System.Text;

namespace ExtractFormFieldValues
{
    internal class Program
    {
        static void Main(string[] args)
        {            
            //Initialize an instance of the PdfDocument instance
            PdfDocument doc = new PdfDocument();
            //Load a PDF document
            doc.LoadFromFile(@"Forms.pdf");

            //Initialize an instance of the StringBuilder class
            StringBuilder sb = new StringBuilder();

            //Get the form from the document
            PdfFormWidget formWidget = doc.Form as PdfFormWidget;

            //Iterate through all fields in the form
            for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
            {
                PdfField field = formWidget.FieldsWidget.List[i] as PdfField;

                //Get the name and value of textbox field
                if (field is PdfTextBoxFieldWidget)
                {
                    PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;                    
                    string name = textBoxField.Name;
                    string value = textBoxField.Text;
                    sb.Append("Textbox Name: " + name + "\r\n");
                    sb.Append("Textbox Value: " + value + "\r\n");
                }

                //Get the name, items and selected item of list box field
                if (field is PdfListBoxWidgetFieldWidget)
                {
                    PdfListBoxWidgetFieldWidget listBoxField = field as PdfListBoxWidgetFieldWidget;
                    string name = listBoxField.Name;
                    sb.Append("Listbox Name: " + name + "\r\n");
                    sb.Append("Listbox Items: \r\n");

                    PdfListWidgetItemCollection items = listBoxField.Values;

                    foreach (PdfListWidgetItem item in items)
                    {
                        sb.Append(item.Value + "\r\n");
                    }
                    string selectedValue = listBoxField.SelectedValue;
                    sb.Append("Listbox Selected Value: " + selectedValue + "\r\n");
                }

                //Get the name, items and selected item of combo box field
                if (field is PdfComboBoxWidgetFieldWidget)
                {
                    PdfComboBoxWidgetFieldWidget comBoxField = field as PdfComboBoxWidgetFieldWidget;
                    string name = comBoxField.Name;
                    sb.Append("Combobox Name: " + name + "\r\n");
                    sb.Append("Combobox Items: \r\n");
                    PdfListWidgetItemCollection items = comBoxField.Values;

                    foreach (PdfListWidgetItem item in items)
                    {
                        sb.Append(item.Value + "\r\n");
                    }
                    string selectedValue = comBoxField.SelectedValue;
                    sb.Append("Combobox Selected Value: " + selectedValue + "\r\n");

                }

                //Get the name and selected item of radio button field
                if (field is PdfRadioButtonListFieldWidget)
                {
                    PdfRadioButtonListFieldWidget radioBtnField = field as PdfRadioButtonListFieldWidget;
                    string name = radioBtnField.Name;
                    sb.Append("Radio Button Name: " + name + "\r\n");
                    string selectedValue = radioBtnField.SelectedValue;
                    sb.Append("Radio Button Selected Value: " + selectedValue + "\r\n");
                }

                //Get the name and status of checkbox field
                if (field is PdfCheckBoxWidgetFieldWidget)
                {
                    PdfCheckBoxWidgetFieldWidget checkBoxField = field as PdfCheckBoxWidgetFieldWidget;
                    string name = checkBoxField.Name;
                    sb.Append("Checkbox Name: " + name + "\r\n");
                    bool status = checkBoxField.Checked;
                    if (status)
                    {
                        sb.Append("Checkbox Status: Checked \r\n");
                    }
                    else
                    {
                        sb.Append("Checkbox Status: Unchecked \r\n");
                    }
                }

                sb.Append("\n");
            }

            //Write the content of the StringBuilder into a text file
            File.WriteAllText("GetAllValues.txt", sb.ToString());
            doc.Dispose();
        }
    }
}

C#/VB.NET: Extract Form Field Values from PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.