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 loadFromFile method with the workbook object, finally call the addDigitalSignature and the saveToFile methods 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>                   <url>http: //repo.e-iceblue.com/nexus/content/groups/public/</url>                </repository>       </repositories>       <dependencies>           <dependency>               <g

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();         workbook.loadFromFile( "Sample.xlsx" );         //Fit to page         workbook.getConverterSetting().setShee

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>                   <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>               </repository>       </repositories>       <dependencies>           <dependency>               <groupId>