C# .NET Add a Signature Field in PDF

To add a signature form field that’s later used for digital signing, follow these steps:

  1. Load the source document by passing its path to the LoadFromFile method of the GdPicturePDF object.

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

  3. Set the measurement unit used to specify dimensions.

  4. Add the signature field with the AddSignatureFormField method. It uses the following parameters:

    • Left — The horizontal distance between the closest point to the defined origin point.

    • Top — The vertical distance between the closest point to the defined origin point.

    • Width — The width of the signature field’s bounding box.

    • Height — The height of the signature field’s bounding box.

    • FieldName — The name of the signature field used for identifying form fields.

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

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

To add a signature form field that’s later used for digital signing, use the following code:

using GdPicturePDF gdPicturePDF = new GdPicturePDF();
gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Select the second page of the PDF document.
gdPicturePDF.SelectPage(1);
// Set the measurement unit to centimeters.
gdPicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
// Set the origin of the coordinate system to the bottom-right corner.
gdPicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomRight);
// Add the `Signature1` signature field.
int signatureField = gdPicturePDF.AddSignatureFormField(2, 5, 5, 2, "Signature1");
// Set the font type to Freestyle Script.
string fontName = gdPicturePDF.AddTrueTypeFont("Freestyle Script", false, false, false);
gdPicturePDF.SetFormFieldFontResName(signatureField, fontName);
// Set the font size to 48 points.
gdPicturePDF.SetFormFieldFontSize(signatureField, 48);
// Set the font color to blue.
gdPicturePDF.SetFormFieldFontColor(signatureField, 0, 0, 255);
// Set the text alignment to center.
gdPicturePDF.SetFormFieldTextAlignment(signatureField, TextAlignment.TextAlignmentCenter);
// Save the PDF document to a file.
gdPicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdPicturePDF As GdPicturePDF = New GdPicturePDF()
    gdPicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Select the second page of the PDF document.
    gdPicturePDF.SelectPage(1)
    ' Set the measurement unit to centimeters.
    gdPicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
    ' Set the origin of the coordinate system to the bottom-right corner.
    gdPicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomRight)
    ' Add the `Signature1` signature field.
    Dim signatureField As Integer = gdPicturePDF.AddSignatureFormField(2, 5, 5, 2, "Signature1")
    ' Set the font type to Freestyle Script.
    Dim fontName As String = gdPicturePDF.AddTrueTypeFont("Freestyle Script", False, False, False)
    gdPicturePDF.SetFormFieldFontResName(signatureField, fontName)
    ' Set the font size to 48 points.
    gdPicturePDF.SetFormFieldFontSize(signatureField, 48)
    ' Set the font color to blue.
    gdPicturePDF.SetFormFieldFontColor(signatureField, 0, 0, 255)
    ' Set the text alignment to center.
    gdPicturePDF.SetFormFieldTextAlignment(signatureField, TextAlignment.TextAlignmentCenter)
    ' Save the PDF document to a file.
    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, or 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.

Define the FontResName parameter with one of the following methods:

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

  • AddTrueTypeFont — This method allows you to load 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 text is set to bold font.

    • Italic — A Boolean value that specifies if the text is set to italic font.

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

  • AddTrueTypeFontFromFile — This method allows you to load 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 texts significantly increase the file size.

Font Size

To specify the text size, use the SetFormFieldFontSize method. It requires 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 requires the following parameters:

  • FieldId — A unique form field ID.

  • Color defined 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 requires the following parameters:

  • FieldId — A unique form field ID.

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