Dev.to · 4 min read

How to Batch Extract PDF Form Data Using C#

How to Batch Extract PDF Form Data Using C#

In enterprise workflows, PDF forms are widely used for data collection. After users fill out a form, systems typically need to automatically extract the field values for storage, validation, or downstream processing. This article demonstrates how to read interactive PDF form fields—including text boxes, checkboxes, radio buttons, list boxes, and combo boxes—using C#. 1. Approach Overview Every form field in a PDF has a unique name, a type indicator, and a current value. The overall process is straightforward: Load the PDF document. Access the form collection. Iterate through all fields. Extract the value according to each field’s type. We’ll use the Free Spire.PDF for .NET library, which offers the PdfFormWidget class for form manipulation and supports field lookup by index or name. 2. Prerequisites 2.1 Install the NuGet Package Install FreeSpire.PDF via the Visual Studio NuGet Package Manager, or run the following command in the Package Manager Console: Install-Package FreeSpire.PDF Once installed, the Spire.Pdf.dll reference is added automatically. Note that the free version limits processing to the first 10 pages per document, which is typically sufficient for form extraction. 2.2 Import Required Namespaces using Spire.Pdf; using Spire.Pdf.Widget; using System; using System.Text; 3. Basic Workflow for Reading Form Fields 3.1 Load the Document and Retrieve the Form PdfDocument doc = new PdfDocument(); doc.LoadFromFile("YourForm.pdf"); // Cast to PdfFormWidget to access the full field collection PdfFormWidget formWidget = doc.Form as PdfFormWidget; The doc.Form property returns a generic form object; casting to PdfFormWidget gives you the complete field list. 3.2 Iterate Over All Fields for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++) { PdfField field = formWidget.FieldsWidget.List[i] as PdfField; Console.WriteLine($"Field Name: {field.Name}"); } 4. Extracting Values by Field Type Different field types store their values in different properties. Use type checking and casting to retrieve the correct data. 4.1 Text Box (PdfTextBoxFieldWidget) if (field is PdfTextBoxFieldWidget textBoxField) { Console.WriteLine($"TextBox - Name: {textBoxField.Name}, Value: {textBoxField.Text}"); } 4.2 Checkbox (PdfCheckBoxWidgetFieldWidget) if (field is PdfCheckBoxWidgetFieldWidget checkBoxField) { Console.WriteLine($"CheckBox - Name: {checkBoxField.Name}, Checked: {checkBoxField.Checked}"); } 4.3 Radio Button Group (PdfRadioButtonListFieldWidget) if (field is PdfRadioButtonListFieldWidget radioBtnField) { Console.WriteLine($"RadioButton - Name: {radioBtnField.Name}, SelectedValue: {radioBtnField.SelectedValue}"); } 4.4 List Box (PdfListBoxWidgetFieldWidget) if (field is PdfListBoxWidgetFieldWidget listBoxField) { Console.WriteLine($"ListBox - Name: {listBoxField.Name}, SelectedValue: {listBoxField.SelectedValue}"); // List all available options foreach (PdfListWidgetItem item in listBoxField.Values) { Console.WriteLine($" Option: {item.Value}"); } } 4.5 Combo Box (Drop‑down) (PdfComboBoxWidgetFieldWidget) if (field is PdfComboBoxWidgetFieldWidget comboField) { Console.WriteLine($"ComboBox - Name: {comboField.Name}, SelectedValue: {comboField.SelectedValue}"); } 5. Important Notes and Troubleshooting 5.1 Check for Empty Forms Not all PDFs contain interactive forms. If doc.Form cannot be cast to PdfFormWidget, or if formWidget.FieldsWidget.Count is zero, the document has no form fields. In such cases, fall back to plain text extraction. 5.2 AcroForm vs. XFA Forms PDF forms come in two standards: AcroForm – Adobe’s native format, which is what this article covers (handled by PdfFormWidget). XFA Forms – XML‑based forms, often created with Adobe LiveCycle. These require separate handling via formWidget.XFAForm, as their field structure and value access are entirely different. 5.3 Release Resources Always close the document to free up resources: doc.Close(); 6. Summary Successfully reading PDF form fields hinges on correctly identifying each field’s type and using the appropriate property to retrieve its value. The PdfFormWidget class provides a unified interface, and pattern matching with as makes type conversion clean and safe, covering most AcroForm scenarios. In practice, it’s wise to first explore the field structure and naming conventions of your target PDF by iterating all fields. Then encapsulate the extracted data into a strongly typed model for business logic. This approach ensures accuracy, maintainability, and ease of future extension.

This is a summary aggregated from Dev.to. Read the complete article on the original site:

Read full article at Dev.to

Related stories