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);
}
}
Uday DodiyaPosted Sep 16, 2022, 11:12 AM
Spire.Office is Paid or Free ?