如何設置PDF密碼和權限在C#中
IronPDF使您可以使用密碼和權限來保護PDF文件,支援用戶密碼用於開啟文件,以及所有者密碼用於控制編輯、列印和複製權限的128位RC4加密。 這項全面的安全功能可讓開發人員實施符合企業合規要求的文件保護策略。
您的企業在每年的PDF安全和合規訂閱上花費過多。考慮使用IronSecureDoc,它提供數位簽名、編輯、加密和保護等SaaS服務管理解決方案,僅需一次性支付。探索IronSecureDoc文件
密碼保護是指對文件進行加密,以限制未經授權的存取。 它通常包含兩種類型的密碼:使用者密碼(或開啟密碼),用於開啟文件;以及擁有者密碼(或權限密碼),用於控制編輯、列印和其他操作的權限。 在您的.NET應用程式中實現PDF安全性時,了解這些不同類型的密碼至關重要。
IronPDF 支援您對現有和新 PDF 檔案的密碼和權限所需的一切功能。 可套用細緻的元資料和安全性設定,包括限制 PDF 文件不可列印、唯讀和加密的功能。 支持128位RC4加密、解密和密碼保護。 這些功能可以與其他IronPDF功能結合,例如數位簽名和PDF壓縮。
快速開始:使用IronPDF設置PDF密碼和權限立即使用 IronPDF 快速保護您的文件。 本範例說明如何設定使用者和擁有者密碼,同時設定權限以防止未經授權的列印。 按照這些簡單的步驟,您可以使用 C# .NET 有效地保護您的 PDF 文件,確保您的敏感資料保持機密性。 IronPDF使您輕鬆在應用程式中實現可靠的安全措施。
PdfDocument.FromFile
最小工作流程(5 個步驟)

- 下載用於為 PDF 設定密碼保護的 C# 函式庫
- 設定
OwnerPassword屬性以防止編輯 PDF 檔案 - 設定
UserPassword屬性以防止開啟 PDF 檔案 - 使用 128 位元加密對 PDF 檔案進行加密
- 將密碼傳給
FromFile方法以開啟 PDF 文件
如何設定PDF的密碼?
使用者密碼和所有者密碼有何不同?
User Password(也稱為開啟密碼)是打開並查看PDF文件所需的。 如果沒有這個密碼,根本無法存取 PDF。 相反,Owner Password(或權限密碼)授予對文件的安全設置的全面控制。 當您使用擁有者密碼開啟 PDF 時,您可以修改權限、移除密碼,並且可以不受限制地存取所有文件功能。 此雙密碼系統可針對不同的使用情境提供彈性的安全選項,從簡單的文件保護到複雜的權限管理情境。
為什麼我應該使用這兩種類型的密碼?
使用這兩種密碼類型可建立全面的安全策略。 使用者密碼可確保僅授權個人能夠檢視文件,而所有者密碼則提供管理控制。 這在商業環境中特別有用,您可能希望員工可以檢視文件 (使用使用者密碼),但只有經理可以修改權限或移除保護 (使用擁有者密碼)。 此外,此方法符合許多產業的合規要求,這些產業規定敏感文件必須有不同的存取層級。
當用戶輸入每個密碼時會發生什麼?
當用戶輸入user password時,他們將根據您設置的權限獲得文件的讀取訪問權。 他們可以檢視內容,但可能會被限制列印、複製文字或修改。 當輸入owner password時,所有限制都會被解除,且用戶會獲得對文件的完全管理特權,包括更改密碼和修改權限設置的能力。
我們有一個想要使用IronPDF保護的樣例PDF文件。 讓我們執行以下程式碼,為 PDF 新增密碼。 在這個例子中,我們將使用密碼password123 。 此方法無論您是從HTML創建PDF還是處理現有文件都有效。
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")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")結果是以下 PDF 文件,您可以透過輸入密碼password123來查看該文件。
使用了哪種加密算法?
設置密碼時,文件將默認加密為128位RC4。 改用SecuritySettings.EncryptionType來選擇更強的演算法:
| 價值 | 算法 | 讀者兼容性 |
|---|---|---|
Rc4_128 | 128位RC4(PDF 1.4)- 默認 | 幾乎每個PDF閱讀器 |
Aes_128 | 128位AES(PDF 1.5+) | Adobe Acrobat 7及更高版本 |
Aes_256 | 256位AES(PDF 2.0)- 最強 | Adobe Acrobat 9及更高版本 |
RC4仍然是默認設置,以使現有代碼不受影響,但它在密碼學上較弱,建議對新文件使用AES。 如需更全面的算法選擇和元數據加密處理,請參閱加密PDF教程。
如何打開具有密碼的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 TryImports 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打開後可以移除密碼保護嗎?
是的,一旦您使用所有者密碼打開PDF,您可以使用RemovePasswordsAndEncryption()方法移除所有密碼保護。 當您需要散佈先前受保護的文件,或將其整合至不支援密碼保護 PDF 的系統時,這將非常有用。
本節介紹如何開啟帶有密碼的 PDF 檔案。 PdfDocument.FromFile方法有第二個可選參數,這就是密碼。 請為此參數提供正確的密碼以開啟PDF檔案。 此功能與其他IronPDF功能相得益彰,例如合併PDF和提取文本。
using IronPdf;
var pdf = PdfDocument.FromFile("protected.pdf", "password123");
//... perform PDF-tasks
pdf.SaveAs("protected_2.pdf"); // Saved as another fileusing IronPdf;
var pdf = PdfDocument.FromFile("protected.pdf", "password123");
//... perform PDF-tasks
pdf.SaveAs("protected_2.pdf"); // Saved as another fileImports IronPdf
Private pdf = PdfDocument.FromFile("protected.pdf", "password123")
'... perform PDF-tasks
pdf.SaveAs("protected_2.pdf") ' Saved as another fileImports IronPdf
Private pdf = PdfDocument.FromFile("protected.pdf", "password123")
'... perform PDF-tasks
pdf.SaveAs("protected_2.pdf") ' Saved as another file如何設定高級安全和權限設置?
我能控制哪些權限?
IronPDF透過SecuritySettings類提供對PDF權限的詳細控制。 您可以管理各種權限,包括
AllowUserAnnotations:控制用戶是否可以添加評論和註釋AllowUserCopyPasteContent:限制文字和圖像複製AllowUserFormData:管理表單填寫功能AllowUserPrinting:設置列印權限,可選擇高品質或低解析度列印
這些權限與密碼保護配合使用,可建立符合您特定需求的全面安全政策。
權限如何與密碼互動?
權限設定會根據密碼組態的不同而有不同的表現。 當未設定密碼時,權限會被強制執行,但有可能被 PDF 編輯軟體繞過。 只需一個使用者密碼,輸入後即可根據定義的權限授予存取權。 然而,當同時設定兩個密碼時,使用者密碼會提供受限制的存取權限,而擁有者密碼則會覆寫所有權限,賦予完全的控制權。 此分層系統可確保針對不同使用者角色進行適當的存取控制。
我可以設置哪些元數據字段?
PdfDocument對象還有一些可以設置的元資料欄位,例如Author和ModifiedDate。 額外的元資料屬性包括Title,Subject,Keywords,Creator,和Producer。 這些領域對於文件管理系統和合規要求是不可或缺的。 您也可以設定自訂的 metadata 欄位,這對於內部追蹤和分類尤其有用。 在我們詳細的指南中了解更多元數據管理。
我應該什麼時候使用MakePdfDocumentReadOnly?
MakePdfDocumentReadOnly方法非常適合您需要創建文件的最終、不可編輯版本時,同時保持一定程度的訪問控制。 此方法將密碼保護與限制權限結合在單一呼叫中,非常適合存檔用途、法律文件或應保持不變的最終報告。 這在需要確保文檔完整性的文檔工作流程中特別有用。
您也可以停用使用者註解、使用者列印等功能,如下所示:
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");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")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")權限設定與文件密碼相關,其行為方式如下。 例如,將AllowUserCopyPasteContent屬性設為false旨在防止內容的複製/粘貼:
- 未設置密碼:沒有密碼的情況下,複製/粘貼內容仍然被阻止。
- 設置了使用者密碼:當設置了使用者密碼後,輸入正確的密碼將允許複製/粘貼內容。
- 設置了所有者密碼:當設置了所有者密碼,僅輸入使用者密碼不會解鎖複製/粘貼功能。 但是,輸入正確的所有者密碼後,即可複製/貼上內容。

