- Create and Verify Digital Signatures using .NET Digital Signature Library

- .NET Digital Signature Library Code Samples

- Download .NET Digital Signature Library with all samples

The OfficeSignature class of the Signature Library digitally signs Microsoft Word, Excel and PowerPoint documents (DOCX, XLSX, PPTX) with the native digital signatures of Microsoft Office, so the signatures are shown and verified by Word, Excel and PowerPoint. The examples below show how to sign a document with a time stamp and a visible signature line, how to let several people sign the same contract, and how to verify the signatures. The certificate can be loaded from a PFX file, from the Windows certificate store or from a smart card / USB token; RSA and ECDSA certificates and the SHA-256, SHA-384 and SHA-512 algorithms are supported.
using System;
using System.IO;
using SignLib; //OfficeSignature, HashAlgorithm
using SignLib.Certificates; //DigitalCertificate
using SignLib.Office; //OfficeSignatureLine, OfficeSignatureInfo
using SignLib.Xml; //XadesSignatureStandard, XadesCommitmentType
//digitally sign a Word document: XAdES-T signature (with a time stamp), signer details and a visible signature line
void DigitallySignOfficeDocument()
{
OfficeSignature cs = new OfficeSignature("");
//Load the signature certificate from a PFX or P12 file
//cs.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate("cert.pfx", "123456");
//Load the certificate from Microsoft Store.
//The smart card or USB token certificates are usually available on Microsoft Certificate Store (start - run - certmgr.msc).
//If the smart card certificate not appears on Microsoft Certificate Store it cannot be used by the library
cs.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate(false, string.Empty, "Select Certificate", "Select the certificate for digital signature");
//The smart card PIN dialog can be bypassed for some smart cards/USB Tokens (use it only for a smart card certificate).
//ATTENTION: This feature will NOT work for all available smart card/USB Tokens because of the drivers or other security measures.
//DigitalCertificate.SmartCardPin = "123456";
//the hash algorithm (SHA256, SHA384 or SHA512) and the signature level: XadesB (default), XadesT (time stamp) or XadesLT (long term)
cs.HashAlgorithm = HashAlgorithm.SHA256;
cs.SignatureStandard = XadesSignatureStandard.XadesT;
cs.TimeStamping.ServerUrl = new Uri("https://ca.signfiles.com/TSAServer.aspx");
//the signer details, shown by Office in the details of the signature
cs.CommitmentType = XadesCommitmentType.ProofOfApproval;
cs.SignatureComments = "Approval of the service agreement";
cs.SignerRole = "Manager";
//a visible signature: an Office signature line added at the end of the Word document (remove it for an invisible signature)
cs.SignatureLine = new OfficeSignatureLine();
cs.SignatureLine.SuggestedSigner = "John Smith";
cs.SignatureLine.SuggestedSignerTitle = "Manager";
cs.SignatureLine.SignatureImage = File.ReadAllBytes("signature.png"); //the handwritten signature (optional)
cs.SignatureLine.ShowSignDate = true;
//apply the digital signature to a DOCX, XLSX or PPTX Office document (the XLSX and PPTX documents get an invisible signature)
cs.ApplyDigitalSignature("Document.docx", "SignedDocument.docx");
Console.WriteLine("Number of signatures: " + cs.GetNumberOfSignatures("SignedDocument.docx"));
//verify the first signature (the index of the signatures starts at 0)
Console.WriteLine("Signature validity status: " + cs.VerifyDigitalSignature("SignedDocument.docx", 0));
}
//several signers: one signature line for each signer, added BEFORE the first signature; then each signer signs his own line
void SignWithSeveralSigners()
{
OfficeSignature document = new OfficeSignature("");
document.AddSignatureLine("Contract.docx", "ContractForSigning.docx", new OfficeSignatureLine { SuggestedSigner = "John Smith", SuggestedSignerTitle = "Manager" });
document.AddSignatureLine("ContractForSigning.docx", "ContractForSigning.docx", new OfficeSignatureLine { SuggestedSigner = "Mary Jones", SuggestedSignerTitle = "Accountant" });
//the first signer signs the first signature line that is not signed yet
OfficeSignature first = new OfficeSignature("");
first.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate("manager.pfx", "123456");
first.SignatureLine = new OfficeSignatureLine { Placement = OfficeSignatureLinePlacement.UseExisting };
first.ApplyDigitalSignature("ContractForSigning.docx", "ContractSigned.docx");
//the second signer signs the next signature line; the first signature remains valid
OfficeSignature second = new OfficeSignature("");
second.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate("accountant.pfx", "123456");
second.SignatureLine = new OfficeSignatureLine { Placement = OfficeSignatureLinePlacement.UseExisting };
second.ApplyDigitalSignature("ContractSigned.docx", "ContractSigned.docx");
Console.WriteLine("All signatures are valid: " + second.VerifyDigitalSignature("ContractSigned.docx"));
}
//verify all the signatures of an Office document and show their details
void VerifyOfficeDocument()
{
OfficeSignature verifier = new OfficeSignature("");
foreach (OfficeSignatureInfo info in verifier.GetSignatures("ContractSigned.docx"))
{
Console.WriteLine("Signature " + (info.Index + 1) + ": " + info.Status); //Success, ContentModified, ReferenceNotFound or InvalidSignature
Console.WriteLine(" Signer: " + info.SignerName + " (" + info.Certificate.Issuer + ")");
Console.WriteLine(" Signing time: " + info.SigningTime.Value.ToLocalTime());
Console.WriteLine(" Time stamp: " + (info.HasTimestamp ? info.TimestampTime.Value.ToLocalTime().ToString() : "no"));
Console.WriteLine(" Visible signature: " + info.IsVisible + ", algorithm: " + info.SignatureAlgorithm);
}
//the signature lines of the Word document and whether they are signed
foreach (OfficeSignatureLineInfo line in verifier.GetSignatureLines("ContractSigned.docx"))
Console.WriteLine(line);
}
The result:
Number of signatures: 1
Signature validity status: True
All signatures are valid: True
Signature 1: Success
Signer: John Smith (CN=John Smith, O=Demo Company, C=US)
Signing time: 10/2/2026 12:04:35 AM
Time stamp: no
Visible signature: True, algorithm: RSA-SHA256
Signature 2: Success
Signer: Mary Jones (CN=Mary Jones, O=Demo Company, C=US)
Signing time: 10/2/2026 12:04:35 AM
Time stamp: no
Visible signature: True, algorithm: RSA-SHA256
John Smith, Manager - signed
Mary Jones, Accountant - signed
Notes:
– The index of the signatures starts at 0, and VerifyDigitalSignature returns True when the signature is valid. VerifyDigitalSignature(file) checks all the signatures of the document.
– Add the signature lines of all the signers before the first signature: a new signature line changes the document and would invalidate the existing signatures. An invisible signature can be added at any time.
– Visible signatures (signature lines) are available for Word documents; Excel and PowerPoint documents receive an invisible signature.
– SHA-1 is not used for new signatures, and the XadesT and XadesLT levels require a time stamp server.
– Without a serial number the library works in demonstration mode and waits a few seconds before each signature.
See also: