如何使用 C# 设置密码和权限保护 PDF 文件 | IronPDF 教程

How to Set Password and Permissions on a PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English
role="alert"> 您的企业在PDF安全性和合规性的年度订阅上花费过多。考虑使用IronSecureDoc,它提供数字签名、涂黑、加密和保护等SaaS服务管理的解决方案,仅需一次性付费。探索IronSecureDoc文档

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

IronPDF支持现有和新PDF文件所需的所有密码和权限功能。 可以应用详细的元数据和安全设置,包括将PDF文档限制为不可打印、只读和加密的能力; 支持128位加密、解密和密码保护。

快速入门:使用IronPDF设置PDF密码和权限

快速入门使用IronPDF保护您的文档。 此示例展示了如何设置用户和所有者密码,同时配置权限以防止未经授权的打印。 通过遵循这些简单步骤,您可以使用C# .NET有效地保护您的PDF文件,确保您的敏感数据保持机密。 IronPDF使您能够在应用程序中轻松实现强大的安全措施。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = IronPdf.PdfDocument.FromFile("document.pdf");
    pdf.SecuritySettings.OwnerPassword = "owner123";
    pdf.SecuritySettings.UserPassword = "user123";
    pdf.SecuritySettings.Permissions = IronPdf.Security.Permissions.NoPrinting;
    pdf.SaveAs("secured_document.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小化工作流程(5个步骤)

如何使用密码和权限保护您的PDF(C#)
  1. 下载 C# 库以密码保护PDF
  2. 设置 OwnerPassword 属性以防止PDF文件被编辑
  3. 设置 UserPassword 属性以防止PDF文件被打开
  4. 使用128位加密加密PDF文件
  5. FromFile 方法中提供密码以打开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")
$vbLabelText   $csharpLabel

结果是以下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
$vbLabelText   $csharpLabel

<hr

高级安全和权限设置

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")
$vbLabelText   $csharpLabel

权限设置与文档密码相关,并按以下方式运行。 例如,将 AllowUserCopyPasteContent 属性设置为 false 旨在防止内容的复制粘贴:

  • 未设置密码:没有密码的情况下,内容的复制粘贴仍然被阻止。
  • 设置用户密码:设置用户密码时,输入正确的密码将允许复制粘贴内容。
  • 设置所有者密码:设置所有者密码时,只输入用户密码将无法解锁复制粘贴功能。 然而,输入正确的所有者密码将允许复制粘贴内容。
class="content-img-align-center">
class="center-image-wrapper"> 权限窗口

一个密切相关的文章讨论了预定义和自定义元数据。 通过遵循此链接了解更多信息:"如何设置和编辑PDF元数据。"

准备好看看您还能做些什么吗? 查看我们的教程页面:签名和保护PDFs

常见问题解答

如何使用 C# 为 PDF 设置密码保护?

您可以使用 IronPDF 在 C# 中使用密码保护 PDF。从 NuGet 下载 IronPDF 库,然后设置 OwnerPassword 以防止编辑,设置 UserPassword 以防止未经授权的打开。最后,用 128 位加密加密 PDF。

在 PDF 安全中,用户密码和所有者密码的区别是什么?

在 PDF 安全中,用户密码用于打开文档,而所有者密码控制权限,如编辑、打印和其他操作。IronPDF 支持这两种类型的密码,以确保全面的文档保护。

如何使用 C# 打开密码保护的 PDF?

要使用 C# 打开密码保护的 PDF,您可以使用 IronPDF 的 PdfDocument.FromFile 方法,将正确的密码作为第二个参数提供以访问文档。

IronPDF 支持何种加密级别来保护 PDF?

IronPDF 支持 128 位加密来保护 PDF,为敏感文档提供强有力的保护。这确保了您的 PDF 安全免遭未经授权的访问。

我可以使用 C# 控制 PDF 权限如打印和注释吗?

是的,使用 IronPDF,您可以通过在 PdfDocumentSecuritySettings 中设置AllowUserAnnotationsAllowUserPrinting 等属性来控制 PDF 权限,如打印和注释。

如何使用 IronPDF 自定义 PDF 元数据?

IronPDF 允许您通过设置 AuthorModifiedDate 等字段来自定义 PDF 元数据。您可以配置这些元数据字段以增强文档的详细信息和安全性。

使用 IronSecureDoc 相对于年度 PDF 安全订阅有哪些优势?

IronSecureDoc 提供了一种经济高效的解决方案,用于管理数字签名、修订、加密和保护一次性付款,而不是每年的订阅。这对企业来说可能是一个更经济的选择。

设置所有者密码如何影响 PDF 用户权限?

在 IronPDF 中设置所有者密码可以让您控制用户权限。例如,即使设置了用户密码,仅输入用户密码也无法解锁某些功能,如复制/粘贴,除非同时提供正确的所有者密码。

我在哪里可以找到更多关于 PDF 安全功能的文档?

更多有关 PDF 安全功能的文档,包括管理 IronSecureDoc 等 SaaS 服务,可以在 IronSoftware 的文档页面上找到,提供详细的信息和说明。

IronPDF 是否完全支持 .NET 10 来设置 PDF 密码和权限?

是的。IronPDF 完全兼容 .NET 10,无需任何特殊配置,即可在 .NET 10 项目中使用其密码保护、安全设置和权限控制(例如用户密码与所有者密码、打印、注释、复制/粘贴)。它在 .NET 10 中“开箱即用”,适用于桌面应用程序和 Web 应用程序。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布