通过 C# 加密 PDF 文件

使用 C# 为采用 AES-128 加密的 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。 1.创建 documentPrivilege 对象并设置选项。 1.调用 Document.Encrypt 方法使用 AES-128 保护 PDF。 1.将结果保存为 PDF 格式

保护 PDF-C#。

<% protect.code-block.subtitle %>


    using (var document = new Aspose.Pdf.Document("template.pdf"))
{
    // instantiate DocumentPrivileges object
    // apply restrictions on all privileges
    var documentPrivilege = Aspose.Pdf.Facades.DocumentPrivilege.ForbidAll;
    // allow screen reading
    documentPrivilege.AllowScreenReaders = true;
    // encrypt the file with User and Owner password.
    document.Encrypt("userpassword", "ownerpassword", documentPrivilege, Aspose.Pdf.CryptoAlgorithm.AESx128, false);
    // save updated document
    document.Save("protected.pdf");
}