向已签名的Excel文件添加数字签名

可能的使用场景

Aspose.Cells提供 Workbook.addDigitalSignature(DigitalSignatureCollection digitalSignatureCollection) 方法,您可以利用它向已经签名的Excel文件添加数字签名。

对已签名的 Excel 文件添加数字签名

以下示例代码解释了如何使用 Workbook.addDigitalSignature(DigitalSignatureCollection digitalSignatureCollection) 方法向已经签名的Excel文件添加数字签名。请检查本代码中使用的 示例Excel文件。此文件已经进行了数字签名。请检查代码生成的 输出Excel文件。我们在此代码中使用了名为 AsposeTest.pfx 的演示证书,密码为 aspose。屏幕截图显示了执行后代码对示例Excel文件的影响。

todo:image_alt_text

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Certificate file and its password
String certFileName = "AsposeTest.pfx";
String password = "aspose";
// Load the workbook which is already digitally signed to add new digital signature
Workbook workbook = new Workbook(srcDir + "sampleDigitallySignedByCells.xlsx");
// Create the digital signature collection
DigitalSignatureCollection dsCollection = new DigitalSignatureCollection();
// Create new digital signature and add it in digital signature collection
// ------------------------------------------------------------
// --------------Begin::creating signature---------------------
// Load the certificate into an instance of InputStream
InputStream inStream = new FileInputStream(srcDir + certFileName);
// Create an instance of KeyStore with PKCS12 cryptography
java.security.KeyStore inputKeyStore = java.security.KeyStore.getInstance("PKCS12");
// Use the KeyStore.load method to load the certificate stream and its password
inputKeyStore.load(inStream, password.toCharArray());
// Create an instance of DigitalSignature and pass the instance of KeyStore, password, comments and time
DigitalSignature signature = new DigitalSignature(inputKeyStore, password,
"Aspose.Cells added new digital signature in existing digitally signed workbook.",
com.aspose.cells.DateTime.getNow());
dsCollection.add(signature);
// ------------------------------------------------------------
// --------------End::creating signature-----------------------
// Add digital signature collection inside the workbook
workbook.addDigitalSignature(dsCollection);
// Save the workbook and dispose it.
workbook.save(outDir + "outputDigitallySignedByCells.xlsx");
workbook.dispose();