通过 Python 加密 PDF 文件

使用 Python 为采用 AES-128 加密的 PDF 格式设置文档权限

如何使用 Python for .NET 库保护 PDF 文件

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

Python Package Manager Console

pip install aspose-pdf

通过 Python 保护 PDF


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

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

保护 PDF-Python。

<% protect.code-block.subtitle %>

    document = Document("template.pdf")
    # instantiate DocumentPrivileges object
    # apply restrictions on all privileges
    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")