Wednesday, 29 May 2019 07:35

Add Digital Signature to Word in C#

Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to add digital signature to a Word document using Spire.Doc.

Spire.Doc provides us the following methods to add digital signature to Word document:

  • public void SaveToFile(string fileName, FileFormat fileFormat, byte[] certificateData, string securePassword);
  • public void SaveToFile(string fileName, FileFormat fileFormat, string certificatePath, string securePassword);
  • public void SaveToStream(Stream stream, FileFormat fileFormat, byte[] certificateData, string securePassword);
  • public void SaveToStream(Stream stream, FileFormat fileFormat, string certificatePath, string securePassword);
  • public static byte[] Sign(Stream sourceStream, byte[] certificateData, string securePassword);
  • public static byte[] Sign(Stream sourceStream, string certificatePath, string securePassword);

In the following example, we'll see how to add digital signature to a Word document and save the result to file using the SaveToFile method with Document object.

//Load the Word document
Document doc = new Document("sample.docx");
//Sign the document with certificate and save to file
doc.SaveToFile("AddDigitalSignature.docx", FileFormat.Docx2013, "gary.pfx", "e-iceblue");

We can also add digital signature to a Word document and save the result to stream using the SaveToStream method with Document object.

//Load the Word document
Document doc = new Document("sample.docx");
//Create a FileStream
FileStream fs = new FileStream();
//Sign the document with certificate and save to stream
doc.SaveToStream(fs, FileFormat.Docx2013, "gary.pfx", "e-iceblue");
fs.Flush();

The following example shows how to add digital signature to a Word document using the Sign method with Document class.

//Read the Word document into a FileStream
FileStream fs = File.OpenRead("sample.docx");
//Sign the document using Sign method with Document class
byte[] result = Document.Sign(fs, "gary.pfx", "e-iceblue");
File.WriteAllBytes("AddDigitalSignature.docx", result);
fs.Flush();

The output document:

Add Digital Signature to Word in C#

Additional Info

  • tutorial_title: