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.


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

        //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 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

        //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

        //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 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

        //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));

        //Set the graphics mode

        //Set the signature content
        signature.setDate(new java.util.Date());
        signature.setReason("Final version");

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

        //Set the document permission

        //Save to file