Automatically Recognize Invoices from Different Vendors using C#

When working in a paperless office, businesses receive hundreds of different forms and invoices from different vendors. It is often a major pain point and bottleneck to manually find, extract, and store all the necessary information. Thankfully with the LEADTOOLS Document SDK and our patented Forms Recognition technology, everything can be easily automated to improve workflow productivity and efficiency.

With LEADTOOLS, users need only to create templates, also known as master forms, for each of the different invoices or form types. These master forms are then stored in a repository and used to automatically recognize which type of filled form is currently being processed.

In the demonstration below, we take a directory of filled forms and compare them to a list of master forms. We will first load the filled form that we wish to recognize along with the master form that we wish to compare it to.

Loading a Filled Form

Filled forms can span multiple pages, so we need to read the image data from the filled form and add each page to an instance of the FormRecognitionAttributes class.

private static FormRecognitionAttributes LoadFilledFormAttributes(string filledFormName)
{
 FormRecognitionAttributes filledFormAttributes = recognitionEngine.CreateForm(null);

 RasterImage currentForm = codecs.Load(filledFormName, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1);

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

 recognitionEngine.CloseForm(filledFormAttributes);

 return filledFormAttributes;
}

Loading a Master Form

Loading a master form is as easy as reading the master form’s .bin file and loading it into an instance of the FormRecognitionAttributes class.

private static FormRecognitionAttributes LoadMasterFormAttributes(string masterFormName)
{
 FormRecognitionAttributes masterFormAttributes = new FormRecognitionAttributes();
 masterFormAttributes.SetData(File.ReadAllBytes(masterFormName));

 return masterFormAttributes;
}

Finding the Right Match

Now we are ready to loop through an entire directory full of filled forms and find which master form they correspond to.

private static void RecognizeForms()
{
 Console.WriteLine("Recognizing Forms\n");

 string[] formsToRecognize = Directory.GetFiles(filledFormsDirectory, "*.tif", SearchOption.AllDirectories);

 string[] masterFileNames = Directory.GetFiles(masterFormsDirectory, "*.bin", SearchOption.AllDirectories);

 foreach (string filledFormName in formsToRecognize)
 {
  FormRecognitionAttributes filledFormAttributes = LoadFilledFormAttributes(filledFormName);

  string resultMessage = "";

  foreach (string masterFileName in masterFileNames)
  {
   FormRecognitionAttributes masterFormAttributes = LoadMasterFormAttributes(masterFileName);

   //Compares the master form to the filled form
   FormRecognitionResult recognitionResult = recognitionEngine.CompareForm(masterFormAttributes, filledFormAttributes, null);

   //When the Recognition Engine compares the two documents it also sets a confidence level for how closely the engine thinks the two documents match
   if (recognitionResult.Confidence >= AllowedConfidenceLevel)
   {
    resultMessage = $"Form {Path.GetFileNameWithoutExtension(filledFormName)} has been recognized as a(n) {Path.GetFileNameWithoutExtension(masterFileName)} with a confidence level of {recognitionResult.Confidence}";
    break;
   }

   resultMessage = $"The form {Path.GetFileNameWithoutExtension(filledFormName)} failed to be recognized with a confidence level of {recognitionResult.Confidence}";
  }

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

Stay Tuned

In the next blog we will show how to read the information from a filled form using the master form found in this post.

Try Our FREE Evaluation Today!

Add any of our advanced technology to your projects by downloading our entire collection of libraries available in our FREE LEADTOOLS Evaluation SDK.

Not sure where to start? Let our experts show you! Schedule a demo for more information on how our award-winning SDKs will help take your application development to the next level.

For pricing or licensing questions, contact our sales team via email or call us at 704-332-5532.

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

Leave a Reply

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