C# 對PDF文檔加密、解密(基于Spire.Cloud.SDK for .NET)
Spire.Cloud.SDK for .NET提供了接口PdfSecurityApi可用于加密、解密PDF文檔。本文將通過C#代碼演示具體加密及解密方法。
使用工具:
- Spire.Cloud.SDK for .NET
- Visual Studio
必要步驟:
步驟一:
dll文件獲取及導(dǎo)入。在程序中通過Nuget搜索下載,直接導(dǎo)入所有dll。
導(dǎo)入效果如下如所示:
步驟二:
App ID及Key獲取。在“我的應(yīng)用”板塊中創(chuàng)建應(yīng)用以獲得App ID及App Key。
步驟三:
源文檔上傳。在“文檔管理”板塊,上傳源文檔。這里可以建文件夾,將文檔存放在文件夾下。不建文件夾時,源文檔及結(jié)果文檔直接保存在根目錄。本文示例中,建了兩個文件夾,分別用于存放源文檔及結(jié)果文檔。(云平臺提供免費1 萬次調(diào)用次數(shù)和 2G 文檔內(nèi)存)
C# 代碼示例
【示例1】加密PDF文檔
using System; using Spire.Cloud.Pdf.Sdk.Client; using Spire.Cloud.Pdf.Sdk.Api; using System.IO; using System.Collections.Generic; namespace Encryt { class Program { //配置賬號信息 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "https://api.e-iceblue.cn"; static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl); static PdfSecurityApi PdfSecurityApi = new PdfSecurityApi(PdfConfiguration); static void Main(string[] args) { string name = "sample.pdf";//源文檔 string destFilePath = "pdfsecurity/Encrypt.pdf";//結(jié)果文檔路徑(將結(jié)果文檔存放在pdfsecurity文件夾下) string userPassword = "123";//設(shè)置用戶密碼 string ownerPassword = "321";//設(shè)置所有者密碼 string keySize = "Key40Bit";//設(shè)置keySize(如果不需要設(shè)置,可設(shè)置為null) List<string> permissionsFlags = new List<string>();//設(shè)置permissionsFlags(如果不需要設(shè)置,可設(shè)置為null) permissionsFlags.Add("Print"); string folder = "input";//源文檔所在文件夾 string password = null;//源文檔密碼 string storage = null; //調(diào)用方法加密文檔 PdfSecurityApi.EncryptDocumentInStorage(name,destFilePath,userPassword,ownerPassword,keySize,permissionsFlags,folder,storage,password); } } }
生成的文檔打開時,需要輸入密碼。
文檔加密結(jié)果:
【示例2】解密PDF文檔
這里以上文中生成的加密PDF為測試文檔。
using System; using Spire.Cloud.Pdf.Sdk.Client; using Spire.Cloud.Pdf.Sdk.Api; namespace Decrypt { class Program { //配置賬號信息 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "https://api.e-iceblue.cn"; static Configuration PdfConfiguration = new Configuration(appId, appKey, baseUrl); static PdfSecurityApi PdfSecurityApi = new PdfSecurityApi(PdfConfiguration); static void Main(string[] args) { string name = "Encrypt.pdf";//源文檔 string destFilePath = "pdfsecurity/Decrypt.pdf";//結(jié)果文檔路徑(pdfsecurity為結(jié)果文檔所在文件夾) string password = "321";//文檔密碼(這里需要使用的是ownerpassword) string folder = "pdfsecurity";//源文檔所在文件夾 string storage = null; //調(diào)用方法解密文檔 PdfSecurityApi.DecryptDocumentInStorage(name,destFilePath,password,folder,storage); } } }
生成的文檔將不再有密碼保護。
以上就是C# 對PDF文檔加密、解密(基于Spire.Cloud.SDK for .NET)的詳細(xì)內(nèi)容,更多關(guān)于C# 對PDF文檔加密、解密的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#使用Excel動態(tài)函數(shù)實現(xiàn)生成依賴列表
這篇文章主要為大家詳細(xì)介紹了如何在C#中使用?Excel?動態(tài)函數(shù)生成依賴列表,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實現(xiàn)
本文主要介紹了C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03c# 獲取CookieContainer的所有cookies函數(shù)代碼
這篇文章主要介紹了c# 獲取CookieContainer所有cookies的函數(shù)代碼,需要的朋友可以參考下2013-06-06在c#中使用servicestackredis操作redis的實例代碼
本篇文章主要介紹了在c#中使用servicestackredis操作redis的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06