最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Programmatically Add a Required Signature Field in an Adobe PDF Using Python, R or etc - Stack Overflow

programmeradmin2浏览0评论

I am trying to programmatically add a required signature field to a PDF using both pymupdf (PyMuPDF) and pyhanko. My goal is to ensure that Adobe Acrobat recognizes the field as a required signature field.

I have two different implementations:

Using PyHanko (append_signature_field):

I can successfully add a signature field that appears in Adobe Acrobat, but I am unable to make it required.

Adobe allows me to sign the field, but it does not enforce the requirement.

Using PyMuPDF (Widget):

I can add a signature field with field_flags = 0, which should allow modifications.

However, Adobe Acrobat treats the PDF as already signed, preventing further edits.

import fitz  # PyMuPDF

def add_signature_field_pymupdf(input_pdf, output_pdf):
    doc = fitz.open(input_pdf)
    page = doc[0]  # First page
    page_height = page.rect.height

    # Define coordinates for the signature field
    x1, y1, x2, y2 = 100, 600, 300, 650
    y1_pdf, y2_pdf = page_height - y2, page_height - y1  # Flip Y-coordinates

    # Create a signature widget
    w = fitz.Widget()
    w.field_name = "AdobeSignHere"
    w.field_label = "Please Sign Here"
    w.field_value = None
    w.text_font = "TiRo"
    w.text_fontsize = 12
    w.rect = fitz.Rect(x1, min(y1_pdf, y2_pdf), x2, max(y1_pdf, y2_pdf))
    w.field_type = fitz.PDF_WIDGET_TYPE_SIGNATURE
    w.field_flags = 2  # Set as required

    page.add_widget(w)

    doc.save(output_pdf, pretty=True)
    print(f"Signature field added: {output_pdf}")

# Run function
output_pdf_pymupdf = "signed_pymupdf.pdf"
add_signature_field_pymupdf(pdf_path, output_pdf_pymupdf)


from pyhanko.sign.fields import append_signature_field, SigFieldSpec
from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter

def add_signature_field_pyhanko(input_pdf, output_pdf):
    with open(input_pdf, "rb") as pdf_in:
        w = IncrementalPdfFileWriter(pdf_in)

        # Define signature field specifications
        sig_field_spec = SigFieldSpec(
            sig_field_name="AdobeSignHere",
            on_page=0,  # First page
            box=(100, 600, 300, 650),  # Position
            empty_field_appearance=True,  # Adobe requires a visible field
            combine_annotation=True,  # Keep annotation and field together
            readable_field_name="Please Sign Here"  # Tooltip for the field
        )

        append_signature_field(w, sig_field_spec)

        with open(output_pdf, "wb") as pdf_out:
            w.write(pdf_out)

    print(f"Signature field added using PyHanko: {output_pdf}")

# Run function
output_pdf_pyhanko = "signed_pyhanko.pdf"
add_signature_field_pyhanko(pdf_path, output_pdf_pyhanko)

This creates a signature field, but when opening the PDF in Adobe Acrobat, it thinks the document is already signed and prevents modifications.

I suspect the issue might be with the field flags or signature field setup, but I can't find the correct combination to make the field required and still signable.

What I've Tried: Setting w.field_flags = 2 (read-only), which prevents signing.

Using w.is_signed = False, but PyMuPDF does not allow manually setting this.

Changing fonts (times-roman, TiRo), but no effect.

Comparing field properties with an Adobe-created required signature field, but I couldn't match it exactly.

Expected Outcome: The PDF should contain a signature field that is marked as required in Adobe Acrobat.

The field should allow a user to sign it.

The document should remain editable until signed.

Question: How can I correctly define a required but signable signature field using PyMuPDF or PyHanko so that Adobe Acrobat enforces the requirement but does not lock the PDF before signing?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论