数字签名广泛应用于各个领域,包括网络安全、电子商务、法律文件等。 Aspose.Total for Python via .NET 提供了一组 API,用于处理各种类型文档(包括 PDF、Word、Excel 等)中的数字签名。 这些 API 使得以编程方式对文档进行数字签名变得更加容易。
以电子方式签署文件
添加数字签名的通用步骤。
- 您需要在 Python 项目中初始化所选的 Aspose API。这通常涉及安装相关的 Aspose 库并将其导入到您的代码中。
- 使用 Aspose API 将要登录的文档加载到 Python 应用程序中。这可能涉及指定文件路径或从流中读取文档。
- 根据您的要求配置数字签名属性,例如签名证书、签名位置、外观以及任何其他选项。您通常需要数字证书进行签名。
- 使用 Aspose API 方法将数字签名应用于文档。此过程将根据您在上一步中提供的配置应用签名。
- 应用数字签名后,将修改后的文档保存到文件或流中。
Python 代码 - 对 PDF 进行数字签名
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.pdf as pdf | |
import aspose.pydrawing as drawing | |
# Set the source directory path | |
filePath = "C://Words//" | |
# Load the license in your application to crop the PDF | |
pdfCropLicense = pdf.License() | |
pdfCropLicense.set_license(filePath + "Conholdate.Total.Product.Family.lic") | |
#Load the PDF file to crop | |
pdfDoc = pdf.Document(filePath + "GeneratedPdf.pdf") | |
#Instantiate the PdfFileSignature for the loaded PDF document | |
signature = pdf.facades.PdfFileSignature(pdfDoc) | |
#Load the certificate file along with the password | |
pkcs = pdf.forms.PKCS7(filePath + "sample.pfx", "123456789") | |
#Assign the access permissions | |
docMdpSignature = pdf.forms.DocMDPSignature(pkcs, pdf.forms.DocMDPAccessPermissions.FILLING_IN_FORMS) | |
#Set the rectangle for the signature placement | |
rect = drawing.Rectangle(150, 650, 450, 150) | |
#Set signature appearance | |
signature.signature_appearance = "sample.jpg" | |
#Sign the PDF file with the certify method | |
signature.certify(1, "Signature Insert Reason", "Contact", "Location", True, rect, docMdpSignature) | |
#Save digitally signed PDF file | |
signature.save("Digitally Signed PDF.pdf") |