Skip to main content

Add and Delete Digital Signature in Excel with Java


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.

  1. <repositories>    
  2.         <repository>    
  3.             <id>com.e-iceblue</id>    
  4.             <name>e-iceblue</name>    
  5.             <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>    
  6.         </repository>    
  7. </repositories>    
  8. <dependencies>    
  9.     <dependency>    
  10.         <groupId>e-iceblue</groupId>    
  11.         <artifactId>spire.xls</artifactId>    
  12.         <version>4.4.6</version>    
  13.     </dependency>    
  14. </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 loadFromFilemethod with the workbook object, finally call the addDigitalSignature and the saveToFilemethods to add digital signature and save the result file.

  1. import com.spire.xls.ExcelVersion;  
  2. import com.spire.xls.Workbook;  
  3. import com.spire.xls.digital.CertificateAndPrivateKey;  
  4.   
  5. import java.util.Date;  
  6.   
  7. public class AddDigitalSignature {  
  8.     public static void main(String []args) throws Exception {  
  9.         //Load an Excel file  
  10.         Workbook workbook=new Workbook();  
  11.         workbook.loadFromFile("Sample.xlsx");  
  12.   
  13.         //Add digital signature  
  14.         CertificateAndPrivateKey cap = new CertificateAndPrivateKey("test.pfx","123456");  
  15.         workbook.addDigitalSignature(cap, "123456",new Date());  
  16.   
  17.         //Save the result file  
  18.         workbook.saveToFile("AddDigitalSignature.xlsx", ExcelVersion.Version2013);  
  19.     }  
  20. }  

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.

  1. import com.spire.xls.ExcelVersion;  
  2. import com.spire.xls.Workbook;  
  3.   
  4. public class DeleteDigitalSignature {  
  5.     public static void main(String []args){  
  6.         //Load an Excel file  
  7.         Workbook workbook=new Workbook();  
  8.         workbook.loadFromFile("AddDigitalSignature.xlsx");  
  9.   
  10.         //Remove digital signature  
  11.         workbook.removeAllDigitalSignatures();  
  12.   
  13.         //Save the result file  
  14.         workbook.saveToFile("RemoveDigitalSignature.xlsx", ExcelVersion.Version2013);  
  15.     }  
  16. }  

The result file:



 

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name> ...

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>     ...

Remove Duplicate Rows in Excel in C# and VB.NET

When an Excel file contains a huge amount of records, there might be duplicate records as well. In this blog, I am going to show you how to remove the duplicate rows in an Excel file programmatically in C# and VB.NET. The library I used: Free Spire.XLS for .NET Free Spire.XLS for .NET is a feature-rich Excel API offered by E-iceblue. It can be easily integrated in your .NET (C#, VB.NET, ASP.NET, .NET Core) applications to create, read, edit, convert and print Excel files without using Microsoft Office. Before coding, you need to get Free Spire.XLS for .NET by installing it via NuGet or downloading it via the official website . C# Code using  Spire.Xls;   using  System.Linq;      namespace  RemoveDuplicateRows   {        class  Program       {            static   void  Main( string [] args) ...