经过数字签名的文档,能够使作者之外的人无法对其进行修改。在之前的文章中,我曾介绍过如何给 和 添加或删除数字签名。此篇文章将讲解给PowerPoint文档添加、检测以及删除数字签名的方法。
使用工具: Free Spire.Presentation for Java (可通过 E-iceblue中文官网 获取,或是在Java项目中创建Maven仓库,然后在pom.xml文件里引用以下代码。)
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
代码示例
添加数字签名
Free Spire.Presentation for Java中的Presentation.addDigitalSignature()方法支持给PowerPoint文档添加数字签名。操作步骤如下:
- 创建Presentation实例并使用Presentation.loadFromFile()方法加载PowerPoint示例文档;
- 使用Presentation.addDigitalSignature()方法添加数字签名;
- 调用Presentation.saveToFile()方法保存结果文档。
import com.spire.presentation.*;
import java.util.Date;
public class AddSignature {
public static void main(String[] args) throws Exception {
//加载PPT文档
Presentation ppt = new Presentation();
ppt.loadFromFile("sample.pptx");
//添加数字签名
ppt.addDigitalSignature("cer.pfx", "123654yes!","The file is confidential", new Date());
//保存文档
ppt.saveToFile("output/AddSignature.pptx",FileFormat.PPTX_2013);
ppt.dispose();
}
}
检测文档是否签名
Free Spire.Presentation for Java支持检测PowerPoint文档中是否设置有数字签名。以下是详细步骤:
- 创建Presentation实例并使用Presentation.loadFromFile()方法加载PowerPoint示例文档;
- 调用Presentation.isDigitallySigned()方法判断文档是否设置有数字签名。有则输出已签名,无则输出未签名。
import com.spire.presentation.*;
public class VerifySignature {
public static void main(String[] args) throws Exception {
//加载用于测试的PPT文档
Presentation ppt = new Presentation();
ppt.loadFromFile("sample.pptx");
//判定文档是否签名
boolean digitalSigned = ppt.isDigitallySigned();
if (digitalSigned == true)
{
System.out.println("已签名!");
}
else if(digitalSigned == false)
{
System.out.println("未签名,可添加签名。");
}
}
}
删除数字签名
Free Spire.Presentation for Java提供了Presentation.removeAllDigitalSignatures()方法来移除PowerPoint文档中的数字签名。以下是详细操作步骤:
- 创建Presentation实例并使用Presentation.loadFromFile()方法加载PowerPoint示例文档;
- 调用Presentation.isDigitallySigned()方法判断该文档是否含有数字签名,判断出有,则调用Presentation.removeAllDigitalSignatures()方法移除签名;
- 调用Presentation.saveToFile()方法保存结果文档。
import com.spire.presentation.*;
public class DeleteSignature {
public static void main(String[] args) throws Exception {
//加载PPT文档
Presentation ppt = new Presentation();
ppt.loadFromFile("AddSignature.pptx");
//判定文档是否签名
boolean digitalSigned = ppt.isDigitallySigned();
if (digitalSigned == true)
{
ppt.removeAllDigitalSignatures();//移除签名
}
//保存文档
ppt.saveToFile("output/RemoveSignature.pptx",FileFormat.PPTX_2013);
ppt.dispose();
}
}
程序运行后,保存的结果文档中不再有数字签名。