Fill and Sign PDFs in C# .NET

To sign a PDF with a digital signature, follow these steps:

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

  2. Set the certificate contained in the digital ID stored in the PCKS#12 file or a Stream object with the SetSignatureCertificateFromP12 method.

  3. Recommended: Specify the signature information with the SetSignatureInfo method. It requires the following string parameters:

    • Name — The person or authority signing the document.

    • Reason — The reason for signing.

    • Location — The physical location where the signing takes place.

    • ContactInfo — The contact information of the signer.

  4. Get the signature’s location with the SetSignaturePosFromPlaceHolder method. It requires either the signature ID or the signature name as its parameter.

  5. Configure the text with the SetSignatureText method. It uses the following parameters:

    • Text — The text to be displayed. If this parameter is an empty string, the text displays the data from the SetSignatureInfo method.

    • FontResName — The font name used. Use either the AddTrueTypeFont method or the AddStandardFont method. If this parameter is an empty string, the predefined font is used.

    • FontSize — The text font size in points.

    • Font color — The font color can be set in one of the following ways: a Color object, a set of four byte parameters (Cyan, Magenta, Yellow, Black), or a 32-bit signed integer.

    • AlignHorz — The horizontal text alignment within the bounding box. Use the TextAlignment enumeration.

    • AlignVert — The vertical text alignment within the bounding box. Use the TextAlignment enumeration.

    • TextDecorationStyle — Optional: A member of the PdfTextDecorationStyle enumeration. It specifies if the text has an overline, strikethrough, or underline.

    • ShowText — A Boolean value that specifies whether the text is displayed.

Information

To use the text settings defined in the signature field, refer to the guide on getting text settings from a signature field.

  1. Apply the signature with the ApplySignature method using the specified settings, and save the PDF document to a file or a Stream object. It requires the following parameters:

    • OutputFileName or OutputStream — Either the path to the output file or a Stream object.

    • SignatureMode — A member of the PdfSignatureMode enumeration that specifies the electronic signature technology used in the signing process.

    • Linearization — Specifies if the output PDF document is linearized.

To sign a signature field with a digital signature in the form of an image, follow the steps in the Signing a Digital Signature with an Image section of the guide on adding digital signatures. To specify the signature’s position, use the SetSignaturePosFromPlaceHolder method instead of SetSignaturePos.

Information

You can only add digital signatures to non-encrypted documents.

To sign a signature form field in a PDF with a digital signature, use the following code:

using GdPicturePDF gdPicturePDF = new GdPicturePDF();
gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Set the certificate from a file.
gdPicturePDF.SetSignatureCertificateFromP12(@"C:\temp\certificate.pfx", "pspdfkit");
// Set the signature position from the signature field.
gdPicturePDF.SetSignaturePosFromPlaceHolder("Signature1");
// Configure the signature text.
gdPicturePDF.SetSignatureText("John Smith", "", 12, GdPictureColor.Red,
    TextAlignment.TextAlignmentCenter, TextAlignment.TextAlignmentCenter, true);
// Apply the signature and save the PDF document.
gdPicturePDF.ApplySignature(@"C:\temp\output.pdf",
    PdfSignatureMode.PdfSignatureModeAdobePPKMS, false);
Using gdPicturePDF As GdPicturePDF = New GdPicturePDF()
    gdPicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Set the certificate from a file.
    gdPicturePDF.SetSignatureCertificateFromP12("C:\temp\certificate.pfx", "pspdfkit")
    ' Set the signature position from the signature field.
    gdPicturePDF.SetSignaturePosFromPlaceHolder("Signature1")
    ' Configure the signature text.
    gdPicturePDF.SetSignatureText("John Smith", "", 12, GdPictureColor.Red,
        TextAlignment.TextAlignmentCenter, TextAlignment.TextAlignmentCenter, True)
    ' Apply the signature and save the PDF document.
    gdPicturePDF.ApplySignature("C:\temp\output.pdf",
        PdfSignatureMode.PdfSignatureModeAdobePPKMS, False)
End Using
Used Methods

Related Topics