翻譯|行業(yè)資訊|編輯:胡濤|2023-12-21 09:51:30.763|閱讀 75 次
概述:在本文中,您將了解如何使用Spire.PDF for .NET以編程方式加密或解密 PDF 文件。歡迎查閱~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
當(dāng)涉及到在 Internet 上共享機(jī)密文檔時(shí),PDF 加密是一項(xiàng)至關(guān)重要的任務(wù)。通過(guò)使用強(qiáng)密碼加密 PDF 文件,您可以保護(hù)文件數(shù)據(jù)免遭未經(jīng)授權(quán)的人員訪問(wèn)。在某些情況下,可能還需要?jiǎng)h除密碼才能公開(kāi)文檔。在本文中,您將了解如何使用Spire.PDF for .NET以編程方式加密或解密 PDF 文件。
Spire.PDF for .NET 是一款獨(dú)立 PDF 控件,用于 .NET 程序中創(chuàng)建、編輯和操作 PDF 文檔。使用 Spire.PDF 類(lèi)庫(kù),開(kāi)發(fā)人員可以新建一個(gè) PDF 文檔或者對(duì)現(xiàn)有的 PDF 文檔進(jìn)行處理,且無(wú)需安裝 Adobe Acrobat。
E-iceblue 功能類(lèi)庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
首先,您需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過(guò)NuGet安裝。
PM> Install-Package Spire.PDF
加密PDF文件有兩種密碼——打開(kāi)密碼和權(quán)限密碼。前者設(shè)置為打開(kāi) PDF 文件,而后者設(shè)置為限制打印、內(nèi)容復(fù)制、注釋等。如果 PDF 文件使用兩種類(lèi)型的密碼保護(hù),則可以使用任一密碼打開(kāi)它。
Spire.PDF for .NET 提供的PdfSecurity.Encrypt (string openPassword, stringpermissionPassword, PdfPermissionsFlags Permissions, PdfEncryptionKeySize keySize)方法允許您設(shè)置打開(kāi)密碼和權(quán)限密碼來(lái)加密 PDF 文件。詳細(xì)步驟如下。
【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
當(dāng)您需要?jiǎng)h除PDF文件的密碼時(shí),可以在調(diào)用PdfSecurity.Encrypt(string openPassword, stringpermissionPassword, PdfPermissionsFlagspermissions, PdfEncryptionKeySize keySize, stringoriginalPermissionPassword)方法時(shí)將打開(kāi)密碼和權(quán)限密碼設(shè)置為空。詳細(xì)步驟如下。
【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 文件,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn