Wednesday, 7 April 2021

[Java] Add and Delete Digital Signature in an Excel worksheet

To add a digital signature in an Excel worksheet can authenticate the document, which shows that it belongs to you or your company. This tutorial will demonstrate how to add a digital signature to an Excel worksheet using Java codes, and then shows how to delete it.

It's worth mentioning that I used a free third-party library called Free Spire.XLS for Java. Before running codes, you need to add Spire.Xls.jar in the library to IDEA. Download the package from the link and manually add it to IDEA, or directly reference it using the following Maven configurations.

<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.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Add Digital Signature

import com.spire.xls.digital.CertificateAndPrivateKey;
import java.util.Date;

public class AddDigitalSignature {
public static void main(String[] args) throws Exception {
//Load the sample document
Workbook workbook=new Workbook();
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

//Add digital signature
CertificateAndPrivateKey cap = new CertificateAndPrivateKey("C:\\Users\\Test1\\Desktop\\gary.pfx","e-iceblue");
workbook.addDigitalSignature(cap, "e-iceblue",new Date());

//Save the document
String result="output/AddDigitalSignature.xlsx";
workbook.saveToFile(result,ExcelVersion.Version2013);
}
}
Output

Delete Digital Signature

import com.spire.xls.*;

public class DeleteDigitalSignature {
public static void main(String[] args) {
//Load the sample document
Workbook workbook=new Workbook();
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\AddDigitalSignature.xlsx");

//Remove digital signature
workbook.removeAllDigitalSignatures();

//Save the document
String result="output/RemoveDigitalSignature.xlsx";
workbook.saveToFile(result,ExcelVersion.Version2013);
}
}

Output





No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...