Read PDF Form Fields in C# .NET

To read a form field’s data in a PDF document, use one of the methods starting with the GetFormField prefix. All these methods require the form field ID as their parameter. The list below contains the most commonly used methods:

The following example finds all text form fields in a document and prints their content to the console:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Determine the number of form fields.
int fieldCount = gdpicturePDF.GetFormFieldsCount();
// Loop through all form fields.
for (int i = 0; i < fieldCount; i++)
{
    // Get the field ID of each form field.
    int fieldID = gdpicturePDF.GetFormFieldId(i);
    // Check if the current field is a text box.
    if (gdpicturePDF.GetFormFieldType(fieldID) == PdfFormFieldType.PdfFormFieldTypeText)
    {
        // Get the current form field's value and print it to the console.
        string fieldValue = gdpicturePDF.GetFormFieldValue(fieldID);
        Console.WriteLine(fieldValue);
    }
}
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Determine the number of form fields.
    Dim fieldCount As Integer = gdpicturePDF.GetFormFieldsCount()
    ' Loop through all form fields.
    For i = 0 To fieldCount - 1
        ' Get the field ID of each form field.
        Dim fieldID As Integer = gdpicturePDF.GetFormFieldId(i)
        ' Check if the current field is of text box type.
        If gdpicturePDF.GetFormFieldType(fieldID) Is PdfFormFieldType.PdfFormFieldTypeText Then
            ' Get the current form field's value and print it to the console.
            Dim fieldValue As String = gdpicturePDF.GetFormFieldValue(fieldID)
            Console.WriteLine(fieldValue)
        End If
    Next
End Using
Used Methods

Related Topics