A signature confirms that the digital document originated from the signer and has not been tampered with during transit. The use of digital signatures eliminates the need for sending paper documents, and reduces the number of the documents that need to be printed, mailed, and stored, saving you time and money. In this article, you will learn how to digitally sign a Word document in C# and VB.NET using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Add a Digital Signature to Word in C#, VB.NET
The steps are as follows.
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Specify the path and the password of a .pfx certificate.
- Digitally sign the document while saving the document using Document.SaveToFile(string fileName, FileFormat fileFormat, string certificatePath, string securePassword) method. Here are some other methods that you can use to digitally sign a Word document.
- public void SaveToFile(string fileName, FileFormat fileFormat, byte[] certificateData, 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[] Document.Sign(Stream sourceStream, byte[] certificateData, string securePassword);
- public static byte[] Document.Sign(Stream sourceStream, string certificatePath, string securePassword);
- C#
- VB.NET
using Spire.Doc; namespace DigitallySignWord { class Program { static void Main(string[] args) { //Create a Document object Document doc = new Document(); //Load a Word file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //Specify the certificate path string certificatePath = "C:\\Users\\Administrator\\Desktop\\gary.pfx"; //Specify the password of the certificate string password = "e-iceblue"; //Digitally sign the document while saving it to a .docx file doc.SaveToFile("AddDigitalSignature.docx", FileFormat.Docx2013, certificatePath, password); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.