安全的 PDF 文档
Contents
[
Hide
]
有时,开发人员需要处理加密的PDF文件。例如:
- 通过所有者密码和用户密码保护文档,以防止任何人都能打开它。
- 在打开文档之后,设置文档的限制或权限。例如,限制文档内容是否可以打印或提取。
本文解释了在将电子表格保存为PDF时如何传递PDF安全选项。
Aspose.Cells提供了PdfSecurityOptions以处理安全性。您可以在保存为PDF时设置所有者密码和用户密码。打开加密的PDF文档以查看时将需要所有者密码或用户密码。
- 用户密码可以为null或空字符串,在这种情况下,用户打开PDF文档时将不需要密码。
- 使用正确的所有者密码打开PDF文档允许对文档进行完全访问(未指定任何访问限制)。
- 使用正确的用户密码打开PDF文档(或打开没有用户密码的文档)允许进行有限访问,如权限所述。
下面的示例代码描述如何使用 Aspose.Cells for Java API 创建安全的PDF文件。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SecurePDFDocuments.class); | |
// Open an Excel file | |
Workbook workbook = new Workbook(dataDir + "input.xlsx"); | |
// Instantiate PDFSaveOptions to manage security attributes | |
PdfSaveOptions saveOption = new PdfSaveOptions(); | |
saveOption.setSecurityOptions(new PdfSecurityOptions()); | |
// Set the user password | |
saveOption.getSecurityOptions().setUserPassword("user"); | |
// Set the owner password | |
saveOption.getSecurityOptions().setOwnerPassword("owner"); | |
// Disable extracting content permission | |
saveOption.getSecurityOptions().setExtractContentPermission(false); | |
// Disable print permission | |
saveOption.getSecurityOptions().setPrintPermission(false); | |
// Save the PDF document with encrypted settings | |
workbook.save(dataDir + "securepdf_test.pdf", saveOption); |
如果电子表格中包含公式,最好在渲染为 PDF 之前调用 Workbook.calculateFormula()。这将确保重新计算公式依赖的值,并在 PDF 中呈现正确的值。