通过 Python 向 PDF 添加数字签名

使用服务器端 Aspose.PDF 为 Python for .NET API 生成和验证数字签名。原生和高性能库

如何使用 Python for .NET 工具向 PDF 文件添加数字签名

為了在 PDF 檔案中加入數位簽名,我們將使用 Aspose.PDF for Python via .NET,這是一個功能強大且易於使用的 API。打開 PyPI,搜尋 aspose-pdf 並安裝它。或者,執行以下命令:

Console

pip install aspose-pdf

通過Python將數位簽名添加到 PDF


你需要 通过.NET for Python 的 Aspose.PDF 才能在你的环境中试用代码。

  1. 使用 Document 實例載入 PDF。
  2. 載入所有 PDF 文件,並指定其完整路徑。
  3. 設定文字樣式,例如字體、字號、顏色等。
  4. 儲存 PDF 文件,您將獲得已插入簽署的文件。
  5. 使用 PdfFileSignature.VerifySigned() 方法驗證簽章的有效性。

使用-Python 向 PDF 文件添加电子签名

此示例代码说明如何为 PDF 页面签名-Python

Input file:

File not added

Certificate file:

File not added

Output format:

Output file:

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)

signature = apdf.facades.PdfFileSignature(document)
# Use PKCS7/PKCS7Detached objects
pkcs = apdf.forms.PKCS7("my_certificate.pfx", "Pa$$w0rd12345")

signature.sign(1, True, apdf.Rectangle(300, 100, 400, 200, True), pkcs)
# Save output PDF file
signature.save(path_outfile)