A digital signature is a type of electronic signature that
helps verify the authenticity of documents. In this blog, I will show you how
to sign an Excel file with digital signature and delete the digital signature from
the result Excel file programmatically in Java using Spire.XLS for Java API.
Add maven dependencies
Before coding, you need to add needed dependencies for
including Spire.XLS for Java into your Java project.
- <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</artifactId>
- <version>4.4.6</version>
- </dependency>
- </dependencies>
The latest Spire.XLS for Java version number can be found here.
Add digital signature
Spire.XLS for Java API provides a Workbook class that represents an Excel workbook. To add digital signature to an Excel file, you first need to create a workbook object, after that load the Excel file using the loadFromFile method with the workbook object, finally call the addDigitalSignature and the saveToFile methods to add digital signature and save the result file.
- import com.spire.xls.ExcelVersion;
- import com.spire.xls.Workbook;
- import com.spire.xls.digital.CertificateAndPrivateKey;
- import java.util.Date;
- public class AddDigitalSignature {
- public static void main(String []args) throws Exception {
- //Load an Excel file
- Workbook workbook=new Workbook();
- workbook.loadFromFile("Sample.xlsx");
- //Add digital signature
- CertificateAndPrivateKey cap = new CertificateAndPrivateKey("test.pfx","123456");
- workbook.addDigitalSignature(cap, "123456",new Date());
- //Save the result file
- workbook.saveToFile("AddDigitalSignature.xlsx", ExcelVersion.Version2013);
- }
- }
The result file:
Delete digital signature
The following example demonstrates how to remove the digital signature from the Excel file using the removeAllDigitalSignatures method in Workbook class.
- import com.spire.xls.ExcelVersion;
- import com.spire.xls.Workbook;
- public class DeleteDigitalSignature {
- public static void main(String []args){
- //Load an Excel file
- Workbook workbook=new Workbook();
- workbook.loadFromFile("AddDigitalSignature.xlsx");
- //Remove digital signature
- workbook.removeAllDigitalSignatures();
- //Save the result file
- workbook.saveToFile("RemoveDigitalSignature.xlsx", ExcelVersion.Version2013);
- }
- }
The result file:
Comments
Post a Comment