DigitalSignatureUtilSign Method (String, String, CertificateHolder, SignOptions)

Signs source document using given CertificateHolder and SignOptions with digital signature and writes signed document to destination file.

Document should be either Doc or Docx.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public static void Sign(
	string srcFileName,
	string dstFileName,
	CertificateHolder certHolder,
	SignOptions signOptions
)

Parameters

srcFileName
Type: SystemString
The file name of the document to sign.
dstFileName
Type: SystemString
The file name of the signed document output.
certHolder
Type: Aspose.WordsCertificateHolder
CertificateHolder object with certificate that used to sign file. The certificate in holder MUST contain private keys and have the X509KeyStorageFlags.Exportable flag set.
signOptions
Type: Aspose.WordsSignOptions
SignOptions object with various signing options.
Examples
Demonstrates how to add new signature line to the document and sign it with personal signature using SignatureLineId.
[Description("WORDSNET-16868")]
public static void Sign()
{
    string signPersonName = "Ron Williams";
    string srcDocumentPath = MyDir + "Document.docx";
    string dstDocumentPath = ArtifactsDir + "SignDocumentCustom.Sign.docx";
    string certificatePath = MyDir + "morzal.pfx";
    string certificatePassword = "aw";

    // We need to create simple list with test signers for this example
    CreateSignPersonData();
    Console.WriteLine("Test data successfully added!");

    // Get sign person object by name of the person who must sign a document
    // This an example, in real use case you would return an object from a database
    SignPersonTestClass signPersonInfo =
        (from c in gSignPersonList where c.Name == signPersonName select c).FirstOrDefault();

    if (signPersonInfo != null)
    {
        SignDocument(srcDocumentPath, dstDocumentPath, signPersonInfo, certificatePath, certificatePassword);
        Console.WriteLine("Document successfully signed!");
    }
    else
    {
        Console.WriteLine("Sign person does not exist, please check your parameters.");
    }

    // Now do something with a signed document, for example, save it to your database
    // Use 'new Document(dstDocumentPath)' for loading a signed document
}

/// <summary>
/// Signs the document obtained at the source location and saves it to the specified destination.
/// </summary>
private static void SignDocument(string srcDocumentPath, string dstDocumentPath,
    SignPersonTestClass signPersonInfo, string certificatePath, string certificatePassword)
{
    // Create new document instance based on a test file that we need to sign
    Document document = new Document(srcDocumentPath);
    DocumentBuilder builder = new DocumentBuilder(document);

    // Add info about responsible person who sign a document
    SignatureLineOptions signatureLineOptions =
        new SignatureLineOptions { Signer = signPersonInfo.Name, SignerTitle = signPersonInfo.Position };

    // Add signature line for responsible person who sign a document
    SignatureLine signatureLine = builder.InsertSignatureLine(signatureLineOptions).SignatureLine;
    signatureLine.Id = signPersonInfo.PersonId;

    // Save a document with line signatures into temporary file for future signing
    builder.Document.Save(dstDocumentPath);

    // Create holder of certificate instance based on your personal certificate
    // This is the test certificate generated for this example
    CertificateHolder certificateHolder = CertificateHolder.Create(certificatePath, certificatePassword);

    // Link our signature line with personal signature
    SignOptions signOptions = new SignOptions
    {
        SignatureLineId = signPersonInfo.PersonId,
        SignatureLineImage = signPersonInfo.Image
    };

    // Sign a document which contains signature line with personal certificate
    DigitalSignatureUtil.Sign(dstDocumentPath, dstDocumentPath, certificateHolder, signOptions);
}

#if NETFRAMEWORK
/// <summary>
/// Converting image file to bytes array.
/// </summary>
private static byte[] ImageToByteArray(Image imageIn)
{
    using (MemoryStream ms = new MemoryStream())
    {
        imageIn.Save(ms, ImageFormat.Png);
        return ms.ToArray();
    }
}
#endif

/// <summary>
/// Create test data that contains info about sing persons
/// </summary>
private static void CreateSignPersonData()
{
    gSignPersonList = new List<SignPersonTestClass>
    {
        #if NETFRAMEWORK
        new SignPersonTestClass(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer",
            ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg"))),
        #else
        new SignPersonTestClass(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer", 
            SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes),
        #endif

        #if NETFRAMEWORK
        new SignPersonTestClass(Guid.NewGuid(), "Stephen Morse", "Head of Compliance",
            ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg")))
        #else
        new SignPersonTestClass(Guid.NewGuid(), "Stephen Morse", "Head of Compliance", 
            SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes)
        #endif
    };
}

private static List<SignPersonTestClass> gSignPersonList;
See Also