加密或解密 PDF 文件
在互聯網上共享機密文件時,PDF 加密是一項至關重要的任務。通過使用強密碼對 PDF 文件進行加密,可以保護文件數據不被未經授權方訪問。在某些情況下,可能還需要刪除密碼才能公開文檔。在本文中,您將學習如何使用 Spire.PDF for .NET 以編程方式加密或解密 PDF 文件。
- 使用密碼加密 PDF 文件
- 刪除密碼以解密 PDF 文件
安裝 Spire.PDF for .NET
首先,您需要將 Spire.PDF for.NET 軟件包中包含的 DLL 文件作為引用添加到您的 .NET 項目中。這些 DLL 文件既可以從這個鏈接下載,也可以通過 NuGet 安裝。
1 PM> Install-Package Spire.PDF
用密碼加密 PDF 文件
有兩種用于加密 PDF 文件的密碼--打開密碼和權限密碼。前者用于打開 PDF 文件,后者用于限制打印、內容復制、注釋等。如果使用這兩種密碼對 PDF 文件進行加密,則可使用任一種密碼打開文件。
Spire.PDF for .NET提供的PdfSecurity.Encrypt(string openPassword, string permissionPassword, PdfPermissionsFlags permissions, PdfEncryptionKeySize keySize)方法允許你同時設置打開密碼和權限密碼來加密PDF文件。具體步驟如下。
- 創建一個 PdfDocument 對象。
- 使用 PdfDocument.LoadFromFile() 方法加載一個示例 PDF 文件。
- 使用PdfDocument.Security屬性獲取文檔的安全參數。
- 使用PdfSecurity.Encrypt(string openPassword, string permissionPassword, PdfPermissionsFlags permissions, PdfEncryptionKeySize keySize)方法用打開密碼和權限密碼加密PDF文件。
- 使用PdfDocument.SaveToFile()方法保存結果文件。
[C#]
01 using Spire.Pdf; 02 using Spire.Pdf.Security; 03 04 namespace EncryptPDF 05 { 06 class Program 07 { 08 static void Main(string[] args) 09 { 10 //Create a PdfDocument object 11 PdfDocument pdf = new PdfDocument(); 12 13 //Load a sample PDF file 14 pdf.LoadFromFile(@"E:\Files\sample.pdf"); 15 16 //Encrypt the PDF file with password 17 pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit); 18 19 //Save the result file 20 pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF); 21 } 22 } 23 }
[VB.NET]
01 Imports Spire.Pdf 02 Imports Spire.Pdf.Security 03 04 Namespace EncryptPDF 05 Class Program 06 Private Shared Sub Main(ByVal args As String()) 07 08 'Create a PdfDocument object 09 Dim pdf As PdfDocument = New PdfDocument() 10 11 'Load a sample PDF file 12 pdf.LoadFromFile("E:\Files\sample.pdf") 13 14 'Encrypt the PDF file with password 15 pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print Or PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit) 16 17 'Save the result file 18 pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF) 19 End Sub 20 End Class 21 End Namespace

移除密碼解密PDF文件
當你需要從PDF文件中移除密碼時,你可以在調用PdfSecurity.Encrypt(string openPassword, string permissionPassword, PdfPermissionsFlags permissions, PdfEncryptionKeySize keySize, string originalPermissionPassword)方法時將打開密碼和權限密碼設置為空。具體步驟如下
- 創建一個 PdfDocument 對象。
- 使用PdfDocument.LoadFromFile(string filename, string password)方法加載帶密碼的加密PDF文件。
- 使用PdfDocument.Security屬性獲取文檔的安全參數。
- 通過使用PdfSecurity.Encrypt(string openPassword, string permissionPassword, PdfPermissionsFlags permissions, PdfEncryptionKeySize keySize, string originalPermissionPassword)方法將打開密碼和權限密碼設置為空,解密PDF文件。
- 使用 PdfDocument.SaveToFile() 方法保存結果文件。
01 using Spire.Pdf; 02 using Spire.Pdf.Security; 03 04 namespace DecryptPDF 05 { 06 class Program 07 { 08 static void Main(string[] args) 09 { 10 //Create a PdfDocument object 11 PdfDocument pdf = new PdfDocument(); 12 13 //Load the encrypted PDF file with password 14 pdf.LoadFromFile("Encrypt.pdf", "open"); 15 16 //Set the password as empty to decrypt PDF 17 pdf.Security.Encrypt(string.Empty, string.Empty, PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit, "permission"); 18 19 //Save the result file 20 pdf.SaveToFile("Decrypt.pdf", FileFormat.PDF); 21 } 22 } 23 }
[VB.NET]
01 Imports Spire.Pdf 02 Imports Spire.Pdf.Security 03 04 Namespace DecryptPDF 05 Class Program 06 Private Shared Sub Main(ByVal args As String()) 07 08 'Create a PdfDocument object 09 Dim pdf As PdfDocument = New PdfDocument() 10 11 'Load the encrypted PDF file with password 12 pdf.LoadFromFile("Encrypt.pdf", "open") 13 14 'Set the password as empty to decrypt PDF 15 pdf.Security.Encrypt(String.Empty, String.Empty, PdfPermissionsFlags.[Default], PdfEncryptionKeySize.Key128Bit, "permission") 16 17 'Save the result file 18 pdf.SaveToFile("Decrypt.pdf", FileFormat.PDF) 19 End Sub 20 End Class 21 End Namespace
申請臨時許可證
若想從生成的文檔中刪除評估信息,或解除功能限制,申請 30 天試用許可證。