python - 如何使用 python 创建包含签名字段的 PDF 文件?
问题描述
为了能够使用基于令牌的 DSC 签署 PDF 文档,我的 PDF 中需要一个所谓的签名字段。
这是一个矩形字段,您可以使用例如 Adobe Reader 或 Adobe Acrobat 填写数字签名。
我想用 Python 创建这个可签名的 PDF。
我从纯文本或 .docx 格式的富文本文档(图像和文本)开始。
如何在 Python 中生成包含此字段的 PDF 文件?
解决方案
不幸的是,我找不到任何(免费)解决方案。只是签署 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)
推荐阅读
- c# - 模拟错误的执行多重响应
- try-catch-finally - 为什么finally块存在?
- c++ - 接口的菱形继承 (C++)
- c# - 如何确定为什么 .editorconfig 中的某些设置未应用?
- sql - 浮动百分比
- sql - 在多个连接记录中进行 SQL 字符串搜索
- swift - 如何创建collectionview header,如搜索一个instagram标签?
- node.js - 基于目标客户端与服务器代码的不同构建
- python - 配置两个远程系统之间的路径映射以使断点在 PyDev 中工作
- ruby-on-rails - Rails ActionView::Template::Error undefined method `count' 解析器