-
From what I gather in the documentation, it seems like it can't edit or add signature fields, only read info from existing signature fields? What I'm trying to do:
Just wondering if what I am trying to accomplish is possible in this tool. Finding my fields and parsing through their metadata is no problem, I'm just running into roadblocks when it comes to the last step (changing/creating the widgets). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Sorry, this is not supported currently. |
Beta Was this translation helpful? Give feedback.
-
If anyone else happens to stumble in here looking to do the same process, I found a solution. I use LibreOffice Draw to create the PDF forms initially, which doesn't export proper PDF signature fields. I just create placeholder text fields in LibreOffice Draw with names that start with
It's worth noting that I ran into a known bug (#3216) but you can easily fix it by removing line 1701 from A note on the coordinates: In my case, it is an 8.5" x 11" document, so the total height (in units) is Here's my full testing script that gives me the results I want: import shutil, fitz, os
from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter
from pyhanko.sign.fields import SigFieldSpec, append_signature_field
og_file = "general_release.pdf"
new_file = "test.pdf"
print("Setting up new file...")
os.remove(new_file)
shutil.copyfile(og_file, new_file)
boxes = {}
print(f"Getting signature field data and removing placeholders...")
pn = 0
fdoc = fitz.open(new_file)
for page in fdoc:
for field in page.widgets():
n = field.field_name
if n.startswith("sig") or n.startswith("init"):
print(f"Found field '{n}' on page {pn}...")
_type = 1 if n.startswith("sig") else 2
boxes[n] = {
"page": pn,
"type": _type,
"box": (
field.rect.x0,
792-field.rect.y0,
field.rect.x1,
792-field.rect.y1
)
}
field.field_name = n + "_orig"
field.update()
try:
page.delete_widget(field)
print("...removed!")
except Exception as e:
print(f"...failed to remove.\nERROR: {e}")
pn += 1
fdoc.saveIncr()
fdoc.close()
print("Adding new signature fields to document...")
try:
with open(new_file, 'rb+') as _doc:
d = IncrementalPdfFileWriter(_doc)
for _name in boxes.keys():
_dict = boxes[_name]
append_signature_field(d, SigFieldSpec(
sig_field_name=_name,
on_page=_dict["page"],
box=_dict["box"]
))
d.write_in_place()
print("...done!")
except Exception as e:
print(f"...failed to add signature fields.\nERROR: {e}") |
Beta Was this translation helpful? Give feedback.
Sorry, this is not supported currently.