DEV Community

loading...

Add or Remove Digital Signatures in Excel in Java

E-iceblue Product Family
Your Office Development Master
・2 min read

An Excel document signed with a digital signature means that the document has been marked as final and no more changes are wanted. If someone insists on doing it, the signature will be removed from the document. In this article, I am going to introduce how to programmatically add or remove digital signatures in Excel using Spire.XLS for Java.

Installing Spire.Xls.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.

<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.5.3</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Example 1. Add digital signature

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.digital.CertificateAndPrivateKey;

import java.util.Date;

public class AddSignature {

    public static void main(String[] args) throws Exception {

        //Get file paths
        String filePath = "C:\\Users\\Administrator\\Desktop\\Employees.xlsx";
        String certificatePath = "C:\\Users\\Administrator\\Desktop\\gary.pfx";

        //Create a Workbook object
        Workbook workbook=new Workbook();

        //Load the sample document
        workbook.loadFromFile(filePath);

        //Load certificate
        CertificateAndPrivateKey certificate = new CertificateAndPrivateKey(certificatePath,"e-iceblue");

        //Add digital signature to workbook
        workbook.addDigitalSignature(certificate, "This file cannot be edited.",new Date());

        //Save the document
        workbook.saveToFile("AddDigitalSignature.xlsx", ExcelVersion.Version2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Image1

Example 2. Remove digital signature

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class RemoveSignature {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook=new Workbook();

        //Load the sample document
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\AddDigitalSignature.xlsx");

        //Remove digital signatures from workbook
        workbook.removeAllDigitalSignatures();

        //Save the document
        workbook.saveToFile("RemoveSignature.xlsx", ExcelVersion.Version2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Image2

Discussion (0)