跳過到頁腳內容
使用IRONPDF

如何在.NET中保護PDF:加密、密碼和權限控制

如何在 .NET 中保護 PDF 檔案:加密、密碼和權限控制

在 .NET 應用程式中建立 PDF 工作流程時,保護敏感文件是一項至關重要的要求。 財務報告、法律合約和合規記錄在分發時如果沒有存取控制,都會存在風險。 任何人都可以開啟、複製或編輯的 PDF 文件並非安全文檔,而是一種安全隱患。

IronPDF提供直接 API,用於加密 PDF 檔案、強制密碼存取以及限製列印和內容複製等權限。 本教學涵蓋了每種安全機制,並提供了 .NET 10 的 C# 程式碼範例。

開始您的免費試用,跟隨以下的程式碼範例。

!{--01001100010010010100001001010010010000010101001001011001010111110100011101000101010101 01000101111101010011010101000100000101010010010101000100010101000100010111110101011101001000110 1010101000100100001011111010100000101001001001111010001000101010101010000110101010100101010101011 10101010001010010010010010010000010100110001011111010000100100110001001111101000011010010111111010000110100101110--

如何在 .NET 中開始使用 PDF 安全功能?

在套用安全設定之前,請將 IronPDF 安裝到您的 .NET 專案中。 開啟 NuGet 套件管理員控制台並執行:

Install-Package IronPdf
Install-Package IronPdf
SHELL

或透過 .NET CLI 新增:

dotnet add package IronPdf
dotnet add package IronPdf
SHELL

安裝完成後,將 using IronPdf; 指令新增至您的檔案。 PdfDocument 類別公開了一個 SecuritySettings 屬性,該屬性控制所有加密和權限選項。 無需額外配置—當您設定密碼時,該程式庫會自動啟動 128 位元加密。

IronPDF 可在 Windows、macOS 和 Linux 上運行,無需額外的本機依賴項,因此安全性 API 在容器化環境中也能正常運作。 IronPDF 無需任何平台特定的設定即可部署到Azure和 Docker。除了 .NET 10 之外,IronPDF 還支援 .NET 8 和 .NET 9,並且對於處理安全文件工作流程的舊版應用程序,還支援.NET Framework 4.6.2及更高版本。

有關包括許可證啟動和項目設定在內的完整安裝步驟,請參閱IronPDF .NET 安裝指南

PDF安全中的使用者密碼和所有者密碼有何不同?

PDF規範定義了兩種不同的密碼類型,它們在文件存取控制中發揮不同的作用。 了解每種機制的工作原理,可以幫助您為自己的用例設計合適的安全模型。

需要使用者密碼(也稱為開啟密碼)才能開啟和檢視 PDF 檔案。 任何試圖存取該文件的人都必須先輸入此密碼,才能看到任何內容。 如果目標是完全防止未經授權的人員閱讀文檔,那麼這種控制措施是合適的。

所有者密碼(也稱為權限密碼)控製文件開啟後允許執行的操作。 即使使用者密碼授予了讀取權限,所有者密碼仍決定是否允許列印、複製內容、編輯或填寫表格。 將兩個密碼設定為不同的值,表示檢視者如果沒有擁有者憑證就無法修改安全性配置。

以下程式碼範例示範如何將兩種密碼類型套用到新的 PDF 檔案:

using IronPdf;

// Create a new PDF document from HTML content
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential Report</h1><p>Sensitive financial data inside.</p>");

// Set owner password to control editing permissions
pdf.SecuritySettings.OwnerPassword = "owner-secret-123";

// Set user password required to open the document
pdf.SecuritySettings.UserPassword = "user-access-456";

// Save the encrypted PDF
pdf.SaveAs("protected-report.pdf");
using IronPdf;

// Create a new PDF document from HTML content
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential Report</h1><p>Sensitive financial data inside.</p>");

// Set owner password to control editing permissions
pdf.SecuritySettings.OwnerPassword = "owner-secret-123";

// Set user password required to open the document
pdf.SecuritySettings.UserPassword = "user-access-456";

// Save the encrypted PDF
pdf.SaveAs("protected-report.pdf");
Imports IronPdf

' Create a new PDF document from HTML content
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Confidential Report</h1><p>Sensitive financial data inside.</p>")

' Set owner password to control editing permissions
pdf.SecuritySettings.OwnerPassword = "owner-secret-123"

' Set user password required to open the document
pdf.SecuritySettings.UserPassword = "user-access-456"

' Save the encrypted PDF
pdf.SaveAs("protected-report.pdf")
$vbLabelText   $csharpLabel

已套用使用者密碼保護的 PDF 檔案 - 開啟時顯示密碼對話框

SecuritySettings 屬性提供對所有加密和權限控制的統一存取。 設定 OwnerPassword 可啟動文件的 128 位元加密。 設定 UserPassword 會在開啟檔案時建立存取屏障。這兩個屬性可以獨立設定——僅使用所有者密碼保護的文件對所有人可見,但會限制他們的操作權限。

有關完整的 SecuritySettings API 的更多詳細信息,請參閱PdfSecuritySettings 類別參考

如何加密現有的 PDF 文件?

許多工作流程需要為現有的 PDF 檔案添加安全性,而非從 HTML 或範本生成新的檔案。 這適用於接收來自外部來源的文件、處理網頁應用程式的上傳,或在歸檔前保護檔案安全。

IronPDF 使用相同的 SecuritySettings API 處理此問題。 使用 PdfDocument.FromFile 載入文件,套用安全配置,然後儲存結果:

using IronPdf;

// Load an existing PDF document from disk
var pdf = PdfDocument.FromFile("financial-statement.pdf");

// Apply both passwords
pdf.SecuritySettings.OwnerPassword = "admin-key-789";
pdf.SecuritySettings.UserPassword = "reader-key-321";

// Restrict printing and content copying
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;

// Save the secured version
pdf.SaveAs("financial-statement-secured.pdf");
using IronPdf;

// Load an existing PDF document from disk
var pdf = PdfDocument.FromFile("financial-statement.pdf");

// Apply both passwords
pdf.SecuritySettings.OwnerPassword = "admin-key-789";
pdf.SecuritySettings.UserPassword = "reader-key-321";

// Restrict printing and content copying
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;

// Save the secured version
pdf.SaveAs("financial-statement-secured.pdf");
Imports IronPdf

' Load an existing PDF document from disk
Dim pdf = PdfDocument.FromFile("financial-statement.pdf")

' Apply both passwords
pdf.SecuritySettings.OwnerPassword = "admin-key-789"
pdf.SecuritySettings.UserPassword = "reader-key-321"

' Restrict printing and content copying
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint
pdf.SecuritySettings.AllowUserCopyPasteContent = False

' Save the secured version
pdf.SaveAs("financial-statement-secured.pdf")
$vbLabelText   $csharpLabel

已套用加密的現有 PDF 文檔,其權限已受限,可在屬性面板中查看。

此方法適用於任何有效的 PDF 檔案,無論其原始建立方式為何。 該函式庫會處理輸入文件,並根據所有指定的安全性設定,產生一份加密副本。 當您儲存至不同路徑時,原始檔案不會被修改。

如需查看將加密與解密整合於單一工作流程的完整範例,請參閱 PDF 加密與解密程式碼範例

可以控制哪些文件權限?

除了密碼保護之外,PDF 安全性還包含對使用者開啟文件後可執行的操作進行細粒度控制。 PDF 規格中的權限標記可讓您獨立地允許或禁止列印、內容複製、編輯、註解以及表單資料輸入。

必須設定擁有者密碼,權限限制才能生效。 若未如此處理,使用相容 PDF 閱讀器的使用者可能會繞過權限標記。

以下範例展示如何為合約文件設定權限,使其可檢視與填寫,但不可編輯或列印:

using IronPdf;

// Load a contract document
var pdf = PdfDocument.FromFile("contract.pdf");

// Owner password is required for permissions enforcement
pdf.SecuritySettings.OwnerPassword = "contract-admin";

// Allow printing with full print quality
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;

// Prevent content extraction (protects against copy/paste attacks)
pdf.SecuritySettings.AllowUserCopyPasteContent = false;

// Lock down editing
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.NoEdit;

// Disable comment additions
pdf.SecuritySettings.AllowUserAnnotations = false;

// Allow form completion while blocking other modifications
pdf.SecuritySettings.AllowUserFormData = true;

// Save with all restrictions
pdf.SaveAs("contract-restricted.pdf");
using IronPdf;

// Load a contract document
var pdf = PdfDocument.FromFile("contract.pdf");

// Owner password is required for permissions enforcement
pdf.SecuritySettings.OwnerPassword = "contract-admin";

// Allow printing with full print quality
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;

// Prevent content extraction (protects against copy/paste attacks)
pdf.SecuritySettings.AllowUserCopyPasteContent = false;

// Lock down editing
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.NoEdit;

// Disable comment additions
pdf.SecuritySettings.AllowUserAnnotations = false;

// Allow form completion while blocking other modifications
pdf.SecuritySettings.AllowUserFormData = true;

// Save with all restrictions
pdf.SaveAs("contract-restricted.pdf");
Imports IronPdf

' Load a contract document
Dim pdf = PdfDocument.FromFile("contract.pdf")

' Owner password is required for permissions enforcement
pdf.SecuritySettings.OwnerPassword = "contract-admin"

' Allow printing with full print quality
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights

' Prevent content extraction (protects against copy/paste attacks)
pdf.SecuritySettings.AllowUserCopyPasteContent = False

' Lock down editing
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.NoEdit

' Disable comment additions
pdf.SecuritySettings.AllowUserAnnotations = False

' Allow form completion while blocking other modifications
pdf.SecuritySettings.AllowUserFormData = True

' Save with all restrictions
pdf.SaveAs("contract-restricted.pdf")
$vbLabelText   $csharpLabel

PDF 文件已套用權限限制,列印和編輯控制項已停用。

下表概述了所有可用的權限屬性及其常見應用:

IronPDF SecuritySettings 權限屬性
屬性類型描述常見使用情境
AllowUserPrintingPdfPrintSecurity控制列印權限:NoPrintFullPrintRights防止列印機密文件
AllowUserCopyPasteContentbool啟用或停用文字與圖片擷取功能防止智慧財產權遭竊取
允許使用者編輯PdfEditSecurity編輯控制功能:NoEdit 或 edit-allowed 值鎖定合約與法律文件以防止修改
允許使用者註解bool允許或拒絕新增註解與標記控制文件審核工作流程
AllowUserFormDatabool啟用或停用表單欄位自動填寫功能允許填寫 PDF 表單,同時阻止其他編輯操作
AllowUserCopyPasteContentForAccessibilitybool管理螢幕閱讀器的內容擷取在限制一般性複製的同時,須符合無障礙規範

如需更多實際應用權限標誌的程式碼範例,請參閱 IronPDF 安全性與元資料範例

如何快速套用唯讀保護?

當目標是一次鎖定所有使用者修改(複製、列印、編輯和註解)時,MakePdfDocumentReadOnly 便捷方法只需一次呼叫即可處理。 此功能適用於需要最高權限限制的最終版本文件,無需逐一設定各項權限。

using IronPdf;

// Load the document to make read-only
var pdf = PdfDocument.FromFile("final-report.pdf");

// Apply full read-only protection with one method call
// This sets owner password and blocks all modification capabilities
pdf.SecuritySettings.MakePdfDocumentReadOnly("owner-readonly-password");

// Save the protected document
pdf.SaveAs("final-report-readonly.pdf");
using IronPdf;

// Load the document to make read-only
var pdf = PdfDocument.FromFile("final-report.pdf");

// Apply full read-only protection with one method call
// This sets owner password and blocks all modification capabilities
pdf.SecuritySettings.MakePdfDocumentReadOnly("owner-readonly-password");

// Save the protected document
pdf.SaveAs("final-report-readonly.pdf");
Imports IronPdf

' Load the document to make read-only
Dim pdf = PdfDocument.FromFile("final-report.pdf")

' Apply full read-only protection with one method call
' This sets owner password and blocks all modification capabilities
pdf.SecuritySettings.MakePdfDocumentReadOnly("owner-readonly-password")

' Save the protected document
pdf.SaveAs("final-report-readonly.pdf")
$vbLabelText   $csharpLabel

MakePdfDocumentReadOnly 方法設定您提供的擁有者密碼,並同時停用所有修改權限。 生成的文件無需密碼即可開啟閱讀,但列印、複製、編輯及註解功能均受限制。 當無需個別調整權限時,這是將文件完全鎖定的最快途徑。 對於某些權限必須保持開放的情況(例如,允許列印但阻止複製),請按照上述權限部分所示配置各個 SecuritySettings 屬性。

如何破解並移除 PDF 密碼保護?

當透過程式化方式處理加密的 PDF 檔案時,您需要提供正確的密碼才能存取內容。 PdfDocument.FromFile 方法接受一個可選的密碼參數來實現此目的。

以下範例展示如何開啟受密碼保護的檔案,並可選擇性地完全移除其加密:

using IronPdf;

// Open a password-protected PDF by supplying the user password
var pdf = PdfDocument.FromFile("protected-report.pdf", "user-access-456");

// Extract text content from the decrypted document
string content = pdf.ExtractAllText();

// Remove all passwords and encryption when you need an unprotected version
pdf.SecuritySettings.RemovePasswordsAndEncryption();

// Save the unencrypted copy
pdf.SaveAs("report-unlocked.pdf");
using IronPdf;

// Open a password-protected PDF by supplying the user password
var pdf = PdfDocument.FromFile("protected-report.pdf", "user-access-456");

// Extract text content from the decrypted document
string content = pdf.ExtractAllText();

// Remove all passwords and encryption when you need an unprotected version
pdf.SecuritySettings.RemovePasswordsAndEncryption();

// Save the unencrypted copy
pdf.SaveAs("report-unlocked.pdf");
Imports IronPdf

' Open a password-protected PDF by supplying the user password
Dim pdf = PdfDocument.FromFile("protected-report.pdf", "user-access-456")

' Extract text content from the decrypted document
Dim content As String = pdf.ExtractAllText()

' Remove all passwords and encryption when you need an unprotected version
pdf.SecuritySettings.RemovePasswordsAndEncryption()

' Save the unencrypted copy
pdf.SaveAs("report-unlocked.pdf")
$vbLabelText   $csharpLabel

RemovePasswordsAndEncryption 方法會移除文件的所有安全保護,並將其儲存為標準的、未受保護的 PDF。 這在處理文件以供歸檔、進一步轉換或重新分發時特別有用,尤其當最終使用者限制已不再適用時。

如果 PDF 包含數位簽名,請將 true 傳遞給 RemovePasswordsAndEncryption(true) 以同時刪除簽名,或省略該參數以保留簽名。

若需同時檢視加密與解密的工作流程,PDF 加密與解密範例透過單一可執行檔案,完整展示了這兩項操作。

還有哪些其他 PDF 安全功能可用?

密碼加密與權限標記已涵蓋最常見的 PDF 安全性需求,但 IronPDF 亦針對更專業的應用情境,提供額外的文件保護層級。

數位簽章:IronPDF 支援使用 X.509 憑證簽署 PDF 文件,以驗證文件真實性並偵測篡改行為。 已簽署的文件會顯示視覺化簽名欄位,並提供簽署者身分的加密證明。 請參閱 PDF 簽名操作指南,以獲取實作細節與憑證設定說明

合規標準:對於受 HIPAA、GDPR 或金融法規管制的應用程式,文件加密通常是必備的控制措施。 IronPDF 的 128 位元加密技術,若搭配適當的金鑰管理措施,即可滿足多數合規框架的基本要求。 請參閱與您特定合規範圍相關的監管指引。

IronSecureDoc:針對企業級文件安全需求——包括資料遮蔽、進階數位簽章工作流程及多文件處理——IronSecureDoc 提供採用一次性授權模式的專用安全產品。

PDF 規格文件中的外部參考資料有助於理解權限模型。 Adobe 開發者網站上的 PDF 規格概述,詳述了加密與權限在格式層級上的實現方式。 NIST 特別出版物 800-111《儲存加密指南》為受監管環境中的加密技術選型提供了參考依據。 若欲更全面了解 .NET 中的文件安全性最佳實務,請參閱 Microsoft 的 .NET 加密模型文件,其中闡述了 PDF 函式庫所建基的底層平台加密機制。

下一步計劃是什麼?

.NET 中的 PDF 安全性涵蓋三個關鍵領域:透過使用者密碼控制誰能開啟文件、透過權限標誌限制使用者可執行的操作,以及透過數位簽章驗證文件的真實性。 IronPDF 透過 SecuritySettings API 和簽章工作流程處理所有這三項,而無需手動實現加密。

延伸閱讀與實作程式碼:

立即開始免費試用 IronPDF,在您的應用程式中測試這些安全功能。 若需進行生產環境部署,請參閱 IronPDF 授權方案,以選擇符合您專案規模的方案。

常見問題解答

PDF中使用者密碼和擁有者密碼有何不同?

使用者密碼(開啟密碼)是開啟PDF文件所需的。擁有者密碼(權限密碼)控制在文件開啟後允許的行動,如列印、複製和編輯。您可以獨立設定兩者——只有擁有者密碼的文件可以被任何人讀取但限制可允許的行動。

如何在C# .NET中加密PDF文件?

載入或建立PdfDocument,然後設定pdf.SecuritySettings.OwnerPassword和/或pdf.SecuritySettings.UserPassword。當您設定任一密碼時,IronPDF會自動應用128位加密。呼叫pdf.SaveAs以寫入加密文件。

如何防止使用者在.NET中列印或複製PDF?

設定pdf.SecuritySettings.AllowUserPrinting = IronPDF.Security.PdfPrintSecurity.NoPrint以阻止列印,並將pdf.SecuritySettings.AllowUserCopyPasteContent = false以防止內容提取。這些限制需要設定擁有者密碼以便執行。

如何在C#中使用IronPDF開啟受密碼防護的PDF?

使用PdfDocument.FromFile與密碼作為第二個參數:var pdf = PdfDocument.FromFile('file.pdf', 'user-password')。這會在記憶體中解密文件以供進一步處理。

如何在.NET中移除PDF的密碼保護?

在用密碼載入PDF後,呼叫pdf.SecuritySettings.RemovePasswordsAndEncryption()並儲存結果。這會從文件中移除所有加密和權限限制。

IronPDF中的MakePdfDocumentReadOnly是什麼?

MakePdfDocumentReadOnly是SecuritySettings上的便利方法,一次呼叫即可設定擁有者密碼並禁用所有修改權限(列印、複製、編輯和註釋)。文件無需密碼即可讀取但無法修改。

IronPDF是否支援PDF文件的數位簽章?

是的。IronPDF支援使用X.509憑證簽署PDF文件。簽署的文件包括視覺簽名字段並提供加密證明簽署者的身份。詳情請參見IronPDF PDF簽署操作指南。

IronPDF的PDF加密是否符合HIPAA或GDPR要求?

IronPDF的128位加密滿足大多數合規框架的基線加密要求。然而,合規涉及加密以外的額外控制,包括金鑰管理、訪問記錄和數據處理政策。請審查您合規範圍的具體要求。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我