首页 > 解决方案 > 如何使用 python 创建包含签名字段的 PDF 文件?

问题描述

为了能够使用基于令牌的 DSC 签署 PDF 文档,我的 PDF 中需要一个所谓的签名字段。

这是一个矩形字段,您可以使用例如 Adob​​e Reader 或 Adob​​e Acrobat 填写数字签名。

您可以使用 Adob​​e Reader 或 Adob​​e Acrobat 等数字签名填充矩形字段的图像。

我想用 Python 创建这个可签名的 PDF。

我从纯文本或 .docx 格式的富文本文档(图像和文本)开始。

如何在 Python 中生成包含此字段的 PDF 文件?

标签: pythonpdfpdf-generationdigital-signature

解决方案


不幸的是,我找不到任何(免费)解决方案。只是签署 PDF 文档的 Python 程序。

但是有一个名为PDFTron的 Python PDF SDK可以免费试用。这是一篇特定文章的链接,展示了如何“将认证签名字段添加到 PDF 文档并对其进行签名”。

# Open an existing PDF
doc = PDFDoc(docpath)

page1 = doc.GetPage(1)

# Create a text field that we can lock using the field permissions feature.
annot1 = TextWidget.Create(doc.GetSDFDoc(), Rect(50, 550, 350, 600), "asdf_test_field")
page1.AnnotPushBack(annot1)

# Create a new signature form field in the PDFDoc. The name argument is optional;
# leaving it empty causes it to be auto-generated. However, you may need the name for later.
# Acrobat doesn't show digsigfield in side panel if it's without a widget. Using a
# Rect with 0 width and 0 height, or setting the NoPrint/Invisible flags makes it invisible. 
certification_sig_field = doc.CreateDigitalSignatureField(cert_field_name)
widgetAnnot = SignatureWidget.Create(doc, Rect(0, 100, 200, 150), certification_sig_field)
page1.AnnotPushBack(widgetAnnot)

...

# Save the PDFDoc. Once the method below is called, PDFNet will also sign the document using the information provided.
doc.Save(outpath, 0)

推荐阅读