This C# sample signs Word, Excel and PowerPoint documents (.docx, .xlsx, .pptx) with the digital signatures that Microsoft Office creates and shows (XAdES package signatures). Microsoft Office does not have to be installed.
What the sample shows
- Invisible signatures with the purpose, the commitment type, the signer role and the signing place, shown by Office in the signature details.
- SHA-256, SHA-384 and SHA-512, RSA and ECDSA certificates.
- Time-stamped signatures (XAdES-T) and signatures with long-term validation data (XAdES-LT).
- Visible signatures: signature lines of a Word document, for one or several signers.
- Signing in memory (byte arrays and streams), verification of all the signatures and detection of a changed document.
C# code: sign an Office document
/// <summary>
/// Signs an Office document (DOCX, XLSX or PPTX) with an invisible digital signature.
/// </summary>
static void SignOfficeDocument(string unsignedDocument, string signedDocument, X509Certificate2 certificate)
{
OfficeSignature officeSignature = new OfficeSignature(LibrarySerialNumber);
officeSignature.DigitalSignatureCertificate = certificate;
// The XAdES level of the signature: XadesB (the default, signing time and signing certificate), XadesT (with
// a time-stamp) or XadesLT (time-stamp and validation data). XadesLTA is not supported for Office documents.
officeSignature.SignatureStandard = XadesSignatureStandard.XadesB;
// The hash algorithm of the signature: SHA256 (the default), SHA384 or SHA512. SHA-1 is not supported.
officeSignature.HashAlgorithm = SignLib.HashAlgorithm.SHA256;
// The information shown by Office in the details of the signature.
// "Purpose for signing this document":
officeSignature.SignatureComments = "I approve this document";
// "Commitment type" (for example "Approved this document"):
officeSignature.CommitmentType = XadesCommitmentType.ProofOfApproval;
// The role (title) of the signer and the place where the document was signed:
officeSignature.SignerRole = "Accounting manager";
officeSignature.SignatureProductionPlace = new XadesProductionPlace
{
City = "Bucharest",
CountryName = "RO"
};
// An explicit signature policy can be set when a regulation requires it (by default the policy is implied, as for the
// signatures created by Office):
// officeSignature.SetSignaturePolicyInformation("2.16.724.1.3.1.1.2.1.9", policyDocumentHash, "2.16.840.1.101.3.4.2.1", "https://example.com/policy.pdf");
// The signatures already present in the document are kept. The output file can be the input file.
officeSignature.ApplyDigitalSignature(unsignedDocument, signedDocument);
Console.WriteLine("Signed: " + Path.GetFileName(signedDocument));
}
C# code: a Word signature line
/// <summary>
/// Signs a Word document with a visible signature: the signature line is added to the document and it is signed.
/// </summary>
static void SignWithNewSignatureLine(string unsignedDocument, string signedDocument)
{
OfficeSignature officeSignature = new OfficeSignature(LibrarySerialNumber);
officeSignature.DigitalSignatureCertificate = LoadSigningCertificate();
officeSignature.SignatureComments = "I approve this document";
// The visible signature. Without this object the signature is invisible.
officeSignature.SignatureLine = new OfficeSignatureLine
{
// UseExistingOrAddNew: the first unsigned signature line of the document is signed; when there is none, a new one
// is added at the end of the document. AddNew always adds a new line (the document must not be signed yet).
// UseExisting signs an existing line and it fails when there is no unsigned line.
Placement = OfficeSignatureLinePlacement.UseExistingOrAddNew,
// The details of a new signature line.
SuggestedSigner = "John Smith",
SuggestedSignerTitle = "Accounting manager",
SuggestedSignerEmail = "john.smith@example.com",
SigningInstructions = "Sign only after the verification of the document.",
Alignment = OfficeSignatureLineAlignment.Left,
Width = 192, // in points (1/72 inch)
Height = 96,
// The text written above the line (when it is empty, the name of the signing certificate is written),
// and the date of the signature.
SignatureText = "John Smith",
ShowSignDate = true
};
officeSignature.ApplyDigitalSignature(unsignedDocument, signedDocument);
Console.WriteLine("Signed: " + Path.GetFileName(signedDocument));
}
C# code: read and verify the signatures
/// <summary>
/// Verifies and displays the signatures of an Office document.
/// </summary>
static void PrintOfficeSignatures(string signedDocument)
{
OfficeSignature officeSignature = new OfficeSignature(LibrarySerialNumber);
Console.WriteLine("Document: " + Path.GetFileName(signedDocument));
Console.WriteLine("Number of signatures: " + officeSignature.GetNumberOfSignatures(signedDocument));
// True if all the signatures of the document are valid (the document was not changed after it was signed).
Console.WriteLine("All the signatures are valid: " + officeSignature.VerifyDigitalSignature(signedDocument));
// The signatures with their information and the result of their verification.
foreach (OfficeSignatureInfo signatureInfo in officeSignature.GetSignatures(signedDocument))
{
Console.WriteLine(" Signature " + signatureInfo.Index + " - " + signatureInfo.SignerName);
// Status: Success, ContentModified (the document was changed after it was signed), ReferenceNotFound
// (a signed part is missing) or InvalidSignature. The trust of the signing certificate is not verified.
Console.WriteLine(" Status: " + signatureInfo.Status + " (valid: " + signatureInfo.IsValid + ")");
Console.WriteLine(" Signature algorithm: " + signatureInfo.SignatureAlgorithm);
if (signatureInfo.SigningTime.HasValue)
Console.WriteLine(" Signing time: " + signatureInfo.SigningTime.Value.ToLocalTime());
if (!string.IsNullOrEmpty(signatureInfo.SignatureComments))
Console.WriteLine(" Purpose: " + signatureInfo.SignatureComments);
if (!string.IsNullOrEmpty(signatureInfo.CommitmentType))
Console.WriteLine(" Commitment type: " + signatureInfo.CommitmentType);
if (!string.IsNullOrEmpty(signatureInfo.SignerRole))
Console.WriteLine(" Signer role: " + signatureInfo.SignerRole);
Console.WriteLine(" Time-stamped: " + signatureInfo.HasTimestamp + (signatureInfo.HasTimestamp ? " (" + signatureInfo.TimestampTime.Value.ToLocalTime() + ")" : string.Empty));
Console.WriteLine(" Validation data (XAdES-LT): " + signatureInfo.HasValidationData);
Console.WriteLine(" Visible signature (signature line): " + signatureInfo.IsVisible + (signatureInfo.IsVisible ? ", line " + signatureInfo.SetupId : string.Empty));
}
// The signature lines of a Word document and if they are signed.
foreach (OfficeSignatureLineInfo signatureLine in officeSignature.GetSignatureLines(signedDocument))
Console.WriteLine(" Signature line: " + signatureLine);
Console.WriteLine();
}
The visible signatures (signature lines) work for Word documents. XAdES-LTA is not supported for Office documents.
Run the full sample. The complete Visual Studio project Office Digital Signature (DOCX, XLSX, PPTX) 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
Why does Office show my signature as not trusted?
Office verifies the trust of the signing certificate. A self-signed test certificate is not trusted; a certificate of a trusted certification authority is.
Can several people sign the same Word document?
Yes. Prepare signature lines for every signer, then every signer signs his own line; the existing signatures remain valid.