如何使用 C# 透過密碼與權限保護 PDF 檔案 | IronPDF 教學

如何在 C# 中設定 PDF 密碼與權限

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 讓您能夠在 C# 中透過密碼和權限保護 PDF 文件,同時支援用於開啟檔案的"使用者密碼",以及用於控制編輯、列印和複製權限的"擁有者密碼",並採用 128 位元加密技術。 這項全面的安全功能讓開發人員能夠實施符合Enterprise合規要求的文件保護策略。

密碼保護是指透過加密文件來限制未經授權的存取。 通常包含兩種類型的密碼:用於開啟文件的"使用者密碼"(或稱開啟密碼),以及用於控制編輯、列印及其他操作權限的"擁有者密碼"(或稱權限密碼)。 在您的 .NET 應用程式中實作 PDF 安全性時,理解這些不同的密碼類型至關重要。

IronPDF 提供您管理現有及新 PDF 檔案密碼與權限所需的一切功能。 可套用細粒度的元資料與安全性設定,包括將 PDF 文件設為不可列印、唯讀及加密等功能。 支援 128 位元加密、解密及密碼保護功能。 這些功能可與 IronPDF 的其他功能(如數位簽章PDF 壓縮)無縫整合。

快速入門:使用 IronPDF 設定 PDF 密碼與權限

立即開始使用 IronPDF,快速保護您的文件安全。 此範例展示如何在設定權限以防止未經授權列印時,同時設定使用者密碼與擁有者密碼。 遵循這些簡單步驟,您即可透過 C# .NET 有效保護 PDF 檔案,確保敏感資料的機密性。 無論您是處理 HTML 轉 PDF 作業,還是處理現有文件,IronPDF 都能讓您輕鬆地在應用程式中實施強大的安全措施。

PdfDocument.FromFile


如何為 PDF 設定密碼?

使用者密碼與擁有者密碼有何區別?

開啟並檢視此 PDF 文件時,必須輸入 User Password(亦稱為開啟密碼)。 若無此密碼,將完全無法開啟該 PDF 檔案。 相較之下,Owner Password(或權限密碼)則賦予對文件安全性設定的完全控制權。 當您使用擁有者密碼開啟 PDF 檔案時,即可修改權限、移除密碼,並無限制地存取所有文件功能。 此雙重密碼系統提供靈活的安全選項,適用於從簡單的文件保護到複雜的權限管理等各種使用情境。

為什麼我應該同時使用這兩種密碼類型?

同時使用這兩種密碼類型,可建立一套全面的安全策略。 使用者密碼可確保僅授權人員能檢視文件,而擁有者密碼則提供管理控制權。 此功能在企業環境中尤為實用,例如您可能希望員工僅能檢視文件(使用使用者密碼),但僅限管理員修改權限或解除保護(使用擁有者密碼)。 此外,此做法符合許多產業的合規要求,這些產業規定敏感文件必須設有不同級別的存取權限。

當使用者輸入各密碼時會發生什麼情況?

當使用者輸入 user password 時,將根據您設定的權限獲得該文件的讀取權限。 使用者可瀏覽內容,但可能受到限制,無法列印、複製文字或進行修改。 當輸入 owner password 時,所有限制將被解除,使用者將獲得該文件的完整管理權限,包括變更密碼及修改權限設定的能力。

我們有一份範例 PDF 檔案,希望使用 IronPDF 對其進行保護。 讓我們執行以下程式碼,為 PDF 檔案設定密碼。 在此範例中,我們將使用密碼 password123。 無論您是將 HTML 轉為 PDF,還是處理現有文件,此方法皆能無縫運作。

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-add-password.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Secret Information:</h1> Hello World");

// Password to edit the pdf
pdf.SecuritySettings.OwnerPassword = "123password";

// Password to open the pdf
pdf.SecuritySettings.UserPassword = "password123";

pdf.SaveAs("protected.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Secret Information:</h1> Hello World")

' Password to edit the pdf
pdf.SecuritySettings.OwnerPassword = "123password"

' Password to open the pdf
pdf.SecuritySettings.UserPassword = "password123"

pdf.SaveAs("protected.pdf")
$vbLabelText   $csharpLabel

最終產出如下所示的 PDF 檔案,您可輸入密碼 password123 進行檢視。

如何開啟設有密碼的 PDF 檔案?

FromFile 接受哪些參數?

