翻譯|行業(yè)資訊|編輯:胡濤|2023-12-21 09:51:30.763|閱讀 75 次
概述:在本文中,您將了解如何使用Spire.PDF for .NET以編程方式加密或解密 PDF 文件。歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
當涉及到在 Internet 上共享機密文檔時,PDF 加密是一項至關(guān)重要的任務(wù)。通過使用強密碼加密 PDF 文件,您可以保護文件數(shù)據(jù)免遭未經(jīng)授權(quán)的人員訪問。在某些情況下,可能還需要刪除密碼才能公開文檔。在本文中,您將了解如何使用Spire.PDF for .NET以編程方式加密或解密 PDF 文件。
Spire.PDF for .NET 是一款獨立 PDF 控件,用于 .NET 程序中創(chuàng)建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發(fā)人員可以新建一個 PDF 文檔或者對現(xiàn)有的 PDF 文檔進行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
首先,您需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.PDF
加密PDF文件有兩種密碼——打開密碼和權(quán)限密碼。前者設(shè)置為打開 PDF 文件,而后者設(shè)置為限制打印、內(nèi)容復制、注釋等。如果 PDF 文件使用兩種類型的密碼保護,則可以使用任一密碼打開它。
Spire.PDF for .NET 提供的PdfSecurity.Encrypt (string openPassword, stringpermissionPassword, PdfPermissionsFlags Permissions, PdfEncryptionKeySize keySize)方法允許您設(shè)置打開密碼和權(quán)限密碼來加密 PDF 文件。詳細步驟如下。
【C#】
using Spire.Pdf; using Spire.Pdf.Security; namespace EncryptPDF { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load a sample PDF file pdf.LoadFromFile(@"E:\Files\sample.pdf"); //Encrypt the PDF file with password pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit); //Save the result file pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF); } } }
【VB.NET】
mports Spire.Pdf Imports Spire.Pdf.Security Namespace EncryptPDF Class Program Private Shared Sub Main(ByVal args As String()) 'Create a PdfDocument object Dim pdf As PdfDocument = New PdfDocument() 'Load a sample PDF file pdf.LoadFromFile("E:\Files\sample.pdf") 'Encrypt the PDF file with password pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print Or PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit) 'Save the result file pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF) End Sub End Class End Namespace
當您需要刪除PDF文件的密碼時,可以在調(diào)用PdfSecurity.Encrypt(string openPassword, stringpermissionPassword, PdfPermissionsFlagspermissions, PdfEncryptionKeySize keySize, stringoriginalPermissionPassword)方法時將打開密碼和權(quán)限密碼設(shè)置為空。詳細步驟如下。
【C#】
using Spire.Pdf; using Spire.Pdf.Security; namespace DecryptPDF { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load the encrypted PDF file with password pdf.LoadFromFile("Encrypt.pdf", "open"); //Set the password as empty to decrypt PDF pdf.Security.Encrypt(string.Empty, string.Empty, PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit, "permission"); //Save the result file pdf.SaveToFile("Decrypt.pdf", FileFormat.PDF); } } }
【VB.NET】
Imports Spire.Pdf Imports Spire.Pdf.Security Namespace DecryptPDF Class Program Private Shared Sub Main(ByVal args As String()) 'Create a PdfDocument object Dim pdf As PdfDocument = New PdfDocument() 'Load the encrypted PDF file with password pdf.LoadFromFile("Encrypt.pdf", "open") 'Set the password as empty to decrypt PDF pdf.Security.Encrypt(String.Empty, String.Empty, PdfPermissionsFlags.[Default], PdfEncryptionKeySize.Key128Bit, "permission") 'Save the result file pdf.SaveToFile("Decrypt.pdf", FileFormat.PDF) End Sub End Class End Namespace
以上便是如何將加密或解密 PDF 文件,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn