跳過到頁腳內容
使用IRONPDF

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

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

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

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

立即開始免費試用,即可跟隨以下程式碼範例學習。

立即開始在您的項目中使用 IronPDF 並免費試用。

第一步:
green arrow pointer

如何在.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");
$vbLabelText   $csharpLabel

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

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

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

如何加密現有的PDF文件?

許多工作流程需要為現有的 PDF 文件添加安全保護,而不是從 HTML 或範本產生新的文件。 這適用於從外部來源接收文件、在 Web 應用程式中處理上傳內容或在歸檔前保護文件的情況。

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");
$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");
$vbLabelText   $csharpLabel

已套用權限限制的PDF文檔,顯示已停用列印和編輯控制項

下表總結了所有可用的權限屬性及其常見用途:

IronPDF安全設定權限屬性
財產類型描述常見用例
`AllowUserPrinting``PdfPrintSecurity`控制列印權限: `NoPrint`或`FullPrintRights`防止印表機密文件
`AllowUserCopyPasteContent``bool`啟用或停用文字和圖像擷取保護智慧財產權免遭竊取
`AllowUserEdits``PdfEditSecurity`控制編輯功能: `NoEdit`或允許編輯的值鎖定合約和法律文件,防止修改
`AllowUserAnnotations``bool`允許或拒絕添加註釋和標記控製文件審核工作流程
`AllowUserFormData``bool`啟用或停用表單欄位自動填充允許填寫PDF表單,同時阻止其他編輯操作
`AllowUserCopyPasteContentForAccessibility``bool`管理螢幕閱讀器的內容擷取在限制一般複製的同時,保持無障礙合規性

有關在實務上使用權限標誌的更多程式碼範例,請參閱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");
$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");
$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 機器人,結合科技與創意的樂趣。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me