FromFile 方法接受兩個主要參數:檔案路徑以及一個可選的密碼字串。 處理受密碼保護的 PDF 檔案時,必須將正確的密碼作為第二個參數提供。 此方法會自動偵測所提供的密碼是使用者密碼還是擁有者密碼,並授予相應的存取權限。 這種無縫整合讓您能在 C# 應用程式中輕鬆處理受保護的文件。

如何處理錯誤的密碼嘗試?

當輸入錯誤的密碼時,IronPDF 會拋出一個特定的例外,您可以擷取並妥善處理該例外。 最佳實務是透過實作 try-catch 區塊,以優雅地處理密碼輸入錯誤:

try 
{
    var pdf = PdfDocument.FromFile("protected.pdf", userPassword);
    // Process the PDF
}
catch (IronPdf.Exceptions.IronPdfPasswordException ex)
{
    // Handle incorrect password
    Console.WriteLine("Invalid password provided");
}
try 
{
    var pdf = PdfDocument.FromFile("protected.pdf", userPassword);
    // Process the PDF
}
catch (IronPdf.Exceptions.IronPdfPasswordException ex)
{
    // Handle incorrect password
    Console.WriteLine("Invalid password provided");
}
Imports IronPdf.Exceptions

Try
    Dim pdf = PdfDocument.FromFile("protected.pdf", userPassword)
    ' Process the PDF
Catch ex As IronPdfPasswordException
    ' Handle incorrect password
    Console.WriteLine("Invalid password provided")
End Try
$vbLabelText   $csharpLabel

開啟後可以移除密碼保護嗎?

是的,一旦您使用擁有者密碼開啟 PDF 檔案,即可透過 RemovePasswordsAndEncryption() 方法移除所有密碼保護。 當您需要分發先前受保護的文件,或將其整合至不支援密碼保護 PDF 的系統時,此功能便十分實用。

本節說明如何開啟設有密碼的 PDF 檔案。 PdfDocument.FromFile 方法有一個可選的第二個參數,即密碼。 請為此參數提供正確的密碼以開啟 PDF 檔案。 此功能可與 IronPDF 的其他功能(如合併 PDF擷取文字)完美整合。

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-open-password.cs
using IronPdf;

var pdf = PdfDocument.FromFile("protected.pdf", "password123");

//... perform PDF-tasks

pdf.SaveAs("protected_2.pdf"); // Saved as another file
Imports IronPdf

Private pdf = PdfDocument.FromFile("protected.pdf", "password123")

'... perform PDF-tasks

pdf.SaveAs("protected_2.pdf") ' Saved as another file
$vbLabelText   $csharpLabel

如何設定進階安全性與權限設定?

我可以控制哪些權限?

IronPDF 透過其 SecuritySettings 類別,提供對 PDF 權限的細部控制。 您可以管理各種權限,包括:

  • AllowUserAnnotations:控制使用者是否能新增註解與標註
  • AllowUserCopyPasteContent:限制文字與圖片的複製
  • AllowUserFormData:管理表單填寫功能
  • AllowUserPrinting:設定列印權限,並提供高品質或低解析度列印選項
  • AllowUserEditing:控制文件修改權限

這些權限與密碼保護功能相輔相成,可建立符合您特定需求的全面性安全政策。

權限與密碼之間如何相互作用?

權限設定的運作方式會根據密碼設定而有所不同。 若未設定密碼,權限設定雖會生效,但可能被 PDF 編輯軟體繞過。 僅需輸入使用者密碼,系統即會依據預設權限授予存取權限。 然而,當兩組密碼皆已設定時,使用者密碼僅提供受限存取權限,而擁有者密碼則會覆寫所有權限,賦予完全控制權。 此分層系統可確保針對不同使用者角色的適當存取控制。

我可以設定哪些元資料欄位?

PdfDocument 物件還包含可設定的元資料欄位,例如 AuthorModifiedDate。 其他元資料屬性包含 Creator 以及 Producer。 這些欄位對於文件管理系統及合規要求至關重要。 您亦可設定自訂元資料欄位,此功能對於內部追蹤與分類特別有用。 請參閱我們的詳細指南,進一步了解元資料管理

何時該使用 MakePdfDocumentReadOnly?

