Read PDF interactive form fields in C# and VB.NET

GemBox.Pdf supports reading PDF interactive form fields.

An interactive form in GemBox.Pdf is represented by a PdfInteractiveForm class and can be accessed via Form property of a PdfDocument class. The base class of all PDF fields is a PdfField and can be accessed via Fields property of a PdfInteractiveForm class.

The following example demonstrates how to read all PDF interactive form fields, their type, name, value and other properties, in a PDF document.

Screenshot of PDF interactive form fields read with GemBox.Pdf
PDF interactive form fields read with GemBox.Pdf
Upload your file (Drag file here)
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using GemBox.Pdf;
using GemBox.Pdf.Forms;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var writer = new StringWriter(CultureInfo.InvariantCulture);
        string format = "{0,-16}|{1,20}|{2,-20}|{3,-20}|{4,-20}", separator = new string('-', 100);

        // Write header.
        writer.WriteLine("Document contains the following form fields:");
        writer.WriteLine();
        writer.WriteLine(format,
            "Type",
            '"' + "Name" + '"',
            "Value",
            "ExportValue/Choice",
            "Checked/Selected");
        writer.WriteLine(separator);

        PdfFieldType? fieldType;
        string fieldName, fieldExportValueOrChoice;
        object fieldValue;
        bool? fieldCheckedOrSelected;

        using (var document = PdfDocument.Load("%InputFileName%"))
            // Group fields by name because all fields with the same name are actually different representations (widget annotations) of the same field.
            // Radio button fields are usually grouped. Other field types are rarely grouped.
            foreach (var fieldGroup in document.Form.Fields.GroupBy(field => field.Name))
            {
                var field = fieldGroup.First();

                fieldType = field.FieldType;
                fieldName = '"' + field.Name + '"';
                fieldValue = field.Value;

                foreach (var widgetField in fieldGroup)
                {
                    switch (widgetField.FieldType)
                    {
                        case PdfFieldType.CheckBox:
                        case PdfFieldType.RadioButton:
                            // Check box and radio button are toggle button fields.
                            var toggleField = (PdfToggleButtonField)widgetField;

                            fieldExportValueOrChoice = toggleField.FieldType == PdfFieldType.CheckBox ?
                                    ((PdfCheckBoxField)toggleField).ExportValue :
                                    ((PdfRadioButtonField)toggleField).Choice;
                            fieldCheckedOrSelected = toggleField.Checked;

                            writer.WriteLine(format,
                                fieldType,
                                fieldName,
                                fieldValue,
                                fieldExportValueOrChoice,
                                fieldCheckedOrSelected);
                            break;

                        case PdfFieldType.ListBox:
                        case PdfFieldType.Dropdown:
                            // List box and drop-down are choice fields.
                            var choiceField = (PdfChoiceField)widgetField;

                            // List box can have multiple values if multiple selection is enabled.
                            if (fieldValue is string[] fieldValues)
                                fieldValue = string.Join(", ", fieldValues);

                            for (int itemIndex = 0; itemIndex < choiceField.Items.Count; ++itemIndex)
                            {
                                fieldExportValueOrChoice = choiceField.Items[itemIndex].ExportValue ?? choiceField.Items[itemIndex].Value;
                                fieldCheckedOrSelected = choiceField.FieldType == PdfFieldType.ListBox ?
                                        ((PdfListBoxField)choiceField).SelectedIndices.Contains(itemIndex) :
                                        ((PdfDropdownField)choiceField).SelectedIndex == itemIndex;

                                writer.WriteLine(format,
                                fieldType,
                                fieldName,
                                fieldValue,
                                fieldExportValueOrChoice,
                                fieldCheckedOrSelected);

                                // Write field type, field name and field value just once for a field group.
                                fieldType = null;
                                fieldName = null;
                                fieldValue = null;
                            }
                            break;

                        default:
                            fieldExportValueOrChoice = null;
                            fieldCheckedOrSelected = null;

                            writer.WriteLine(format,
                                fieldType,
                                fieldName,
                                fieldValue,
                                fieldExportValueOrChoice,
                                fieldCheckedOrSelected);
                            break;
                    }

                    // Write field type, field name and field value just once for a field group.
                    fieldType = null;
                    fieldName = null;
                    fieldValue = null;
                }

                writer.WriteLine(separator);
            }

        Console.Write(writer.ToString());
    }
}
Imports System
Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports GemBox.Pdf
Imports GemBox.Pdf.Forms

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim writer = New StringWriter(CultureInfo.InvariantCulture)
        Dim format As String = "{0,-16}|{1,20}|{2,-20}|{3,-20}|{4,-20}", separator As String = New String("-"c, 100)

        ' Write header.
        writer.WriteLine("Document contains the following form fields:")
        writer.WriteLine()
        writer.WriteLine(format,
            "Type",
            """"c & "Name" & """"c,
            "Value",
            "ExportValue/Choice",
            "Checked/Selected")
        writer.WriteLine(separator)

        Dim fieldType As PdfFieldType?
        Dim fieldName, fieldExportValueOrChoice As String
        Dim fieldValue As Object
        Dim fieldCheckedOrSelected As Boolean?

        Using document = PdfDocument.Load("%InputFileName%")
            ' Group fields by name because all fields with the same name are actually different representations (widget annotations) of the same field.
            ' Radio button fields are usually grouped. Other field types are rarely grouped.
            For Each fieldGroup In document.Form.Fields.GroupBy(Function(field) field.Name)

                Dim field = fieldGroup.First()

                fieldType = field.FieldType
                fieldName = """"c + field.Name + """"c
                fieldValue = field.Value

                For Each widgetField In fieldGroup

                    Select Case widgetField.FieldType

                        Case PdfFieldType.CheckBox,
                             PdfFieldType.RadioButton
                            ' Check box and radio button are toggle button fields.
                            Dim toggleField = CType(widgetField, PdfToggleButtonField)

                            fieldExportValueOrChoice = If(toggleField.FieldType = PdfFieldType.CheckBox,
                                CType(toggleField, PdfCheckBoxField).ExportValue,
                                CType(toggleField, PdfRadioButtonField).Choice)
                            fieldCheckedOrSelected = toggleField.Checked

                            writer.WriteLine(format,
                                fieldType,
                                fieldName,
                                fieldValue,
                                fieldExportValueOrChoice,
                                fieldCheckedOrSelected)


                        Case PdfFieldType.ListBox,
                             PdfFieldType.Dropdown
                            ' List box and drop-down are choice fields.
                            Dim choiceField = CType(widgetField, PdfChoiceField)

                            ' List box can have multiple values if multiple selection is enabled.
                            Dim fieldValues = TryCast(fieldValue, String())
                            If fieldValues IsNot Nothing Then fieldValue = String.Join(", ", fieldValues)

                            For itemIndex As Integer = 0 To choiceField.Items.Count - 1

                                fieldExportValueOrChoice = If(choiceField.Items(itemIndex).ExportValue, choiceField.Items(itemIndex).Value)
                                fieldCheckedOrSelected = If(choiceField.FieldType = PdfFieldType.ListBox,
                                    CType(choiceField, PdfListBoxField).SelectedIndices.Contains(itemIndex),
                                    CType(choiceField, PdfDropdownField).SelectedIndex = itemIndex)

                                writer.WriteLine(format,
                                    fieldType,
                                    fieldName,
                                    fieldValue,
                                    fieldExportValueOrChoice,
                                    fieldCheckedOrSelected)

                                ' Write field type, field name and field value just once for a field group.
                                fieldType = Nothing
                                fieldName = Nothing
                                fieldValue = Nothing
                            Next


                        Case Else
                            fieldExportValueOrChoice = Nothing
                            fieldCheckedOrSelected = Nothing

                            writer.WriteLine(format,
                                fieldType,
                                fieldName,
                                fieldValue,
                                fieldExportValueOrChoice,
                                fieldCheckedOrSelected)

                    End Select

                    ' Write field type, field name and field value just once for a field group.
                    fieldType = Nothing
                    fieldName = Nothing
                    fieldValue = Nothing
                Next

                writer.WriteLine(separator)
            Next
        End Using

        Console.Write(writer.ToString())
    End Sub
End Module

Check next example or download examples from GitHub.