Python-based statistical model to auto-detect coordinates of Form fields on a PDF

Viewed 356

I want to detect coordinates of a fill-able PDF forms, more precisely bounding box coordinates of a text-box where a particular text field (String) is going to be placed.

Currently I'm following a hard-coded Python-Pdfminer approach using LTTBox to detect the coordinates based on certain string presence.

Example:

def identifyTextboxes(path, fieldName, fieldType):
    # reading the file and setting prams
    fp = open(path, 'rb')
    rsrcmgr = PDFResourceManager()
    laparams = LAParams()
    device = PDFPageAggregator(rsrcmgr, laparams=laparams)
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    pages = PDFPage.get_pages(fp)

    # defining key-terms of custodian:
    names=["Name of entity:", "Name of Investor (Please Print or Type)", "Print Name of Entity", "Name of Prospective Investor ", 
    "Investor's Name", "Name (the “Applicant” or “We”)", "On Behalf of:", "Name of Business"]

    num_pages = 0
    output = []
    for page in pages:
        temp_dict = {}
        interpreter.process_page(page)
        layout = device.get_result()
        num_pages += 1

        # fetching the coordinates of the text via bbox
        for lobj in layout:
            if isinstance(lobj, LTTextBox):
                (x, y, xw, yh), text = lobj.bbox, lobj.get_text()
                for name in names:
                    ResSearch = re.search(name, text)                
                    if ResSearch:break
                    field='textbox'
            if ResSearch:break
        if ResSearch:
            temp_dict['label'] = fieldName
            temp_dict['type'] = fieldType
            temp_dict["value"] = ''
            temp_dict['group'] = ''
            temp_dict["overlay"] = {'page': num_pages, 'left': (xw)-90, 'top':((y-10)-(yh-y))+90, 'height': 20, 'width':240}
            output.append(temp_dict)
            print(lobj)
    return output

In the code above, i detect places where the matching string appears from the list names and define the coordinate of the text-box on the right to the appearance based on LTTBox coordinates and a fixed with, as you can see on the return output['overlay'].

This process is very much hard-coded and fails when any unknown string or situation occurs in a PDF, hence not that robust.

I want to drive a more data driven statistical approach to detect the coordinates of a bounding box via CNN/RNN/CNN+RNN. I have gone through EAST detector, but that doesn't seem to solve the problem, so maybe training a more customized network should be perhaps more useful.

Please find the pictures attached for a better understanding if what the current code does. enter image description here enter image description here

I'm new to ML, i need guidance to construct a network of this kind. Any help is highly appreciated.

1 Answers

If the forms are proper PDF AcroForm fields, you can easily find them by just reading the PDF file.

Just look for text like this in your PDF file:

7 0 obj
<<
/Type /Annot 
/Subtype /Widget 
/Rect [ 87.539 495.187 139.289 511.890 ] 
/F 4 
/FT /Tx 
/H /N 
/R 0 
/Ff 4194304 
/BS << /W 1 /S /S  >>
/MK <</BC [ 0.267 0.267 0.267 ] /BG [ 0.996 0.839 0.804 ]  >>
/T (name1[first])
/TU (<FE><FF>)
/DV ()
/DA (/F2 9.9 Tf 0.000 g)
/NM (0007-5003)
/M (D:20181012063448)
>>

Another example:

23 0 obj
<</Type/Annot/Subtype/Widget/F 4
/Rect[165.7 388.3 315.7 402.5]
/FT/Tx
/P 1 0 R
/T(Address 1 Text Box)
/V <FEFF>
/DV <FEFF>
/MaxLen 40
/DR<</Font 6 0 R>>
/DA(0 0 0 rg /F3 11 Tf)
/AP<<
/N 60 0 R
>>
>>
endobj

The coordinates of the field are the numbers after /Rect, in the sequence left, bottom, right, top.

It could be that the object in question is compressed. In that case you won't see it as text. In such a case I would recommend using mutool clean -d input.pdf readable.pdf to decompress all objects in the PDF file, making the file readable with a text editor. mutool is a command-line tool that comes with mupdf.

Related