Fill and sign PDFs in C# .NET
To sign a PDF with a digital signature, follow the steps below:
- Load the source document with a signature field by passing its path to the
LoadFromFilemethod of theGdPicturePDFobject. - Set the certificate contained in the digital ID stored in the
PCKS#12file or aStreamobject with theSetSignatureCertificateFromP12method. - Recommended: Specify the signature information with the
SetSignatureInfomethod. 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
SetSignaturePosFromPlaceHoldermethod. It requires either the signature ID or the signature name as its parameter. - Configure the text with the
SetSignatureTextmethod. It uses the following parameters:
Text— The text to be displayed. If this parameter is an empty string, the text displays the data from theSetSignatureInfomethod.FontResName— The font name used. Use either theAddTrueTypeFontmethod or theAddStandardFontmethod. 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
Colorobject, 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 theTextAlignmentenumeration.AlignVert— The vertical text alignment within the bounding box. Use theTextAlignmentenumeration.TextDecorationStyle— Optional: A member of thePdfTextDecorationStyleenumeration. 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
ApplySignaturemethod using the specified settings, and save the PDF document to a file or aStreamobject. It requires the following parameters:OutputFileNameorOutputStream— Either the path to the output file or aStreamobject.SignatureMode— A member of thePdfSignatureModeenumeration 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", "nutrient");// 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", "nutrient") ' 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