All docs
V21.2
21.2
21.1
20.2
20.1
The page you are viewing does not exist in version 20.1. This link will take you to the root page.
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

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.v21.2.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

View Example: How to add and validate a digital signature in a Word 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;
}

Run Demo: Word (RTF) Document Signature

Example: Sign an XLSX Document

View Example: How to Sign a Workbook and Validate the Signature

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;
}

Run Demo: Spreadsheet Document Signature

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

View Example: How to add and validate a digital signature in a Word 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

View Example: How to Sign a Workbook and Validate the Signature

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);
  }