Secure PDF Documents
Contents
[
Hide
]
Sometimes, developers need to work with encrypted PDF files. For example, they need to secure documents with user and owner passwords so not just anyone can open them, or want to restrict whether the document content can be printed or extracted.
This article explains how to pass in PDF security options when saving spreadsheets to PDF.
Aspose.Cells APIs provide the PdfSecurityOptions class for working with the security of PDF file format. The sample code below describes how to create secured PDF files with Aspose.Cells for Java API.
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); |
If the spreadsheet contains formulas, it is best to call Workbook.calculateFormula() just before rendering it to PDF. This ensures that formula dependent values are recalculated, and the correct values are rendered in the PDF.