Get the PDF Form Field ID in C# .NET

The form field ID is a parameter required in most methods related to form fields. Use it to specify the form field upon which you want to perform an operation. For example, modify the form field, add items to the form field, or remove it.

Getting the Form Field ID When Adding a New Form Field

When you use a method to create a form field, it returns an integer. This value is the newly created form field’s ID. Save this value in a variable for future use, as shown in the code example below:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Set the origin of the coordinate system.
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
// Set the measurement unit to centimeters.
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
// Select the first PDF page.
gdpicturePDF.SelectPage(1);
// Add the text form field and save its ID in a variable.
int fieldID = gdpicturePDF.AddTextFormField(1, 2, 8, 3, "TextField1", "",
    true, "", 12, 0, 0, 255);
// Write the form field ID to the console.
Console.WriteLine("Form field ID: {0}", fieldID);
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Set the origin of the coordinate system.
    gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
    ' Set the measurement unit to centimeters.
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
    ' Select the first PDF page.
    gdpicturePDF.SelectPage(1)
    ' Add the text form field and save its ID as an integer.
    Dim fieldID As Integer = gdpicturePDF.AddTextFormField(1, 2, 8, 3, "TextField1", "",
        True, "", 12, 0, 0, 255)
    ' Write the form field ID to the console.
    Console.WriteLine("Form field ID: {0}", fieldID)
End Using
Used Methods

Related Topics

Getting the Form Field ID by Looping Through All Fields

To get the form field ID when you have multiple form fields in a PDF document, follow these steps:

  1. Get the total number of form fields in the PDF document with the GetFormFieldsCount method.

  2. Loop through all the form fields.

  3. Get the ID of each form field with the GetFormFieldId method.

  4. Use the field ID in a method that retrieves information about a specified form field, such as the following:

To get the form field ID of all text box fields, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\output.pdf");
// Get the form field count.
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)
    {
        Console.WriteLine("Text form field ID: {0}", fieldID);
    }
}
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\output.pdf")
    ' Get the form field count.
    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 a text box.
        If gdpicturePDF.GetFormFieldType(fieldID) Is PdfFormFieldType.PdfFormFieldTypeText Then
            Console.WriteLine("Text form field ID: {0}", fieldID)
        End If
    Next
End Using
Information

If there’s only one form field in a PDF, you can skip the loop and use the GetFormFieldId method with 0 as its parameter.