How to Digitally Sign Word, Excel, PowerPoint and PDF Documents in Java

Digital signature is a KPI-based digital certificate, which is used to verify the identity of the signer and ensure that the electronically transmitted documents have not been forged or tampered with. As documents are increasingly paperless, more and more enterprises have embraced the speed and convenience of this type of signature. Knowing how to digitally sign MS office (Word, Excel, PowerPoint) documents and PDF files can be a real benefit for developers. This article will show you how to add digital signatures to these file formats by using Spire.Office for Java.

  • Digitally Sign a Word Document in Java
  • Digitally Sign an Excel Document in Java
  • Digitally Sign a PowerPoint Document in Java
  • Digitally Sign a PDF Document in Java

Add Spire.Office.jar as Dependency 

To begin with, you need to download Spire.Office for Java and add the Spire.Office.jar file in your Java application as a dependency. If you use Maven, you can easily import the jar file in your application using the following configurations.

<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.office</artifactId>
        <verson>7.6.4</version>
    </dependency>
</dependencies>

Digitally Sign a Word Document in Java

The Document.saveToFile() method has an overload that supports embedding a digital signature while saving the Word document to a Doc or Docx file. The following are the full steps.

  • Create a Document object, and load a Word file from disk.
  • Specify the path and the password of a PFX certificate. 
  • Digitally sign the Word file using the Document.saveToFile(string fileName, FileFormat fileFormat, string certificatePath, string securePassword) method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class DigitallySignWord {

    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 path of the certificate
        String certificatePath = "C:\\Users\\Administrator\\Desktop\\certificate.pfx";

        //Specify the password of the certificate
        String password = "psd123";

        //Digitally sign the document while saving it to a .docx file
        doc.saveToFile("AddDigitalSignature.docx", FileFormat.Docx_2013, certificatePath, password);
    }
}

Digitally Sign an Excel Document in Java

Signing an Excel document is quite simple, too. The steps are as follows.

  • Create a Workbook object, and load an Excel file from disk.
  • Load a PFX certificate while initializing the CertifitcateAndPrivateKey object.
  • Add the digital signature to the workbook using the Workbook.addDigitalSignature() method.
  • Save the workbook to another Excel file.
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.digital.CertificateAndPrivateKey;

import java.util.Date;

public class DigitallySignExcel {

    public static void main(String[] args) throws Exception {

        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Load an Excel file
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

        //Load a PFX certificate
        CertificateAndPrivateKey certificate = new CertificateAndPrivateKey("C:\\Users\\Administrator\\Desktop\\certificate.pfx", "psd123");

        //Add digital signature to workbook
        workbook.addDigitalSignature(certificate, "Final Version", new Date());

        //Save the workbook to file
        workbook.saveToFile("SignExcel.xlsx", ExcelVersion.Version2013);
    }
}

Digitally Sign a PowerPoint Document in Java

The way to sign a PowerPoint file is similar to that of signing an Excel document. The following are the steps.

  • Create a Presentation object, and load a PowerPoint file from disk.
  • Specify the path and the password of a PFX certificate.
  • Digitally sign the PowerPoint file using the Presentation.addDigitalSignature() method. 
  • Save the presentation to another PowerPoint file.
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

import java.util.Date;

public class DigitallySignPowerPoint {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Load a PowerPoint document
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");

        //Specify the path and the password of the PFX certificate
        String pfxPath = "C:\\Users\\Administrator\\Desktop\\certificate.pfx";
        String password = "psd123 ";

        //Specify the comment
        String comment = "Modification is not allowed";

        //Add a digital signature
        presentation.addDigitalSignature(pfxPath,password,comment,new Date());

        //Save the document to file
        presentation.saveToFile("SignPowerPoint.pptx", FileFormat.PPTX_2013);
    }
}

Digitally Sign a PDF Document in Java

The method of adding a visible digital signature to PDF is a bit different and complicated. Here come the detailed steps.

  • Create a PdfDocument object, and load a PDF file from disk. 
  • Load a PFX certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object and specify its position and size on the document.
  • Set the signature details including date, name, location, reason, signature image, and document permissions through the properties under the PdfSignature object.
  • Save the document to another PDF file.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.*;
import com.spire.pdf.security.*;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class DigitallySignPDF {

    public static void main(String[] args) {

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

        //Load a sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Load a pfx certificate
        PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\certificate.pfx", "psd123");

        //Create a PdfSignature object and specify its position and size
        PdfSignature signature = new PdfSignature(doc, doc.getPages().get(0), cert, "MySignature");
        Rectangle2D rect = new Rectangle2D.Float();
        rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 320, (float) doc.getPages().get(0).getActualSize().getHeight() - 140), new Dimension(270, 100));
        signature.setBounds(rect);

        //Set the graphics mode
        signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail);

        //Set the signature content
        signature.setNameLabel("Signer:");
        signature.setName("John");
        signature.setContactInfoLabel("ContactInfo:");
        signature.setContactInfo("514249");
        signature.setDateLabel("Date:");
        signature.setDate(new java.util.Date());
        signature.setLocationInfoLabel("Location:");
        signature.setLocationInfo("U.S.");
        signature.setReasonLabel("Reason:");
        signature.setReason("Final version");
        signature.setSignImageSource(PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\signImage.png"));

        //Set the signature font
        signature.setSignDetailsFont(new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Regular));

        //Set the document permission
        signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
        signature.setCertificated(true);

        //Save to file
        doc.saveToFile("SignPDF.pdf");
        doc.close();
    }
}