如何設置 PDF 密碼和權限

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

密碼保護涉及加密文件以限制未經授權的存取。它通常包括兩種類型的密碼:使用者密碼 (或開啟密碼),用於打開文件的密碼,以及擁有者密碼 (或權限密碼), 用於控制編輯、列印和其他操作的權限。

IronPDF 支援您現有和新 PDF 文件所需的所有密碼和權限設置。可以應用詳細的元數據和安全設置,包括將 PDF 文件限制為無法列印、僅供閱讀和加密,支援 128 位加密、解密和密碼保護。


C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip

手動安裝到您的項目中

下載DLL

設置 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")
VB   C#

結果如下的 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
VB   C#

高級安全性和許可權設置

PdfDocument 物件也有您可以設置的MetaData字段,如 AuthorModifiedDate。您還可以禁用使用者註解、使用者列印等,如下所示:

: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")
VB   C#

權限設定與文件密碼有關,其行為如下。例如,將 AllowUserCopyPasteContent 屬性設置為 false 旨在防止內容的複製/貼上:

  • 未設置密碼:未設置密碼時,內容的複製/貼上仍然被阻止。
  • 設置使用者密碼:當設置了使用者密碼時,輸入正確的密碼將允許內容的複製/貼上。
  • 設置擁有者密碼:當設置了擁有者密碼時,僅輸入使用者密碼將無法解鎖複製/貼上功能。然而,輸入正確的擁有者密碼將允許內容的複製/貼上。
權限視窗

有關的文章討論了預定義和自定義的元數據。請通過以下鏈接了解更多信息:如何設定和編輯PDF元數據.