翻譯|使用教程|編輯:李顯亮|2020-04-14 09:31:30.453|閱讀 364 次
概述:在本系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何設置權限并加密和解密PDF文件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何設置權限并加密和解密PDF文件。
>>Aspose.PDF for .NET更新至最新版v20.4,歡迎下載體驗。
要在PDF文件上設置特權,請創建DocumentPrivilege類的對象,然后指定要應用于文檔的權限。定義特權后,將此對象作為參數傳遞給該Document對象的Encrypt(..)方法。以下代碼段顯示了如何設置PDF文件的權限。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures(); // Load a source PDF file using (Document document = new Document(dataDir + "input.pdf")) { // Instantiate Document Privileges object // Apply restrictions on all privileges DocumentPrivilege documentPrivilege = DocumentPrivilege.ForbidAll; // Only allow screen reading documentPrivilege.AllowScreenReaders = true; // Encrypt the file with User and Owner password. // Need to set the password, so that once the user views the file with user password, // Only screen reading option is enabled document.Encrypt("user", "owner", documentPrivilege, CryptoAlgorithm.AESx128, false); // Save updated document document.Save(dataDir + "SetPrivileges_out.pdf"); }
使用Document對象的Encrypt方法來加密PDF文件。您可以將用戶密碼,所有者密碼和權限傳遞給Encrypt方法。除此之外,還可以傳遞CryptoAlgorithm枚舉的任何值。該枚舉提供了加密算法和密鑰大小的不同組合。最后,使用Document對象的Save方法保存加密的PDF文件。
注意:
以下代碼段顯示了如何加密PDF文件。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures(); // Open document Document document = new Document(dataDir+ "Encrypt.pdf"); // Encrypt PDF document.Encrypt("user", "owner", 0, CryptoAlgorithm.RC4x128); dataDir = dataDir + "Encrypt_out.pdf"; // Save updated PDF document.Save(dataDir);
為了解密PDF文件,首先需要創建一個Document對象,然后使用所有者的密碼打開PDF。之后,需要調用Document對象的Decrypt方法。最后,使用Document對象的Save方法保存更新的PDF文件。以下代碼段顯示了如何解密PDF文件。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures(); // Open document Document document = new Document(dataDir+ "Decrypt.pdf", "password"); // Decrypt PDF document.Decrypt(); dataDir = dataDir + "Decrypt_out.pdf"; // Save updated PDF document.Save(dataDir);
如果要更改PDF文件的密碼,首先需要使用帶有Document對象的所有者密碼來打開PDF文件。之后,需要調用Document對象的 ChangePasswords方法。您需要將當前的所有者密碼以及新的用戶密碼和新的所有者密碼傳遞給此方法。最后,使用Document對象的Save方法保存更新的PDF文件。
以下代碼段顯示了如何更改PDF文件的密碼。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures(); // Open document Document document = new Document(dataDir+ "ChangePassword.pdf", "owner"); // Change password document.ChangePasswords("owner", "newuser", "newowner"); dataDir = dataDir + "ChangePassword_out.pdf"; // Save updated PDF document.Save(dataDir);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn