Office File API v20.2 – Digital Signatures in Office Documents

Office-Inspired Products
11 February 2021

As of our last major release (v20.2), our Office File API offers support for digital signatures. You can now electronically sign documents in the following 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

You can use XAdES-BES and XAdES-T signatures with X.509 certificates. Our API allows you to execute the following actions:

  • add an invisible digital signature and save the result;
  • validate signatures in a document;
  • remove all signatures.

Use the DocumentSigner class methods to manage digital signatures. To use DocumentSigner, add a reference to the DevExpress.Docs.v20.2.dll assembly. You must own an active subscription to either the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this assembly in production code. For more information on Office File API licensing, please email clientservcies@devexpress.com.

Sign Documents

Use the DocumentSigner.Sign method to sign documents and save the result. The SignatureOptions class object allows you to specify validation data (certificate, hash algorithm, timestamp, etc.). Pass the SignatureInfo object as the method’s parameter to define signer information.

The code sample below signs Word and Excel files:

using DevExpress.Office.DigitalSignatures;
using System;
using System.Security.Cryptography.X509Certificates;

static void Main(string[] args)
{
    // Sign Excel workbook:
    SignDocument("Template.xlsx", "Workbook_signed.xlsx");

    // Sign Word document:
    SignDocument("Template.docx", "Template_signed.docx");
}

static void SignDocument(string path, string output)
{
    DocumentSigner documentSigner = new DocumentSigner();
    SignatureOptions signatureOptions = CreateSignatureOptions();
    SignatureInfo signatureInfo = CreateSignatureInfo();
    // Apply signature:
    documentSigner.Sign(path, output, signatureOptions, signatureInfo);
}

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

If you open saved documents in Microsoft Word or Microsoft Excel, Microsoft displays the following messages:

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.

private static void ValidateSignature(string path)
{
  DocumentSigner validator = new DocumentSigner();

  // Specify validation options.
  // In this example, certificate validation is skipped:
  SignatureValidationOptions options = new SignatureValidationOptions();
  validationOptions.ValidationFlags = ~ValidationFlags.ValidateSignatureCertificate 
  	& ~ValidationFlags.ValidateTimestampCertificate;

  // Validate signature:
  PackageSignatureValidation signatureValidation = validator.Validate(path, options);
  AnalyzeValidationResult(signatureValidation);
}

The Validate method returns a PackageSignatureValidation instance that contains validation information. Check the Result and ResultMessage properties to determine whether the signature is valid. If the 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 equals the number of signatures.

The code sample below analyzes the validation result and displays information in the console:

private static void AnalyzeValidationResult(PackageSignatureValidation validation)
 {
    string validationMessage = validation.ResultMessage;
    PackageSignatureValidationResult validationResult = validation.Result;
  
   // Check validation result and show information in the console:
   if (validationResult == PackageSignatureValidationResult.Invalid 
   		|| validationResult == 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();
  }
}

Please refer to the following GitHub repositories for complete code sample projects:

Remove Signatures

Utilize the DocumentSigner.RemoveSignatures method to clear signatures from a document and save the result.

The code sample below removes signatures from Word and Excel files:

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

Your Feedback Matters

Should you have any questions/feedback regarding digital signatures, please comment here or submit a support ticket via the DevExpress Support Center. We will be happy to follow-up.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.