通过 C# 保护 PDF

使用 AES-128 加密为 PDF 设置文档权限。使用 Aspose.PDF for .NET 以编程方式修改 PDF 文档

如何使用使用 .NET 工具保护 PDF 文件

为了保护 PDF 文件,我们将使用 Aspose.PDF for .NET API,这是一款功能丰富、功能强大且易于使用的适用于 net 平台的文档处理 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以从软件包管理器控制台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF

通过 C# 保护 PDF


你需要 Aspose.PDF for .NET 在你的环境中试用代码。

  1. 使用 “文档” 实例加载 PDF。
  2. 创建 documentPrivilege 对象并设置选项。
  3. 调用 Document.Encrypt 方法使用 AES-128 保护 PDF。
  4. 将结果保存为 PDF 格式

保护 PDF-C#

此示例代码显示了如何保护 PDF 文件

var inputFile = Path.Combine(dataDir, "input.pdf");
var outputFile = Path.Combine(dataDir, "output.pdf");
var document = new Aspose.Pdf.Document(inputFile);
var documentPrivilege = Aspose.Pdf.Facades.DocumentPrivilege.ForbidAll;
documentPrivilege.AllowScreenReaders = true;
document.Encrypt("userpassword", "ownerpassword", 
    documentPrivilege, 
    Aspose.Pdf.CryptoAlgorithm.AESx128, false);
document.Save(outputFile);