Edit PDF Form Fields in C# .NET

To edit a form field, use one of the methods starting with the SetFormField prefix. All these methods require the form field ID as their parameter. Any additional parameters depend on the method you want to use. These methods can change the following areas of a form field, among others:

  • Appearance

  • Content

  • Function properties

To edit a form field, follow these steps:

  1. Create a GdPicturePDF object.

  2. Load the PDF file with the LoadFromFile method.

  3. Determine the field ID. For more information, see the get field ID guide.

  4. Modify the form field with a method starting with the SetFormField prefix.

  5. Save the PDF document to a file with the SaveToFile method.

To change the font size and set the background of all text box fields in the document to light blue, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Get 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 font size of the form field.
        float fontSize = gdpicturePDF.GetFormFieldFontSize(fieldID);
        // Increase the form field's font size by two points.
        gdpicturePDF.SetFormFieldFontSize(fieldID, fontSize + 2);
        // Set the form field's background to light blue.
        gdpicturePDF.SetFormFieldBackgroundColor(fieldID, 173, 216, 230);
    }
}
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Get 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 a text box.
        If gdpicturePDF.GetFormFieldType(fieldID) Is PdfFormFieldType.PdfFormFieldTypeText Then
            ' Get the current font size of the form field.
            Dim fontSize As Single = gdpicturePDF.GetFormFieldFontSize(fieldID)
            ' Increase the form field's font size by two points.
            gdpicturePDF.SetFormFieldFontSize(fieldID, fontSize + 2)
            ' Set the form field's background to light blue.
            gdpicturePDF.SetFormFieldBackgroundColor(fieldID, 173, 216, 230)
        End If
    Next
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics