Use Office File API to Sign Word and Excel Files
- 8 minutes to read
Office File API supports signatures in the following document formats:
- Microsoft Word
- Open XML (DOCX, DOTX, DOTM, DOCM)
- 97-2003 (DOC, DOT)
- Microsoft Excel
- Open XML (XLSX, XLTX, XLSM)
- 97-2003 binary file (XLS, XLT)
- Microsoft PowerPoint
- PPTX, PPT
Utilize the DocumentSigner class methods to apply, validate and remove digital signatures. To use DocumentSigner, add a reference to the DevExpress.Docs.v24.1.dll assembly. If you use this assembly in production code, a license for the DevExpress Office File API or DevExpress Universal Subscription is required. For pricing information, refer to the following page: DevExpress Subscription Plans.
Tip
Refer to the following article for information on PDF signatures: Sign PDF Documents.
Sign Documents
Use the DocumentSigner.Sign method to sign documents and save the result. You can use XAdES-BES and XAdES-T signatures with X.509 certificates. The SignatureOptions class object allows you to specify validation data (certificate, hash algorithm, timestamp, etc.). Pass the SignatureInfo object as the method’s signatureInfo parameter to define the signer information.
The options and signatureInfo parameters cannot be null.
Example: Sign a DOCX Document
using DevExpress.Office.DigitalSignatures;
using System;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
static void Main(string[] args)
{
//Apply a signature:
string documentOutput = "Document_signed.docx";
SignDocument("Template.docx", documentOutput);
}
static void SignDocument(string path, string output)
{
DocumentSigner documentSigner = new DocumentSigner();
SignatureOptions signatureOptions = CreateSignatureOptions();
SignatureInfo signatureInfo = CreateSignatureInfo();
documentSigner.Sign(path, output, signatureOptions, signatureInfo);
Process.Start(output);
}
//Specify a signature certificate and digest method:
static SignatureOptions CreateSignatureOptions()
{
X509Certificate2 certificate =
new X509Certificate2("Certificate/SignDemo.pfx", "dxdemo");
SignatureOptions options = new SignatureOptions();
options.Certificate = certificate;
options.DigestMethod = HashAlgorithmType.SHA256;
//In this example, certificate validation is skipped
options.SignatureFlags &= ~SignatureFlags.ValidateCertificate;
options.CertificateKeyUsageFlags = X509KeyUsageFlags.None;
X509ChainPolicy policy = new X509ChainPolicy();
policy.RevocationMode = X509RevocationMode.NoCheck;
policy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
policy.VerificationFlags |= X509VerificationFlags.AllowUnknownCertificateAuthority |
X509VerificationFlags.IgnoreCertificateAuthorityRevocationUnknown;
options.CertificatePolicy = policy;
options.TimestampCertificatePolicy = policy;
return options;
}
//Specify signer information:
static SignatureInfo CreateSignatureInfo()
{
SignatureInfo signatureInfo = new SignatureInfo();
signatureInfo.CommitmentType = CommitmentType.ProofOfApproval;
signatureInfo.Time = DateTime.UtcNow;
signatureInfo.ClaimedRoles.Clear();
signatureInfo.ClaimedRoles.Add("Sales Representative");
signatureInfo.Comments = "Demo Digital Signature";
return signatureInfo;
}
Example: Sign an XLSX Document
using DevExpress.Office.DigitalSignatures;
using System;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
static void Main(string[] args)
{
//Apply a signature:
string workbookOutput = "Workbook_signed.xlsx";
SignDocument("Template.xlsx", workbookOutput);
}
static void SignDocument(string path, string output)
{
DocumentSigner documentSigner = new DocumentSigner();
SignatureOptions signatureOptions = CreateSignatureOptions();
SignatureInfo signatureInfo = CreateSignatureInfo();
documentSigner.Sign(path, output, signatureOptions, signatureInfo);
Process.Start(output);
}
//Specify a signature certificate and digest method:
static SignatureOptions CreateSignatureOptions()
{
X509Certificate2 certificate =
new X509Certificate2("Certificate/SignDemo.pfx", "dxdemo");
SignatureOptions options = new SignatureOptions();
options.Certificate = certificate;
options.DigestMethod = HashAlgorithmType.SHA256;
//In this example, certificate validation is skipped
options.SignatureFlags &= ~SignatureFlags.ValidateCertificate;
options.CertificateKeyUsageFlags = X509KeyUsageFlags.None;
X509ChainPolicy policy = new X509ChainPolicy();
policy.RevocationMode = X509RevocationMode.NoCheck;
policy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
policy.VerificationFlags |= X509VerificationFlags.AllowUnknownCertificateAuthority |
X509VerificationFlags.IgnoreCertificateAuthorityRevocationUnknown;
options.CertificatePolicy = policy;
options.TimestampCertificatePolicy = policy;
return options;
}
//Specify signer information:
static SignatureInfo CreateSignatureInfo()
{
SignatureInfo signatureInfo = new SignatureInfo();
signatureInfo.CommitmentType = CommitmentType.ProofOfApproval;
signatureInfo.Time = DateTime.UtcNow;
signatureInfo.ClaimedRoles.Clear();
signatureInfo.ClaimedRoles.Add("Sales Representative");
signatureInfo.Comments = "Demo Digital Signature";
return signatureInfo;
}
Validate Signatures
Utilize the DocumentSigner.Validate method to validate a signature in the specified file or stream.
The SignatureValidationOptions object allows you to specify signature validation options (signature or timestamp certificate policy, decryption password, etc.). Use the SignatureValidationOptions.ValidationFlags property to exclude validation steps.
Example: Validate Signatures in a DOCX Document
static void Main(string[] args)
{
ValidateSignature("Template_signed.docx");
}
private static void ValidateSignature(string path)
{
DocumentSigner validator = new DocumentSigner();
//Specify validation options
//In this example, certificate validation is skipped
SignatureValidationOptions validationOptions = new SignatureValidationOptions();
validationOptions.ValidationFlags = ~ValidationFlags.ValidateSignatureCertificate & ~ValidationFlags.ValidateTimestampCertificate;
//Validate the signature:
PackageSignatureValidation signatureValidation = validator.Validate(path, validationOptions);
AnalyzeValidationResult(signatureValidation);
}
Example: Validate Signatures in an XLSX Document
using DevExpress.Office.DigitalSignatures;
using System.Diagnostics;
using System.Net;
static void Main(string[] args)
{
ValidateSignature("Template_signed.xlsx");
}
private static void ValidateSignature(string path)
{
DocumentSigner validator = new DocumentSigner();
//In this example, certificate validation is skipped
SignatureValidationOptions validationOptions = new SignatureValidationOptions();
validationOptions.ValidationFlags = ~ValidationFlags.ValidateSignatureCertificate & ~ValidationFlags.ValidateTimestampCertificate;
//Validate the signature:
PackageSignatureValidation signatureValidation = validator.Validate(path, validationOptions);
AnalyzeValidationResult(signatureValidation);
}
Analyze Validation Result
The Validate method returns the PackageSignatureValidation instance that contains validation information. Check the PackageSignatureValidation.Result and PackageSignatureValidation.ResultMessage properties to determine whether the signature is valid.
Note
Make sure that the signature certificate is registered on your machine. Otherwise, the signature is invalid.
If the document is not signed, the PackageSignatureValidation.Result property returns SignaturesNotFound.
If the document to be validated is encrypted, specify the SignatureValidationOptions.DecryptionPassword property to decrypt the document. Otherwise, the Result property returns PackageSignatureValidationResult.PackageOpenFailed.
If the PackageSignatureValidation.Result property returns Invalid or PartiallyValid, check the PackageSignatureValidation.Items property to obtain a list of items with detailed validation information. The number of SignatureValidationInfo objects in the list is equal to the number of signatures.
The table below lists API used to obtain information:
Property | Description |
---|---|
SignatureValidationInfo.PassedChecks | Returns verification types the signature passed. |
SignatureValidationInfo.PassedCheckDetails | Obtains information about passed verifications. |
SignatureValidationInfo.FailedChecks | Returns verification types the signature did not pass. |
SignatureValidationInfo.FailedCheckDetails | Retrieves information about failed verifications. |
SignatureValidationInfo.CheckDetails | Gets information about all verifications. |
The code sample below analyzes the validation result and shows information in the console:
private static void AnalyzeValidationResult(PackageSignatureValidation signatureValidation)
{
string validationMessage = signatureValidation.ResultMessage;
//Check validation result and show information in the console:
switch (signatureValidation.Result)
{
case PackageSignatureValidationResult.Valid:
Console.WriteLine(validationMessage); Console.ReadKey();
Process.Start(output);
break;
case PackageSignatureValidationResult.SignaturesNotFound:
Console.WriteLine(validationMessage);
break;
case PackageSignatureValidationResult.Invalid:
case PackageSignatureValidationResult.PartiallyValid:
var failedCheckDetails = signatureValidation.Items[0].FailedCheckDetails;
Console.WriteLine(validationMessage);
int i = 1;
foreach (SignatureCheckResult checkResult in failedCheckDetails)
{
Console.WriteLine(String.Format("Validation details {0}: \r\n" +
"{1} failed, Info: {2} \r\n", i, checkResult.CheckType, checkResult.Info));
i++;
}
Console.ReadKey();
break;
}
}
Remove Signatures
Utilize the DocumentSigner.RemoveSignatures method to clear signatures from a document and save the result.
static void Main(string[] args)
{
ClearSignatures("Template_signed.docx", @"D:/Template_cleared.docx");
ClearSignatures("Template_signed.xlsx", @"D:/Template_cleared.xlsx");
}
private static void ClearSignatures(string path, string output)
{
DocumentSigner remover = new DocumentSigner();
remover.RemoveSignatures(path, output);
Process.Start(output);
}