This C# sample signs a PDF document with the .NET Digital Signature Library (SignLib). The certificate can come from a PFX (PKCS#12) file, from the bytes of a PFX file stored in a database or a secret store, or from the Windows certificate store, where the drivers of smart cards and USB tokens publish their certificates.
How to sign a PDF in C#
- Create a
PdfSignatureobject with the serial number of the library and load the document withLoadPdfDocument(a file path, a byte array or a URL). - Set the signing certificate in
DigitalSignatureCertificate, loaded withDigitalCertificate.LoadCertificate. - Optionally set the reason, the location, the position of the visible signature and the certification level (
CertifySignature). - Call
ApplyDigitalSignature: it returns the signed PDF document as a byte array.
C# code: certification signature
The first signature of the document certifies it: the changes allowed after signing are limited to form filling, annotations and other signatures.
/// <summary>
/// Gets the certificate used to sign the documents. A certificate can be loaded from various sources: choose one.
/// </summary>
static X509Certificate2 LoadSigningCertificate()
{
// 1. Load the certificate (with its private key) from a PFX or P12 file.
return DigitalCertificate.LoadCertificate(SampleFile("cert.pfx"), CertificatePassword);
// 2. Create a certificate on the fly (see the sample "Create PFX Digital Certificates"):
// return CreateSelfSignedCertificate();
// 3. Select a certificate from the Windows certificate store (Current User). Smart cards and USB tokens
// usually publish their certificates in this store (start - run - certmgr.msc). If the certificate of a
// smart card is not visible in the Windows store, the library cannot use it with this method.
// The user selects the certificate from a dialog window:
// return DigitalCertificate.LoadCertificate(false, string.Empty, "Select Certificate", "Select the certificate for digital signature");
}
/// <summary>
/// Signs a PDF file and certifies the document. A certification signature must be the first signature of a document.
/// </summary>
static void SignPdfWithCertificationSignature(string unsignedPdfFile, string signedPdfFile)
{
PdfSignature pdfSignature = new PdfSignature(LibrarySerialNumber);
pdfSignature.LoadPdfDocument(unsignedPdfFile);
pdfSignature.DigitalSignatureCertificate = LoadSigningCertificate();
// The reason and the location of the signature are displayed in the signature appearance and in the signature properties.
pdfSignature.SigningReason = "I approve this document";
pdfSignature.SigningLocation = "Accounting department";
// The visible signature is placed in one of the corners of the first page.
pdfSignature.SignaturePosition = SignaturePosition.TopLeft;
// The document is certified: the further changes of the document are restricted (here, only form filling,
// annotations and other signatures are allowed). A document can have a single certification signature.
pdfSignature.CertifySignature = CertifyMethod.AnnotationsAndFormFilling;
// Sign the document and save the signed PDF file.
File.WriteAllBytes(signedPdfFile, pdfSignature.ApplyDigitalSignature());
Console.WriteLine("The first PDF signature was created." + Environment.NewLine);
}
Sign a PDF in memory with a PFX certificate
The document and the PFX file can be byte arrays, so no file is written on the disk: the way for web applications and services. This method is from the sample PDF Digital Signature with PFX Certificate.
/// <summary>
/// Signs a PDF document with a certificate loaded from a PFX file and returns the signed document.
/// </summary>
static byte[] SignPdf(byte[] unsignedPdf, string pfxFile, string pfxPassword)
{
PdfSignature pdfSignature = new PdfSignature(LibrarySerialNumber);
// The PDF document is loaded from a byte array (it can also be loaded from a file path or from a URL).
pdfSignature.LoadPdfDocument(unsignedPdf);
// 1. The certificate is loaded from a PFX file...
pdfSignature.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate(pfxFile, pfxPassword);
// 2. ...or from the bytes of a PFX file (for example read from a database or from a secret store).
// pdfSignature.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate(File.ReadAllBytes(pfxFile), pfxPassword);
pdfSignature.SigningReason = "I approve this document";
pdfSignature.SigningLocation = "Accounting department";
pdfSignature.SignaturePosition = SignaturePosition.TopLeft;
// The hash algorithm of the signature (SHA-256 is the default).
pdfSignature.HashAlgorithm = SignLib.HashAlgorithm.SHA256;
return pdfSignature.ApplyDigitalSignature();
}
The signature is a PKCS#7 signature by default, recognized by every PDF reader. Set SignatureStandard = PdfSignatureStandard.Pades for a PAdES signature (ETSI EN 319 142), add a time-stamp with TimeStamping.ServerUrl, and see the visible signature options or PAdES-LTA for signatures that can be validated years later.
Run the full sample. The complete Visual Studio project PDF Digital Signature is included in the library download, in the folder NET Sample Projects (.NET 8, .NET 9, .NET 10 and .NET 11) and in NET Framework Sample Projects (.NET Framework 4.6.2), with the test files and a test certificate. Without a serial number the library runs in demo mode. Download the library and the samples · User manual
Frequently asked questions
Can I sign a PDF with a smart card or a USB token?
Yes. The drivers of most smart cards and USB tokens publish the certificate in the Windows certificate store, and DigitalCertificate.LoadCertificate selects it from there, with a selection window or without user intervention. A token that is not visible in the Windows store can be used through PKCS#11.
Does the signed PDF open in Adobe Reader?
Yes. The signatures are standard PDF signatures (PKCS#7 or PAdES) and Adobe Reader shows them in the signature panel. Adobe trusts the signature when the certificate is issued by a certification authority that Adobe trusts.