一篇密切相關的文章討論了預定義元資料和自訂元資料。 請透過點擊此連結了解更多:「如何設置和編輯PDF元數據」。
準備好看看您還能做些什麼嗎? 查看我們的教程頁面:簽署和保護PDF
對於需要先進安全功能的企業應用程式,考慮探索PDF/A合規以便長期保存文件,或實施與HSM的數位簽名以增強驗證。 IronPDF的安全功能與Azure部署方案整合,並支援多種渲染選項以符合您的具體需求。
常見問題
如何使用IronPDF為PDF設置密碼?
要在IronPDF中為PDF設置密碼,請使用SecuritySettings屬性。您可以設置OwnerPassword來控制例如編輯和列印等權限,而UserPassword則限制查看PDF的存取。
在IronPDF中,使用者密碼與擁有者密碼有什麼區別?
使用者密碼,也稱為開啟密碼,是查看PDF所需的密碼。擁有者密碼或許可權密碼,提供對PDF安全設置的完整管理控制,包括許可權和密碼管理。
為什麼我應該為我的PDF使用使用者密碼和擁有者密碼?
使用使用者密碼和擁有者密碼提供了分層的安全方法。使用者密碼限制了只有授權的觀眾才能訪問,而擁有者密碼允許管理控制,使得安全管理更加詳細。
PDF 安全功能支援何種加密等級?
在使用SecuritySettings屬性設定密碼或許可權時,IronPDF自動應用128位元RC4加密。您可以透過將SecuritySettings.EncryptionType指派為PdfEncryptionType.Aes_128或PdfEncryptionType.Aes_256來選擇更強的算法。
我可以在使用IronPDF開啟PDF後移除密碼保護嗎?
是的,一旦使用擁有者密碼打開PDF,您可以使用IronPDF中的RemovePasswordsAndEncryption()方法來移除所有密碼保護。
設置PDF密碼時應用哪種加密算法?
設置密碼會默認使用128位元RC4加密文件。將SecuritySettings.EncryptionType指派為PdfEncryptionType.Aes_128或PdfEncryptionType.Aes_256來選擇AES。RC4保持為默認設置以避免影響現有代碼,但建議對新文件使用AES。
我可以在創建新的PDF時添加安全設置嗎?
是的,IronPDF允許您在創建新的PDF後立即應用安全設置。在創建PDF後,您可以存取SecuritySettings屬性來設置密碼和權限,然後再儲存最終文件,確保您的新文件從一開始就受到保護。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。