如何设置PDF的密码和权限
密码保护涉及加密文档以限制未经授权的访问。它通常包括两种类型的密码:用户密码 (或打开密码)打开文档所需的密码,以及所有者密码 (或权限密码)控制编辑、打印和其他操作的权限。
IronPDF 支持您对现有和新建 PDF 文件的密码和权限的一切需求。可应用细粒度的元数据和安全设置,包括限制 PDF 文档不可打印、只读和加密的功能,全部支持 128 位加密、解密和密码保护。
如何在 C# 中使用密码和权限保护 PDF
- 下载用密码保护 PDF 的 C# 库
- 设置 用户密码 属性以防止 PDF 文件被编辑
- 设置 用户密码 属性,以防止打开 PDF 文件
- 使用 128 位加密技术加密 PDF 文件
- 提供密码
发件人文件
打开 PDF 文档的方法
开始在您的项目中使用IronPDF,并立即获取免费试用。
查看 IronPDF 上 Nuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变PDF。
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")
输入密码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
高级安全和权限设置
PdfDocument 对象还具有元数据字段,你可以对其进行设置,如 Author 和 ModifiedDate。您还可以禁用用户注释、用户打印等功能,如下图所示:
: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 元数据."