IRONSOFTWAREHOME
PDF加密和解密
using IronPdf;
using System;

//Open an Encrypted File, alternatively create a new PDF from Html

var pdf = PdfDocument.FromFile("encrypted.pdf", "password");

//Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto";
pdf.MetaData.Keywords = "SEO, Friendly";
pdf.MetaData.ModifiedDate = DateTime.Now;

//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;

// change or set the document encrpytion password
pdf.Password = "my-password";
pdf.SaveAs("secured.pdf");
Imports IronPdf
Imports System

'Open an Encrypted File, alternatively create a new PDF from Html

Dim pdf = PdfDocument.FromFile("encrypted.pdf", "password")

'Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto"
pdf.MetaData.Keywords = "SEO, Friendly"
pdf.MetaData.ModifiedDate = DateTime.Now

'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

' change or set the document encrpytion password
pdf.Password = "my-password"
pdf.SaveAs("secured.pdf")
NuGet Download
PM > Install-Package IronPdf
PDF加密和解密

PDF加密和解密

PDF安全性是处理PDF文件时的重要方面,而IronPDF非常重视PDF安全性。 使用IronPDF,您可以通过应用自定义元数据和安全设置来加密和解密PDF文件。 IronPDF支持对加密文件的128位加密,能够解密先前加密的文件(前提是您拥有访问文件所需的正确密码),并对现有或新创建的PDF文档应用密码保护。

using IronPDF 加密和解密您的 PDF 文件的第一步是创建一个新的 PDF 文件,或者如我们在此处所做的那样加载一个现有文件。 在使用FromFile方法传递了两个参数:第一个是PDF的文件路径,第二个是打开加密PDF的密码。

下一行是编辑我们的加密PDF的元数据。 Author字段,使用我们自定义的字符串值。 每个元数据字段可以使用MetaData属性内定制的任何其他内容特别有帮助。

下一行从PDF文档中移除任何当前的密码和加密。 这个解密过程允许您保存PDF而没有任何加密,或者清除它以便您为PDF添加新的加密设置,这就是我们接下来要做的。 setting被您希望更改的实际设置替换。 例如,pdf.SecuritySettings.MakePdfDocumentReadOnly将PDF设置为只读。 PdfSecuritySettings类包含许多选项,允许完全自定义您的PDF的安全设置,从不允许用户注释到控制打印权限; 这个类确保适当的PDF安全性得到安全而高效的处理。

pdf.Password用于更改您PDF的密码或设置新密码,如果当前没有密码。 这个密码使用强大的128位加密加密PDF文件,防止未经授权的访问。 一旦您对元数据和安全设置感到满意,您可以使用SaveAs()将PDF文档保存到所需的文件位置。 学习使用 IronPDF 设置 PDF 权限和密码

在 GitHub 上查看

准备开始了吗?

Nuget Downloads 21,105,021版本:2026.9刚刚发布

Key in blue circle

立即获取免费的 30 天试用版密钥。

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

OR
bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥。
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 “IronPDF”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

或在此处下载 Windows 安装程序。

  1. 下载并解压 IronPDF 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键点击引用。选择浏览,“IronPDF.dll”

$999 起