IRONSOFTWAREHOME
PDF 加密與解密
using IronPdf;
using System;

//Open an Encrypted File, alternatively create a new PDF from Html

var pdf = PdfDocument.FromFile("encrypted.pdf", "password");

//Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto";
pdf.MetaData.Keywords = "SEO, Friendly";
pdf.MetaData.ModifiedDate = DateTime.Now;

//Edit file security settings

//The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption();
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserFormData = false;
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;

// change or set the document encrpytion password
pdf.Password = "my-password";
pdf.SaveAs("secured.pdf");
Imports IronPdf
Imports System

'Open an Encrypted File, alternatively create a new PDF from Html

Dim pdf = PdfDocument.FromFile("encrypted.pdf", "password")

'Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto"
pdf.MetaData.Keywords = "SEO, Friendly"
pdf.MetaData.ModifiedDate = DateTime.Now

'Edit file security settings

'The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption()
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key")
pdf.SecuritySettings.AllowUserAnnotations = False
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SecuritySettings.AllowUserFormData = False
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights

' change or set the document encrpytion password
pdf.Password = "my-password"
pdf.SaveAs("secured.pdf")
NuGet Download
PM > Install-Package IronPdf
PDF 加密與解密

PDF 加密與解密

PDF安全性是處理PDF文件的重要方面,而IronPDF非常重視PDF安全性。 使用IronPDF,您可以通過應用自訂的元資料和安全設置來加密和解密PDF文件。 IronPDF支持您的加密文件進行128位加密,能夠解密之前加密的文件(當您擁有正確的存取密碼時),並對現有或新建立的PDF文件應用密碼保護。

使用IronPDF加密和解密PDF文件的第一步是建立一個新的PDF文件或像我們這樣載入一個現有的文件。 在使用FromFile方法傳遞了兩個參數:第一個是PDF的文件路徑,第二個是打開加密PDF的密碼。

接下來的幾行正在編輯我們加密PDF的元資料。 Author字段,使用我們自訂的字串值。 每個元資料字段可以通過MetaData屬性中的任何其他内容。

下一行刪除PDF文件中的任何當前密碼和加密。 此解密過程允許您保存PDF而不加密,或清除以便您為PDF新增新的加密設置,這將是我們接下來要做的。 setting被您希望更改的實際設置替換。 例如,pdf.SecuritySettings.MakePdfDocumentReadOnly將PDF設置為只讀。 PdfSecuritySettings類包含許多選項,允許完全自訂您的PDF的安全設置,從不允許使用者註釋到控制列印權限; 此類確保正確的PDF安全性能夠安全且有效地處理。

pdf.Password用於更改您的PDF密碼,或者設置一個新的密碼,如果還沒有設置密碼的話。 此密碼使用強大的128位加密來加密PDF文件,以防未經授權的存取。 一旦您對元資料和安全設置滿意,您可以使用SaveAs()將PDF文件保存到所需的文件位置。 學習如何使用IronPDF設置PDF許可權和密碼

在 GitHub 上查看

準備開始了嗎?

Nuget Downloads 21,105,021版本:2026.9剛剛發布

Key in blue circle

立即免費取得 30 天試用金鑰。

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

OR
bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
預訂您的免費現場演示
Booking Badge

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或電子郵件sales@ironsoftware.com
您的詳細資訊將始終保密。
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰。
無需信用卡或帳戶建立
C# 用於PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解決方案資源管理器,右鍵點選參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronPdf"
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.9

或者點擊此處下載Windows安裝程式。

  1. 下載並解壓IronPDF到類似~/Libs的位置,位於您的解決方案目錄中
  2. 在Visual Studio解決方案資源管理器,右鍵點選參考。選擇瀏覽,"IronPdf.dll"

授權從$999起