Secure PDF Documents
Contents
[
Hide
]
Sometimes, developers need to work with encrypted PDF files. For example:
- Secure the documents with owner and user passwords so not just anyone can open it.
- Set restrictions or permissions to the document after the document is opened. e.g. 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 provides PdfSecurityOptions for working with security. You can set owner and user passwords while saving to PDF. The owner password or user password will be required to open the encrypted PDF document for viewing.
- The user password can be null or empty string, in this case no password will be required from the user when opening the PDF document.
- Opening the PDF document with the correct owner password allows full access(without any access restrictions specified) to the document.
- Opening the PDF document with the correct user password (or opening a document that does not have a user password) allows limited access as the permissions specified.
The sample code below describes how to secure PDFs with Aspose.Cells.
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open an Excel file | |
Workbook workbook = new Workbook(dataDir+ "input.xlsx"); | |
// Instantiate PDFSaveOptions to manage security attributes | |
PdfSaveOptions saveOption = new PdfSaveOptions(); | |
saveOption.SecurityOptions = new Aspose.Cells.Rendering.PdfSecurity.PdfSecurityOptions(); | |
// Set the user password | |
saveOption.SecurityOptions.UserPassword = "user"; | |
// Set the owner password | |
saveOption.SecurityOptions.OwnerPassword = "owner"; | |
// Disable extracting content permission | |
saveOption.SecurityOptions.ExtractContentPermission = false; | |
// Disable print permission | |
saveOption.SecurityOptions.PrintPermission = false; | |
// Save the PDF document with encrypted settings | |
workbook.Save(dataDir+ "securepdf_test.out.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.