PDF控件Spire.PDF for .NET【安全】演示:将加密或解密 PDF 文件

当涉及到在 Internet 上共享机密文档时,PDF 加密是一项至关重要的任务。通过使用强密码加密 PDF 文件,您可以保护文件数据免遭未经授权的人员访问。在某些情况下,可能还需要删除密码才能公开文档。在本文中,您将了解如何使用Spire.PDF for .NET以编程方式加密或解密 PDF 文件。

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式**(qun:767755948 )**

Spire.PDF for.net下载 Spire.PDF for java下载

安装适用于 .NET 的 Spire.PDF

首先,您需要将 Spire.PDF for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。

复制代码
PM> Install-Package Spire.PDF
使用密码加密 PDF 文件

加密PDF文件有两种密码------打开密码权限密码。前者设置为打开 PDF 文件,而后者设置为限制打印、内容复制、注释等。如果 PDF 文件使用两种类型的密码保护,则可以使用任一密码打开它。

Spire.PDF for .NET 提供的PdfSecurity.Encrypt **(string openPassword, stringpermissionPassword, PdfPermissionsFlags Permissions, PdfEncryptionKeySize keySize)**方法允许您设置打开密码和权限密码来加密 PDF 文件。详细步骤如下。

  • 创建一个PdfDocument对象。
  • **使用PdfDocument.LoadFromFile()**方法加载示例 PDF 文件。
  • 使用PdfDocument.Security属性获取文档的安全参数。
  • **使用PdfSecurity.Encrypt(string openPassword, stringpermissionPassword, PdfPermissionsFlags Permissions, PdfEncryptionKeySize keySize)**方法使用打开密码和权限密码加密 PDF 文件。
  • **使用PdfDocument.SaveToFile()**方法保存结果文件。

【C#】

复制代码
using Spire.Pdf;
using Spire.Pdf.Security;

namespace EncryptPDF
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();

//Load a sample PDF file
pdf.LoadFromFile(@"E:\Files\sample.pdf");

//Encrypt the PDF file with password
pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit);

//Save the result file
pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF);
}
}
}

VB.NET

复制代码
mports Spire.Pdf
Imports Spire.Pdf.Security

Namespace EncryptPDF
Class Program
Private Shared Sub Main(ByVal args As String())

'Create a PdfDocument object
Dim pdf As PdfDocument = New PdfDocument()

'Load a sample PDF file
pdf.LoadFromFile("E:\Files\sample.pdf")

'Encrypt the PDF file with password
pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print Or PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit)

'Save the result file
pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace
删除密码以解密 PDF 文件

当您需要删除PDF文件的密码时,可以在调用**PdfSecurity.Encrypt(string openPassword, stringpermissionPassword, PdfPermissionsFlagspermissions, PdfEncryptionKeySize keySize, stringoriginalPermissionPassword)**方法时将打开密码和权限密码设置为空。详细步骤如下。

  • 创建一个PdfDocument对象。
  • **使用PdfDocument.LoadFromFile(字符串文件名,字符串密码)**方法加载带密码的加密 PDF 文件。
  • 使用PdfDocument.Security属性获取文档的安全参数。
  • **通过使用PdfSecurity.Encrypt(string openPassword, stringpermissionPassword, PdfPermissionsFlagspermissions, PdfEncryptionKeySize keySize, stringoriginalPermissionPassword)**方法将打开密码和权限密码设置为空来解密 PDF 文件。
  • **使用PdfDocument.SaveToFile()**方法保存结果文件。

【C#】

复制代码
using Spire.Pdf;
using Spire.Pdf.Security;

namespace DecryptPDF
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();

//Load the encrypted PDF file with password
pdf.LoadFromFile("Encrypt.pdf", "open");

//Set the password as empty to decrypt PDF
pdf.Security.Encrypt(string.Empty, string.Empty, PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit, "permission");

//Save the result file
pdf.SaveToFile("Decrypt.pdf", FileFormat.PDF);
}
}
}

VB.NET

复制代码
Imports Spire.Pdf
Imports Spire.Pdf.Security

Namespace DecryptPDF
Class Program
Private Shared Sub Main(ByVal args As String())

'Create a PdfDocument object
Dim pdf As PdfDocument = New PdfDocument()

'Load the encrypted PDF file with password
pdf.LoadFromFile("Encrypt.pdf", "open")

'Set the password as empty to decrypt PDF
pdf.Security.Encrypt(String.Empty, String.Empty, PdfPermissionsFlags.[Default], PdfEncryptionKeySize.Key128Bit, "permission")

'Save the result file
pdf.SaveToFile("Decrypt.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace

以上便是如何将加密或解密 PDF 文件,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,~

相关推荐
兰文彬6 分钟前
PPT转化成PDF脚本
pdf·powerpoint
夏天的风991 小时前
本地部署PLM系统,如何用 ZeroNews 实现远程访问?
安全·远程工作
wanhengidc1 小时前
高性价比云手机挑选指南
运维·网络·安全·游戏·智能手机
索迪迈科技2 小时前
记一次 .NET 某中医药附属医院门诊系统 崩溃分析
windows·c#·.net·windbg
拉法豆粉4 小时前
三方软件测试可移植性测试哪些内容
数据库·安全
ChaITSimpleLove4 小时前
零代码入侵:Kubernetes 部署时自动注入 kube-system UID 到 .NET 9 环境变量
kubernetes·.net·环境变量·uid·kube-system·集群环境唯一id
@AfeiyuO5 小时前
vue3 实现将页面生成 pdf 导出(html2Canvas + jspdf)
前端·pdf·vue
午夜游鱼5 小时前
Go 泛型实战:一行代码封装 sync.Pool,性能与安全兼得
开发语言·安全·golang
小码编匠6 小时前
WPF 多线程更新UI的两种实用方案
后端·c#·.net
半桔7 小时前
【Linux手册】信号量与建造者模式:以 PV 操作保证并发安全,分步组装构建复杂对象
linux·运维·安全·建造者模式