當您需要建立一份最終且不可編輯的文件版本,同時仍需維持某種程度的存取控制時,MakePdfDocumentReadOnly 方法是理想選擇。 此方法將密碼保護與權限限制整合於單一呼叫中,非常適合用於檔案歸檔、法律文件或必須保持不變的最終報告。 這在需要確保文件完整性的文件工作流程中特別有用。

您亦可停用使用者註解、使用者PRINT功能,以及以下所示的更多功能:

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-advanced.cs
using IronPdf;

// Open an Encrypted File, alternatively create a new PDF from HTML
var pdf = PdfDocument.FromFile("protected.pdf", "password123");

// 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;

// Save the secure PDF
pdf.SaveAs("secured.pdf");
Imports IronPdf

' Open an Encrypted File, alternatively create a new PDF from HTML
Private pdf = PdfDocument.FromFile("protected.pdf", "password123")

' 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

' Save the secure PDF
pdf.SaveAs("secured.pdf")
$vbLabelText   $csharpLabel

權限設定與文件密碼相關,其運作方式如下。 例如,將 AllowUserCopyPasteContent 屬性設定為 false 的目的是為了防止內容被複製/貼上:

  • 未設定密碼:若未設定密碼,內容的複製/貼上功能仍會被封鎖。
  • 設定使用者密碼:設定使用者密碼後,輸入正確密碼即可進行內容複製/貼上。
  • 已設定擁有者密碼:當設定擁有者密碼時,僅輸入使用者密碼將無法解鎖複製/貼上功能。 不過,輸入正確的擁有者密碼後,即可進行內容的複製/貼上。
顯示已停用安全功能且僅啟用PRINT功能的檔案權限對話方塊

一篇相關文章探討了預定義與自訂元資料。 點擊此連結了解更多:"如何設定與編輯 PDF 元資料"。

準備好探索更多可能性了嗎? 請點此查看我們的教學頁面:簽署與保護 PDF 文件

對於需要進階安全功能的 Enterprise 應用程式,建議考慮採用符合 PDF/A 標準的格式以實現長期文件保存,或透過 HSM 實施數位簽章以強化驗證機制。 IronPDF 的安全性功能可與 Azure 部署情境無縫整合,並支援多種渲染選項,以滿足您的特定需求。

常見問題

如何在 C# 中為 PDF 檔案新增密碼保護?

您可以透過設定 IronPDF 的 SecuritySettings 屬性,為 PDF 檔案新增密碼保護。只需載入您的 PDF 文件,然後將 UserPassword 屬性設定為需輸入密碼才能開啟檔案,並/或設定 OwnerPassword 屬性以控制編輯權限。IronPDF 支援 128 位元加密,以確保文件安全。

PDF 檔案中的「使用者密碼」與「擁有者密碼」有何區別?

在 IronPDF 中,開啟並檢視 PDF 文件需輸入使用者密碼(或開啟密碼),而擁有者密碼(或權限密碼)則控制使用者對文件可執行的操作,例如編輯、列印或複製內容。您可以透過 SecuritySettings.UserPassword 和 SecuritySettings.OwnerPassword 屬性,分別獨立設定這兩組密碼。

我可以防止使用者從我的 PDF 中列印或複製內容嗎?

是的,IronPDF 允許您對 PDF 文件設定細粒度的權限。您可以使用 SecuritySettings.Permissions 屬性來限制列印、複製或編輯等操作。例如,設定 Permissions.NoPrinting 將防止使用者列印文件,即使他們擁有使用者密碼亦然。

PDF 安全功能支援何種等級的加密?

IronPDF 支援 PDF 文件的 128 位元加密,為您的敏感檔案提供企業級安全性。當您使用 SecuritySettings 屬性為 PDF 文件設定密碼或權限時,此加密等級會自動套用。

如何移除現有 PDF 檔案的密碼保護?

若要使用 IronPDF 移除 PDF 的密碼保護,您首先需要透過向 FromFile 方法提供密碼來開啟受保護的文件。開啟後,您可以將 UserPassword 和 OwnerPassword 屬性重設為空字串以清除安全性設定,然後將文件儲存為無保護狀態。

將 HTML 轉換為 PDF 時,我可以新增安全性設定嗎?

是的,IronPDF 允許您在將 HTML 轉換為 PDF 後立即套用安全性設定。從 HTML 內容建立 PDF 後,您可以在儲存最終文件之前,透過存取 SecuritySettings 屬性來設定密碼和權限,確保轉換後的檔案從一開始就受到保護。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 18,926,724 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。