Forms Processing API Tutorial: Recognize and Process a Form

Automate your data entry problems away with a state-of-the-art forms processing API. Whether you’re working with customer surveys, tax documents, or billing records every industry uses forms daily to conduct business. Moving data from a paper to a digital medium can be a time consuming hassle. That’s why LEADTOOLS has developed exclusive capabilities that extract text from images containing any combination of machine-printed text, handwritten text, MICR, MRZ, and OMR fields. LEADTOOLS will automatically detect and recognize everything! Below are the main steps to quickly and accurately process various form types regardless of how the data is formatted.

First, we need to initialize the forms engine. This does all the hard work of reading and recognizing the data:

static void InitFormsEngines() 
{ 
Console.WriteLine("Initializing Engines"); 
codecs = new RasterCodecs(); 
recognitionEngine = new FormRecognitionEngine(); 
processingEngine = new FormProcessingEngine(); 
formsOCREngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); 
formsOCREngine.Startup(codecs, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime"); 
OcrObjectsManager ocrObjectsManager = new OcrObjectsManager(formsOCREngine); 
ocrObjectsManager.Engine = formsOCREngine; 
recognitionEngine.ObjectsManagers.Add(ocrObjectsManager); 

Console.WriteLine("Engines initialized successfully"); 
}   

Forms recognition requires a master form and a filled form. A master form contains blank fields and serves as a template to specify zones. A filled form is a one that contains data in the fields.

The next step is to specify the master forms:

private static void CreateMasterFormAttributes() 
{ 
Console.WriteLine("Processing Master Form"); 
string[] masterFileNames = Directory.GetFiles(@"C:\LEADTOOLS21\Resources\Images\Forms\MasterForm Sets\OCR", "*.tif", SearchOption.AllDirectories); 

foreach (string masterFileName in masterFileNames) 
{ 
string formName = Path.GetFileNameWithoutExtension(masterFileName); 
using (RasterImage image = codecs.Load(masterFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1)) 
{ 
FormRecognitionAttributes masterFormAttributes = recognitionEngine.CreateMasterForm(formName, Guid.Empty, null); 
for (int i = 0; i < image.PageCount; i++) 
{ 
image.Page = i + 1; 
recognitionEngine.AddMasterFormPage(masterFormAttributes, image, null); 
} 
recognitionEngine.CloseMasterForm(masterFormAttributes); 
File.WriteAllBytes(formName + ".bin", masterFormAttributes.GetData()); 
} 
} 
Console.WriteLine("Master Form Processing Complete"); 
Console.WriteLine("============================================================="); 
}   

Finally, we’re ready to read the filled forms:

private static void RecognizeForm()
{
Console.WriteLine("Recognizing Form\n");
var GetProjectDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string formToRecognize = @"C:\LEADTOOLS21\Resources\Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif";
using (RasterImage image = codecs.Load(formToRecognize, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1))
{
FormRecognitionAttributes filledFormAttributes = recognitionEngine.CreateForm(null);

for (int i = 0; i < image.PageCount; i++)
{
image.Page = i + 1;
recognitionEngine.AddFormPage(filledFormAttributes, image, null);
}
recognitionEngine.CloseForm(filledFormAttributes);

string resultMessage = "The form could not be recognized";
string[] masterFileNames = Directory.GetFiles(GetProjectDirectory, "*.bin");

foreach (string masterFileName in masterFileNames)
{
string fieldsfName = Path.GetFileNameWithoutExtension(masterFileName) + ".xml";
string fieldsfullPath = Path.Combine(@"C:\LEADTOOLS21\Resources\Images\Forms\MasterForm Sets\OCR", fieldsfName);
processingEngine.LoadFields(fieldsfullPath);
FormRecognitionAttributes masterFormAttributes = new FormRecognitionAttributes();
masterFormAttributes.SetData(File.ReadAllBytes(masterFileName));
FormRecognitionResult recognitionResult = recognitionEngine.CompareForm(masterFormAttributes, filledFormAttributes, null);
if (recognitionResult.Confidence >= 80)
{
List<PageAlignment> alignment = new List<PageAlignment>();
for (int k = 0; k < recognitionResult.PageResults.Count; k++)
alignment.Add(recognitionResult.PageResults[k].Alignment);

resultMessage = $"This form has been recognized as a {Path.GetFileNameWithoutExtension(masterFileName)}";
ProcessForm(image, alignment);
break;
}
}

Console.WriteLine(resultMessage, "Recognition Results");
Console.WriteLine("=============================================================\n");
}
}

private static void ProcessForm(RasterImage image, List<PageAlignment> alignment)
{
processingEngine.OcrEngine = formsOCREngine;
string resultsMessage = string.Empty;

processingEngine.Process(image, alignment);
foreach (FormPage formPage in processingEngine.Pages)
foreach (FormField field in formPage)
if (field != null)
resultsMessage = $"{resultsMessage}{field.Name} = {(field.Result as TextFormFieldResult).Text}\n";

if (string.IsNullOrEmpty(resultsMessage))
Console.WriteLine("No fields were processed", "FieldProcessing Results");
else
Console.WriteLine(resultsMessage, "Field ProcessingResults");
} 

There you have it! This is all that’s needed to extract data from filled forms. For a more in depth look, refer to the tutorial on how to recognize and process a form.

Free Evaluation!

Download the LEADTOOLS SDK for free straight from our site. This trial is good for 60 days and comes with unlimited chat and email support.

Need Help?

Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team ( sales@leadtools.com ) or call us at 704-332-5532.

About 

This entry was posted in Forms Recognition and Processing and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *