Fill and Sign PDFs in C# .NET
To sign a PDF with a digital signature, follow these steps:
-
Load the source document with a signature field by passing its path to the
LoadFromFile
method of theGdPicturePDF
object. -
Set the certificate contained in the digital ID stored in the
PCKS#12
file or aStream
object with theSetSignatureCertificateFromP12
method. -
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.
-
-
Get the signature’s location with the
SetSignaturePosFromPlaceHolder
method. It requires either the signature ID or the signature name as its parameter. -
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 theSetSignatureInfo
method. -
FontResName
— The font name used. Use either theAddTrueTypeFont
method or theAddStandardFont
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 theTextAlignment
enumeration. -
AlignVert
— The vertical text alignment within the bounding box. Use theTextAlignment
enumeration. -
TextDecorationStyle
— Optional: A member of thePdfTextDecorationStyle
enumeration. It specifies if the text has an overline, strikethrough, or underline. -
ShowText
— A Boolean value that specifies whether the text is displayed.
-
To use the text settings defined in the signature field, refer to the guide on getting text settings from a signature field.
-
Apply the signature with the
ApplySignature
method using the specified settings, and save the PDF document to a file or aStream
object. It requires the following parameters:-
OutputFileName
orOutputStream
— Either the path to the output file or aStream
object. -
SignatureMode
— A member of thePdfSignatureMode
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
.
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