1

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 :)

Chetra_S11
  • 66
  • 6
  • 1
    "I want multiple people to be able to sign on the same document". Are you adding them one at a time after previous signatures have been added? Or are the empty signature fields all present in the PDF before the first signing? – Ryan Jan 19 '22 at 18:17
  • Thank you for ur reply. I want to be able to add one signature at a time, it can be a document with no signature or previously signed document. – Chetra_S11 Jan 20 '22 at 02:14
  • Is it just PDFTron validation you are concerned about, or you want all the signatures to validate in any/all vendor that does signature validation? – Ryan Jan 20 '22 at 19:18

1 Answers1

2

Yes you can add multiple Signature Fields and Sign them using PDFTron SDK. Just repeat the SignOnNextSave code you had above, and always make sure to save using e_incremental.

Note though that currently PDFTron Validation would fail once two or more signatures added, as this would strictly speaking violate what the PDF specification describes for default behavior. Though I checked with Adobe Acrobat at least and it was fine with 2+ signatures, so at least Adobe appears to be doing something proprietary and non-standard. I cannot say about other vendors, if that is important for you.

Ryan
  • 2,473
  • 1
  • 11
  • 14
  • Thank you for the explanation. Can you tell me if there is a way to change the default behavior, so that validation will not fail if I add two or more signature on a document? – Chetra_S11 Jan 21 '22 at 01:48
  • "validation will not fail" you mean PDFTron validation? At this time no, but supporting this Adobe specific logic is in our product backlog, but unscheduled at this time. If this is critical for you, please reach out directly to pdfron.com support to discuss further. – Ryan Jan 21 '22 at 22:54