Discuss this help topic in SecureBlackbox Forum

Sign document

To sign OpenXPS (Open XML Paper Specification, XPS) document using XML digital signature handler you need to

  1. create an instance of TElOfficeOpenXPSSignatureHandler class and add it to the document using AddSignature() method of the document object
  2. specify, what information should be signed, by adding document parts, core properties, signature origin 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. Signature origin can be added using AddSignatureOrigin() method of the handler.
  3. configure signature-releated properties of the handler, such as signature definition or XAdES info
  4. call Sign() method to sign, after that close the document to flush changes

C#:


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

        TElOfficeOpenXPSSignatureHandler OpenXPSSigHandler = new TElOfficeOpenXPSSignatureHandler();
        Document.AddSignature(OpenXPSSigHandler, true);

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

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

    OpenXPSSigHandler := TElOfficeOpenXPSSignatureHandler.Create(nil);
    Document.AddSignature(OpenXPSSigHandler, True);

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

How To articles about XPS documents

Discuss this help topic in SecureBlackbox Forum