使用Java在PowerPoint中添加、验证或删除数字签名


数字签名是数字信息(如电子邮件消息或电子文档)上的电子加密认证戳。它可以帮助收件人验证文档内容自签名后是否已更改。如果进行任何更改,签名将立即失效。在本文中,我将演示如何使用 Java 在 PowerPoint 中添加、验证或删除数字签名。
 
为了处理 PowerPoint 文档,我将使用Spire.Presentation for Java API。API 的 jar 可以从官方网站下载或从 Maven 安装,方法是将以下代码添加到基于 maven 的项目的 pom.xml 文件中。

<repositories>   
    <repository>   
        <id>com.e-iceblue</id>   
        <name>e-iceblue</name>   
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>   
    </repository>   
</repositories>   
<dependencies>   
    <dependency>   
        <groupId> e-iceblue </groupId>   
        <artifactId>spire.presentation</artifactId>   
        <version>5.1.7</version>   
    </dependency>   
</dependencies>

在Java中为PowerPoint添加数字签名
以下是向PowerPoint文档添加数字签名的步骤。

  1. 创建一个Presentation类的实例。
  2. 使用Presentation.loadFromFile(String filePath)方法加载PowerPoint文档。
  3. 使用Presentation.addDigitalSignature(String pfxPath, String password, String comments, Date signTime)方法向文档添加数字签名。
  4. 使用Presentation.saveToFile(String filePath, FileFormat fileFormat)方法保存结果文档。

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
 
import java.util.Date;
 
public class AddSignature {
    public static void main(String []args) throws Exception {
        //Create a Presentation instance
        Presentation presentation = new Presentation();
       
//Load the PowerPoint document
        presentation.loadFromFile(
"Sample.pptx");
 
       
//Add a digital signature
        String pfxPath =
"Certificate.pfx";
        String password =
"123456"; //The password of the .pfx file
        String comment =
"Modification is not allowed";
        presentation.addDigitalSignature(pfxPath, password, comment, new Date());
 
       
//Save the result to file
        presentation.saveToFile(
"AddSignature.pptx", FileFormat.PPTX_2013);
    }
}

 
用Java验证PowerPoint中的数字签名
以下是在PowerPoint文档中验证数字签名的步骤。

  • 创建一个Presentation类的实例。
  • 使用Presentation.loadFromFile(String filePath)方法加载一个PowerPoint文档。
  • 使用Presentation.isDigitallySigned()方法检测该文档是否有数字签名。

import com.spire.presentation.Presentation;
 
public class VerifyIfPPTisDigitallySigned {
    public static void main(String []args) throws Exception {
        //Create a Presentation instance
        Presentation presentation = new Presentation();
       
//Load a PowerPoint document
        presentation.loadFromFile(
"AddSignature.pptx");
 
       
//Verify if the document is digitally signed
        if (presentation.isDigitallySigned()) {
            System.out.println(
"This document is digitally signed");
        } else {
            System.out.println(
"This document is not digitally signed");
        }
    }
}

 
在Java中从PowerPoint中删除数字签名
以下是删除PowerPoint文档中所有数字签名的步骤。
  1. 创建一个Presentation类的实例。
  2. 使用Presentation.loadFromFile(String filePath)方法加载一个PowerPoint文档。
  3. 使用Presentation.isDigitallySigned()方法检测该文档是否有数字签名。如果结果为真,使用Presentation.removeAllDigitalSignatures()方法移除所有数字签名。
  4. 使用Presentation.saveToFile(String filePath, FileFormat fileFormat)方法保存结果文档。

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
 
public class RemoveSignature {
    public static void main(String []args) throws Exception {
        //Create a Presentation instance
        Presentation presentation = new Presentation();
 
       
//Load the PowerPoint document
        presentation.loadFromFile(
"AddSignature.pptx");
 
       
//Determine if the document is digitally signed
        if (presentation.isDigitallySigned())
        {
           
//Remove all digital signatures
            presentation.removeAllDigitalSignatures();
        }
 
       
//Save the result document
        presentation.saveToFile(
"RemoveSignature.pptx", FileFormat.PPTX_2013);
    }
}