DocumentSigner.Sign(Stream, Stream, SignatureOptions, SignatureInfo) Method
Signs a file with the specified signature and signature options.
Namespace: DevExpress.Office.DigitalSignatures
Assembly: DevExpress.Docs.v24.2.dll
NuGet Package: DevExpress.Document.Processor
Declaration
public void Sign(
Stream inputStream,
Stream outputStream,
SignatureOptions options,
SignatureInfo signatureInfo
)
Parameters
Name | Type | Description |
---|---|---|
inputStream | Stream | The stream that contains a file to sign. |
outputStream | Stream | The stream to which to save the file. |
options | SignatureOptions | An object that contains signature options. |
signatureInfo | SignatureInfo | An object that contains the signature information. |
Remarks
Use the Sign method to sign documents and save the result. You can use XAdES-BES and XAdES-T signatures with X.509 certificates. 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
The SignatureOptions class object allows you to specify validation data (certificate, hash algorithm, timestamp, etc.). Pass the SignatureInfo object to define the signer information.
The options and signatureInfo parameters cannot be null.
The code sample below signs and saves a Word and Excel document:
using DevExpress.Office.DigitalSignatures;
using System;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
static void Main(string[] args)
{
//Sign a workbook:
using (Stream stream = new FileStream("Template.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (Stream outputStream = new FileStream("Workbook_signed.xlsx", FileMode.Create, FileAccess.Write, FileShare.Read))
{
SignDocument(stream, outputStream);
}
}
//Sign a document:
using (Stream stream = new FileStream("Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (Stream outputStream = new FileStream("Template_signed.docx", FileMode.Create, FileAccess.Write, FileShare.Read))
{
SignDocument(stream, outputStream);
}
}
}
static void SignDocument(Stream path, Stream output)
{
DocumentSigner documentSigner = new DocumentSigner();
documentSigner.Sign(path, output,
CreateSignatureOptions(), CreateSignatureInfo());
}
//Specify a signature certificate and digest method:
static SignatureOptions CreateSignatureOptions()
{
X509Certificate2 certificate = new X509Certificate2("Certificate/SignDemo.pfx", "dxdemo");
Uri tsaServer = new Uri("https://freetsa.org/tsr");
SignatureOptions options = new SignatureOptions();
options.Certificate = certificate;
if (tsaServer != null)
options.TsaClient = new TsaClient(tsaServer, HashAlgorithmType.SHA256);
//In this example, certificate validation is skipped
options.SignatureFlags &= ~SignatureFlags.ValidateCertificate;
options.CertificateKeyUsageFlags = X509KeyUsageFlags.None;
options.DigestMethod = HashAlgorithmType.SHA256;
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;
}