Create a Fillable PDF Form in C# .NET

To add a fillable form to a PDF, 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 using the SelectPage method.

  6. Add a form field. By default, all form field types are fillable.

  7. Optional: Specify the font configuration. For more information, refer to the Text Settings in Form Fields section.

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

Information

All methods that add a new form field return an integer value, which is the field ID. Recommended: Save the field ID to a variable so that you can later reference the field.

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 as an integer.
int formID = gdpicturePDF.AddTextFormField(1, 2, 8, 3, "TextField1", "", true, "", 12, 0, 0, 255);
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
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 formID As Integer = gdpicturePDF.AddTextFormField(1, 2, 8, 3, "TextField1", "", True, "", 12, 0, 0, 255)
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics

Text Settings in Form Fields

This section explains how to configure text settings such as font type, color, and size.

Font Type

To specify the text font, use the SetFormFieldFontResName method. It requires the following parameters:

  • FieldId — A unique form field ID.

  • FontResName — A string value of the text font.

You can define the FontResName parameter with one of the following methods:

  • AddStandardFont — This method requires the PdfStandardFont enumeration as its parameter.

  • AddTrueTypeFont — This allows loading any font format used in your operating system and uses the following parameters:

    • FontName — The name of the font as a string value.

    • Bold — A Boolean value that specifies if the loaded font is bold.

    • Italic — A Boolean value that specifies if the loaded font is italic.

    • Embedded — A Boolean value that specifies if the loaded font is embedded.

  • AddTrueTypeFontFromFile — This allows loading a TrueType or OpenType font from a file.

  • AddTrueTypeFontU — This method adds a TrueType or OpenType font with Unicode character support.

  • AddTrueTypeFontFromFileU — This method adds a TrueType or OpenType font from a file with Unicode character support.

Information

Embedded text significantly increases the file size.

Font Size

To specify the text size, use the SetFormFieldFontSize method. It uses the following parameters:

  • FieldId — A unique form field ID.

  • FontSize — The text size expressed in points (pt).

Font Color

To specify the text color, use the SetFormFieldFontColor method. It uses the following parameters:

  • FieldId — A unique form field ID.

  • Color definition — You can define the color in one of the following ways:

    • A set of three Byte parameters that specify the RGB color model.

    • A set of four Byte parameters that specify the CMYK color model.

    • A GdPictureColor object.

Text Alignment

To specify the text alignment, use the SetFormFieldTextAlignment method. It uses the following parameters:

  • FieldId — A unique form field ID.

  • TextAlign — A member of the TextAlignment enumeration that specifies the text alignment.