** Need Help,i have to find the Signature field in pdf and add digital sign on it, this is my Python code but it is not working, signed pdf (output) doesn't have signature, i want it to find the signature field on its own and then add the signature (basically signature field is a button when we click on it then it asks to Sign and Continue** ---
import os
from PyPDF2 import PdfReader
from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter
from pyhanko.sign import signers
# Set the folder path, certificate path, and certificate password
folder_path = folderPath
certificate_path = CertificatePath
certificate_password = Password
# Get all PDF files in the folder
for filename in os.listdir(folder_path):
if filename.endswith(".pdf"):
pdf_file = os.path.join(folder_path, filename)
print(f"Signing {pdf_file}...")
# try:
# Open the PDF file
pdf_reader = PdfReader(pdf_file)
# Find the signature field
signature_field_name = None
for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[0]
annots = page.get('/Annots')
print(annots)
if annots is not None:
for annot in annots:
annotation_obj = annot.get_object()
print(annotation_obj)
signature_field_name = "Digital Signature"
if signature_field_name:
with open(pdf_file, "rb") as f:
w = IncrementalPdfFileWriter(f)
with open(certificate_path,'rb') as cert_file:
pfx_data = cert_file.read()
cms_signer = signers.SimpleSigner.load_pkcs12(pfx_file= certificate_path,passphrase= certificate_password)
print(cms_signer)
signature_meta=signers.PdfSignatureMetadata(
field_name= signature_field_name
)
print(signature_meta)
# out=signers.sign_pdf()
out = signers.sign_pdf(
w,
signature_meta=signers.PdfSignatureMetadata(field_name= signature_field_name
),signer=cms_signer
)
# print(f"Out is{out}")
# Save the signed PDF
signed_pdf_file = os.path.join(folder_path, f"signed_{filename}")
with open(signed_pdf_file, "wb") as f:
f.write(out.getbuffer())
print(f"Signed {pdf_file} successfully.")
else:
print(f"No signature field found in {pdf_file}")
# except Exception as e:
# print(f"Error signing {pdf_file}: {e}")
Подробнее здесь: https://stackoverflow.com/questions/796 ... ure-in-pdf
Python добавить цифровую подпись в PDF ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как поставить цифровую подпись в PDF-файле с помощью смарт-карты и iTextSharp?
Anonymous » » в форуме C# - 0 Ответы
- 41 Просмотры
-
Последнее сообщение Anonymous
-