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

digital signature - Custom background image positioning within stamp field using pyHanko python library - Stack Overflow

programmeradmin3浏览0评论

I’ve been testing the pyHanko library for digitally signing PDF documents. The digital signing process went smoothly using a .pfx file, and the signature is visible in Adobe Acrobat.

However, I’ve encountered an issue with positioning the visible stamp. While my image appears, I can't figure out how to adjust its placement within the field to avoid overlapping with the text.

If anyone has experience with this library, I would greatly appreciate your guidance.

Here is the relevant part of my code:

from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter
from pyhanko.sign import signers
from pyhanko.sign.fields import append_signature_field, SigFieldSpec
from pyhanko.sign.signers import PdfSigner, SimpleSigner
from pyhanko.stamp import TextStampStyle
from pyhanko.pdf_utils.images import PdfImage
from pyhanko.pdf_utils.text import TextBoxStyle

signer = SimpleSigner.load_pkcs12(pfx_file='certificate.pfx')

with open('test.pdf', 'rb') as doc:
    writer = IncrementalPdfFileWriter(doc)

    # Define a signature field to sign
    append_signature_field(
        writer,
        sig_field_spec=SigFieldSpec(
            "Signature1",  # Field name
            box=(150, 100, 350, 200)  # Signature box position
        )
    )

    # Load the background image
    background_image = PdfImage("signature.jpg")  

    # Define the stamp style without border
    stamp_style = TextStampStyle(
        border_width=0,
        background=background_image,
        stamp_text="Approved by: John X\non %(ts)s",  # Add custom text with newline
        timestamp_format='%d.%m.%Y %H:%M:%S %Z',  # Format for the timestamp
        background_opacity=0.5,  # Set the opacity (0.0 to 1.0)
        text_box_style=TextBoxStyle(font_size=9, border_width=0)  # Set the font size
            )


    # Perform the signing process
    with open('signed_test.pdf', 'wb') as out_file:
        PdfSigner(
            signers.PdfSignatureMetadata(
                field_name='Signature1', 
                reason="Document approved", 
                location="New York"
            ),
            signer=signer,
            stamp_style=stamp_style
        ).sign_pdf(
            writer,
            output=out_file
        )

print("PDF signed successfully and saved as 'signed_test.pdf'")`

I tried to follow documentation on .4.0/api-docs/pyhanko.stamp.html but didn't figure it how as there is no example.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论