Create a Fillable PDF Form in C# .NET
To add a fillable form to a PDF, follow these steps:
-
Create a
GdPicturePDF
object. -
Load the PDF file with the
LoadFromFile
method. -
Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePDFOrigin
enumeration. -
Set the measurement unit with the
SetMeasurementUnit
method to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnit
enumeration. -
Select the PDF page where you want to place the form field using the
SelectPage
method. -
Add a form field. By default, all form field types are fillable.
-
Optional: Specify the font configuration. For more information, refer to the Text Settings in Form Fields section.
-
Save the PDF document to a file with the
SaveToFile
method.
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
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 thePdfStandardFont
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.
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.
-
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 theTextAlignment
enumeration that specifies the text alignment.