通过 Python 保护 PDF

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

Aspose.PDF for Python for .NET Logo

如何使用使用 Python 工具保护 PDF 文件

為了保護 PDF 文件,請使用功能強大且易於使用的 API—Aspose.PDF for Python via .NET。開啟 PyPI,搜尋 aspose-pdf 並安裝。或者,執行以下命令:

Console

pip install aspose-pdf

通過Python保護 PDF


您需要 Aspose.PDF for .NET 在您的環境中嘗試代碼。

  1. 使用 Document 實例載入 PDF。
  2. 建立 DocumentPrivilege 物件並設定選項。
  3. 呼叫 Document.Encrypt 方法使用 AES-128 加密 PDF。
  4. 以 PDF 格式儲存結果

保護 PDF - Python

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

import aspose.pdf as apdf

from os import path
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)
document_privilege = apdf.facades.documentPrivilege.forbid_all
document_privilege.allow_screen_readers = True
document.encrypt("userpassword", "ownerpassword",
    document_privilege, apdf.CryptoAlgorithm.AE_SX128, False)
document.save(path_outfile)