这个快速示例指导如何使用 Python 从 PDF 中删除签名。它包含使用 Python 从 PDF 中删除签名的基本步骤以及展示该功能的可运行示例代码。您还将学习删除 PDF 中的所有签名或根据其名称删除特定签名。
使用 Python 从 PDF 中删除签名的步骤
- 配置 IDE 以使用 Aspose.PDF for Python 通过 .NET 并删除签名
- 实例化 PdfFileSignature 类的对象
- 将源 PDF 文件与 PdfFileSignature 对象绑定
- 使用 get_sign_names() 方法访问所有签名名称的列表
- 遍历所有签名并使用 remove_signature() 方法删除所需的签名
- 删除签名后保存输出 PDF
这些步骤设计了使用 Python 从 PDF 中删除电子签名的过程。首先,源 PDF 文件与 PdfFileSignature 类实例绑定,该实例包含 get_sign_names() 方法以访问所有签名和 remove_signature() 方法以删除单个签名。在最后一步中,您可以根据需要将生成的 PDF 文件保存在磁盘上或内存流中。
使用 Python 从 PDF 中删除数字签名的代码
import aspose.pdf as pdf | |
# Set the source directory path | |
filePath = "C://Words//" | |
# Load the license in your application to remove signature from PDF | |
pdfSignatureLicense = pdf.License() | |
pdfSignatureLicense.set_license(filePath + "Conholdate.Total.Product.Family.lic") | |
#Create a PdfFileSignature object | |
pdfSign = pdf.facades.PdfFileSignature() | |
#Open the signed PDF document | |
pdfSign.bind_pdf(filePath + "Signed.pdf") | |
#Get a list of signature names | |
sigNames = pdfSign.get_sign_names(True) | |
#Remove all the signatures from the PDF file | |
for index in range(sigNames.length): | |
print("Removed {sigNames[index]}") | |
pdfSign.remove_signature(sigNames[index]) | |
#Save the updated PDF file | |
pdfSign.save(filePath + "RemoveSignature_out.pdf") | |
print("Signature removed from PDF") |
此代码展示了如何使用 Python 从 PDF 中删除签名。 PdfFileSignature 类实例用于删除签名,但是,该类包含许多其他相关功能,如验证源文件是否经过认证、确认 LTV 启用标志、验证文件是否包含签名等等,仅举几例。请注意,在使用 remove_signature() 方法时,还可以设置标志以仅删除签名或同时删除签名和字段。
本主题已指导我们从 PDF 文件中删除签名。如果您有兴趣学习保护 PDF 文件,请参阅 如何使用Python使用密码保护PDF文件 上的文章。