Java/ Add Digital Signatures to Word Documents

Java/ Add Digital Signatures to Word Documents

·

1 min read

A digital signature is an electronic encrypted authentication stamp applied to digital data. It is mainly used to confirm that the information originated from the signer and has not been changed. This article will demonstrate how to programmatically add a digital signature to a Word document using Free Spire.Doc for Java.

Import JAR Dependency

Method 1: Download the free API and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

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

Steps and Complete Sample Code

  1. Create a Document instance.
  2. Load a sample Word document using Document.loadFromFile() method.
  3. Specify the path and password of a certificate.
  4. Digitally sign the document and save it to another file using Document.saveToFile(java.lang.String fileName, FileFormat fileFormat, java.lang.String certificatePath, java.lang.String securePassword) method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class AddDigitalSignature {

    public static void main(String[] args) {

        //Create a Document object
        Document doc = new Document();

        //Load  the sample Word file
        doc.loadFromFile("Privacy Policy.docx");

        //Specify certificate path
        String certificatePath = "F:\\pfx\\output\\Eric.pfx";

        //Specify password of the certificate
        String certificatePassword = "111";

        //Sign the document with certificate and save to file
        doc.saveToFile("AddDigitalSignature.docx", FileFormat.Docx_2013, certificatePath, certificatePassword);
    }
}

WordSignature.jpg