How can I determine if a signature field is signed using the PDFNet SDK?

154 views
Skip to first unread message

Michael Mitchell

unread,
Feb 25, 2021, 3:25:52 PM2/25/21
to PDFTron SDK
Q: How can I determine if a signature field is signed using the PDFNet SDK?

A: The short answer is that you'll need to manually identify if a signature actually exists within the signature widget itself.

To do this, you can determine if an ink or stamp annotation exists within a signature field which is what happens when a user inserts a signature in the PDFTron WebViewer for instance.

The sample code below in Python shows how to do this.

    # Iterate over all fields
    itr = doc.GetFieldIterator()
    while itr.HasNext():

        # Examine all signature widgets
        if itr.Current().GetType() == Field.e_signature and itr.Current().IsAnnot():
            signature_found = False

            print("Signature field name: " + itr.Current().GetName())
       

            print("Traversing all annotations in the document...")
            page_num = 1
            page_itr = doc.GetPageIterator()

            sig_widget = Annot(itr.Current().GetSDFObj())
            sig_widget_rect = sig_widget.GetRect()

            # Iterate through all annotations in the document
            while page_itr.HasNext():
                page_num = page_num + 1
                page = page_itr.Current()
                num_annots = page.GetNumAnnots()
                i = 0
                while i < num_annots:
                    annot = page.GetAnnot(i)
                    if not annot.IsValid():
                        continue

                    if annot.GetType() == Annot.e_Ink or annot.GetType() == Annot.e_Stamp:

                       # Check if ink or stamp annotation is in signature field widget
                       ink_rect = annot.GetRect()
                       if (sig_widget_rect.Contains(ink_rect.x1, ink_rect.y1) or
                           sig_widget_rect.Contains(ink_rect.x1, ink_rect.y2) or
                           sig_widget_rect.Contains(ink_rect.x2, ink_rect.y1) or
                           sig_widget_rect.Contains(ink_rect.x2, ink_rect.y2)):
                           signature_found = True    


                       
# Check if ink or signature widget overlaps with stamp or ink annotation
                       if (ink_rect.Contains(sig_widget_rect.x1, sig_widget_rect.y1) or
                           ink_rect.Contains(sig_widget_rect.x1, sig_widget_rect.y2) or
                           ink_rect.Contains(sig_widget_rect.x2, sig_widget_rect.y1) or
                           ink_rect.Contains(sig_widget_rect.x2, sig_widget_rect.y2)):
                           signature_found = True    

                    i = i + 1
                page_itr.Next()


            print()
            if signature_found:
                print("Signature found")
            else:
                print("No signature found")

            print("------------------------------")
        itr.Next()

   
    print("Done.")

Note that this will register any ink annotation or stamp annotation within a signature widget as being the signature even if it was not meant for that particular field. This will, however, detect any signature field that is missing a signature annotation that is not a stamp or ink. 
Reply all
Reply to author
Forward

Reply all
Reply to author
Forward
0 new messages