News Category

Java: Digitally Sign a Word Document

2022-07-07 06:01:00 Written by  support iceblue
Rate this item
(0 votes)

A digital signature is a specific type of signature that is backed by a digital certificate, providing proof of your identity. Digital signatures are used to protect documents in a wide range of fields such as life insurance, invoices, rental agreements, and employment contracts and so on. This article will show you how to digitally sign a Word document in Java using Spire.Doc for Java.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.4.6</version>
    </dependency>
</dependencies>
    

Add a Digital Signature to a Word Document in Java

The follows are the steps to digitally sign a Word document using Spire.Doc for Java.

  • 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. There 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);
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class DigitallySignWordDocument {

    public 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\\certificate.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.Docx_2013, certificatePath, password);
    }
}

Java: Digitally Sign a Word Document

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.

Additional Info

  • tutorial_title:
Last modified on Monday, 12 June 2023 02:15