I'm currently working on a Digital Signature project in Python which can create digital signature on PDF documents and verify them. I was able to add a single digital signature to PDF Files using the code below:
PDFNet.Initialize()
doc = PDFDoc(pdf_path)
page1 = doc.GetPage(1)
# Create signature field
sign_field = doc.CreateDigitalSignatureField("Signature")
widgetAnnot = SignatureWidget.Create(doc, Rect(0, 0, 100, 100), sign_field)
page1.AnnotPushBack(widgetAnnot)
sign_field.SignOnNextSave(pkcs12_path, "")
# Save file
output_path = get_only_path(pdf_path) + "output.pdf"
doc.Save(output_path, SDFDoc.e_incremental)
PDFNet.Terminate()
I was also able to verify the document with the code below:
# Get certificate
dsfield_iter = doc.GetDigitalSignatureFieldIterator()
while dsfield_iter.HasNext():
sign_field = dsfield_iter.Current()
cert = sign_field.GetSignerCertFromCMS()
dsfield_iter.Next()
# Save certificate for verification
cert_path = get_only_path(pdf_path) + "certificate.cer"
with open(cert_path, "wb") as cer:
# cert.GetData() return DER format
cer.write(cert.GetData())
# Verifying signature
opts = VerificationOptions(VerificationOptions.e_compatibility_and_archiving)
opts.AddTrustedCertificate(cert_path)
result = doc.VerifySignedDigitalSignatures(opts)
However, I have a problem. I want multiple people to be able to sign on the same document with their self signed certificate in different signature fields and also able to verify the document. I used my code for the same process but was unable to verify the signature after more than one people sign on it. I would like to get help on this, please kindly share your thoughts :)