PDF Form Field Items in C# .NET

Combo box and list form fields can contain items. GdPicture.NET allows you to create, edit, and reorganize these field items.

Adding a Form Field Item

To add an item to a form field, follow these steps:

  1. Create a GdPicturePDF object.

  2. Load the PDF file with the LoadFromFile method.

  3. Set the origin of the coordinate system with the SetOrigin method. This method requires the PDFOrigin enumeration.

  4. Set the measurement unit with the SetMeasurementUnit method to specify the form field’s dimensions and position. This method uses the PdfMeasurementUnit enumeration.

  5. Select the PDF page where you want to place the form field with the SelectPage method.

  6. Add either a combo box or a list form field. Save its field ID to a variable.

  7. Add field items with the AddFormFieldItem method. It uses the following parameters:

    • FieldID — The form field ID where the item is added.

    • Text — The text displayed in the form field item.

    • Value — Optional: The export value used when exporting the form field’s data.

  8. Optional: Set the default selected item with the SetFormFieldValue method. It uses the following parameters:

    • FieldId — The form field ID.

    • Value — The item’s Text property.

  9. Optional: Set the CommitOnSelChange flag to true with the SetFormFieldItemCommit method. Doing so enables applications to perform an action once an item is selected, without having to exit the form field. This method works only for combo boxes and list form fields with the Multiselect parameter set to false.

  10. Optional: Use the SetFormFieldItemEdit method to allow adding custom items inside the form field. This method is applicable only for a combo box form field.

To add three items to a combo box, use the following code:

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 second PDF page.
gdpicturePDF.SelectPage(2);
string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);
// Add a combo box form field.
int fieldID = gdpicturePDF.AddComboFormField(2, 2, 5, 0.5f, "combobox1", fontResName, 12, 0, 0, 200, false);
// Add three items to the combobox.
gdpicturePDF.AddFormFieldItem(fieldID, "Option 1", "1");
gdpicturePDF.AddFormFieldItem(fieldID, "Option 2", "2");
gdpicturePDF.AddFormFieldItem(fieldID, "Option 3", "3");
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Private Sub SurroundingSub()
    Dim 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 second PDF page.
    gdpicturePDF.SelectPage(2)
    Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica)
    ' Add a combo box form field.
    Dim fieldID As Integer = gdpicturePDF.AddComboFormField(2, 2, 5, 0.5F, "combobox1", fontResName, 12, 0, 0, 200, False)
    ' Add three items to the combobox.
    gdpicturePDF.AddFormFieldItem(fieldID, "Option 1", "1")
    gdpicturePDF.AddFormFieldItem(fieldID, "Option 2", "2")
    gdpicturePDF.AddFormFieldItem(fieldID, "Option 3", "3")
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Sub
Used Methods

Related Topics

Deleting a Form Field Item

To delete a form field item, use the DeleteFormFieldItem method. It requires the following parameters:

  • FieldId — The form field ID.

  • ItemIdx — The position index of the item.

To delete a form field item, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Get form field count.
int fieldCount = gdpicturePDF.GetFormFieldsCount();
// Loop through all form fields.
for(int i = 0; i < fieldCount; i++)
{
    // Get form field ID.
    int fieldID = gdpicturePDF.GetFormFieldId(i);
    // Check if the current form field has the "combobox1" title.
    if (gdpicturePDF.GetFormFieldTitle(fieldID) == "combobox1")
    {
        // Get the form field item count.
        int itemCount = gdpicturePDF.GetFormFieldItemCount(fieldID);
        // Delete the last form field item.
        gdpicturePDF.DeleteFormFieldItem(fieldID, itemCount - 1);
    }
}
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Get form field count.
    Dim fieldCount As Integer = gdpicturePDF.GetFormFieldsCount()
    ' Loop through all form fields.
    For i = 0 To fieldCount - 1
        ' Get form field ID.
        Dim fieldID As Integer = gdpicturePDF.GetFormFieldId(i)
        ' Check if the current form field has the "combobox1" title.
        If gdpicturePDF.GetFormFieldTitle(fieldID) Is "combobox1" Then
            ' Get the form field item count.
            Dim itemCount As Integer = gdpicturePDF.GetFormFieldItemCount(fieldID)
            ' Delete the last form field item.
            gdpicturePDF.DeleteFormFieldItem(fieldID, itemCount - 1)
        End If
    Next
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics