Discuss this help topic in SecureBlackbox Forum

Sign document

To sign an OpenXML document using XML digital signature handler you need to

  1. load the document into an instance of TElOfficeOpenXMLDocument class
  2. create an instance of TElOfficeOpenXMLSignatureHandler class and add it to the document using AddSignature() method of the document object
  3. specify what information should be signed by adding document parts, core properties or the whole document. The whole document can be added using AddDocument() method of the handler. Core properties can be added using AddCoreProperties() method of the handler. Document parts can be added using AddPart() method of the handler.
  4. configure signature-releated properties of the handler, such as SignatureInfoV1, signature line or XAdES info
  5. call Sign() method to sign, after that close the document to flush changes.

C#:


void SignOpenXML(string sourceFilename, TElX509Certificate certificate)
{
    using (TElOfficeDocument Document = new TElOfficeDocument())
    {
        Document.Open(sourceFilename);
        if ((Document.DocumentFormat != TSBOfficeDocumentFormat.OpenXML) || !Document.Signable)
            throw new Exception("Cannot sign document using XML signature handler");

        TElOfficeOpenXMLSignatureHandler OpenXMLSigHandler = new TElOfficeOpenXMLSignatureHandler();
        Document.AddSignature(OpenXMLSigHandler, true);

        OpenXMLSigHandler.AddDocument();
        OpenXMLSigHandler.Sign(certificate);
    }
}
Delphi:

procedure SignOpenXML(const SourceFilename : string; Certificate : TElX509Certificate);
var
  Document : TElOfficeDocument;
  OpenXMLSigHandler : TElOfficeOpenXMLSignatureHandler;
begin
  Document := TElOfficeDocument.Create(nil);
  try
    Document.Open(SourceFilename);
    if (Document.DocumentFormat <> dfOpenXML) or not Document.Signable then
      raise Exception.Create('Cannot sign document using XML signature handler');

    OpenXMLSigHandler := TElOfficeOpenXMLSignatureHandler.Create(nil);
    Document.AddSignature(OpenXMLSigHandler, True);

    OpenXMLSigHandler.AddDocument();
    OpenXMLSigHandler.Sign(Certificate);
  finally
    FreeAndNil(Document);
  end;
end;

How To articles about MS Office OpenXML documents

Discuss this help topic in SecureBlackbox Forum