如何設置 PDF 密碼和權限
密碼保護涉及對文件進行加密以限制未經授權的訪問。 通常包括兩種類型的密碼:用戶密碼(或開啟密碼),用於打開文件的密碼,以及擁有者密碼(或權限密碼),用於控制編輯、列印和其他操作的權限。
IronPDF 支援您現有和新PDF文件所需的所有密碼和權限功能。 可以應用精細的元數據和安全設置,包括限制 PDF 文件為不可列印、唯讀和加密的能力; 支援128位元加密、解密和密碼保護。
開始使用 IronPDF
立即在您的專案中使用IronPDF,並享受免費試用。
如何在C#中使用密碼和權限保護PDF

- 下載C#庫以使用密碼保護PDF
- 設置 擁有者密碼 防止 PDF 文件被編輯的屬性
- 設置 使用者密碼 防止PDF文件被打開的屬性
- 使用128位加密對PDF文件進行加密
- 請提供密碼給
從文件
打開PDF文件的方法
為PDF設置密碼
我們有一個範例 PDF 文件我們希望使用IronPDF來保護的內容。 讓我們執行以下代碼以對PDF添加密碼。 在此範例中,我們將使用密碼 password123。
: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")
結果如下的 PDF,您可以通過輸入密碼 password123 來查看。
打開一個設有密碼的PDF文件
本節介紹如何打開具有密碼的 PDF。 PdfDocument.FromFile
方法有第二個可選參數,即是密碼。 提供正確的密碼至此參數以開啟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
高級安全性和權限設置
PdfDocument對象還具有您可以設置的元數據字段,例如作者和修改日期。 您也可以禁用用戶註釋、用戶打印等功能,如下所示:
: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")
許可權設置與文檔密碼相關,其行為如下。 例如,將 AllowUserCopyPasteContent 屬性設置為 false 旨在防止複製/粘貼內容:
- 未設置密碼:未設置密碼時,複製/粘貼內容將保持被阻止。
- 用戶密碼設定:當設定了用戶密碼,輸入正確的密碼將允許複製/粘貼內容。
- 設置擁有者密碼:當設置擁有者密碼後,僅輸入用戶密碼將無法解鎖複製/粘貼功能。 然而,輸入正確的擁有者密碼將允許複製/粘貼內容。
一篇密切相關的文章討論了預定義的元數據和自定義元數據。 透過以下連結了解更多:如何設定和編輯PDF元數據.