保护 PDF 文档
Contents
[
Hide
]
有时,开发人员需要处理加密的 PDF 文件。例如,他们需要使用用户密码和所有者密码来保护文档的安全,以免任何人都可以打开它们,或者想要限制文档内容是否可以打印或提取。
本文介绍如何在将电子表格保存到 PDF 时传入 PDF 安全选项。
Aspose.Cells API 提供Pdf安全选项处理 PDF 文件格式安全性的类。下面的示例代码描述了如何使用 Aspose.Cells for Java API 创建安全的 PDF 文件。
This file contains hidden or 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); |
如果电子表格包含公式,最好调用工作簿.calculateFormula() 就在将其呈现为 PDF 之前。这可确保重新计算公式相关值,并在 PDF 中呈现正确的值。