如何设置PDF的密码和权限

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

密码保护涉及加密文档以限制未经授权的访问。它通常包括两种类型的密码:用户密码 (或打开密码)打开文档所需的密码,以及所有者密码 (或权限密码)控制编辑、打印和其他操作的权限。

IronPDF 支持您对现有和新建 PDF 文件的密码和权限的一切需求。可应用细粒度的元数据和安全设置,包括限制 PDF 文档不可打印、只读和加密的功能,全部支持 128 位加密、解密和密码保护。


适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronPDFNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变PDF。

适用于PDF的C# NuGet库 nuget.org/packages/IronPdf/
Install-Package IronPdf

考虑安装 IronPDF DLL 直接。下载并手动安装到您的项目或GAC表单中: IronPdf.zip

手动安装到你的项目中

下载DLL

为 PDF 设置密码

这里我们有一个 PDF 示例 我们要使用 IronPDF 保护的 PDF。让我们执行以下代码为 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#

输入密码password123,就可以查看下面的 PDF 文件。

打开有密码的 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 对象还具有元数据字段,你可以对其进行设置,如 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 元数据."