Auto Recognize and Process a Form - Console C#

This tutorial shows how to create a master set of forms, and then automatically recognizes and processes a form using the LEADTOOLS High-Level Form Interface.

Overview  
Summary This tutorial covers how to recognize and process a form in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Auto Recognize and Process a Form - Console C# tutorial.

Create the Project and Add LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the following necessary LEADTOOLS references.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed:

The local DLLs are located at <INSTALL_DIR>\LEADTOOLS 20\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to Files to be Included in Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Initialize AutoFormsEngine, RasterCodes, IOcrEngine, and DiskMasterFormsRepository

With the project created, the references added, and the license set, coding can begin.

In Solution Explorer, open Program.cs. In the Program class, add two new methods called InitFormsEngines() and RecognizeAndProcessForm(). Then add the following code to initialize the AutoFormsEngine, RasterCodes, IOcrEngine, and DiskMasterFormsRepository objects.

C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Forms.Processing; 
using Leadtools.Forms.Recognition; 
using Leadtools.Forms.Auto; 
C#
// Add these global members 
static AutoFormsEngine autoEngine;  
static RasterCodecs codecs;  
static IOcrEngine ocrEngine;  
static DiskMasterFormsRepository formsRepository;  
C#
static void Main(string[] args) 
{ 
   SetLicense(); 
   InitFormsEngines(); 
} 
 
static void SetLicense() 
{ 
   string license = @"C:\LEADTOOLS 20\Support\Common\License\LEADTOOLS.LIC"; 
   string key = File.ReadAllText(@"C:\LEADTOOLS 20\Support\Common\License\LEADTOOLS.LIC.KEY"); 
   RasterSupport.SetLicense(license, key); 
   if (RasterSupport.KernelExpired) 
         Console.WriteLine("License file invalid or expired."); 
   else 
         Console.WriteLine("License file set successfully"); 
} 
C#
static void InitFormsEngines() 
{ 
   try 
   { 
         codecs = new RasterCodecs(); 
 
         ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false); 
         ocrEngine.Startup(codecs, null, null, @"C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime"); 
 
         formsRepository = new DiskMasterFormsRepository(codecs, @"C:\Users\Public\Documents\LEADTOOLS Images\Forms\MasterForm Sets\OCR"); 
         autoEngine = new AutoFormsEngine(formsRepository, ocrEngine, null, AutoFormsRecognitionManager.Default | AutoFormsRecognitionManager.Ocr, 30, 80, true); 
 
         Console.WriteLine("Engines initialized successfully!"); 
   } 
   catch (Exception ex) 
   { 
         Console.WriteLine(ex.Message); 
   } 
} 

Add the Forms Recognition and Processing Code

In the Program class add two new methods called RecognizeAndProcessForm() and ShowProcessedResults(AutoFormsRunResult runResult);. Call the RecognizeAndProcessForm() method in the Main method after the InitFormsEngines() method. Call the ShowProcessedResults() method, passing runResult as the overload, at the bottom of the RecognizeAndProcessForm() method, as shown below.

C#
static void RecognizeAndProcessForm() 
{ 
   try 
   { 
         string resultMessage = "Form not recognized"; 
         string formToRecognize = @"C:\Users\Public\Documents\LEADTOOLS Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif"; 
 
         AutoFormsRunResult runResult = autoEngine.Run(formToRecognize, null); 
         if (runResult != null) 
         { 
            FormRecognitionResult recognitionResult = runResult.RecognitionResult.Result; 
            resultMessage = $@"This form has been recognized as a {runResult.RecognitionResult.MasterForm.Name} with {recognitionResult.Confidence}% confidence."; 
         } 
 
         Console.WriteLine("Recognition Results:"); 
         Console.WriteLine(resultMessage); 
         Console.WriteLine("========================================================================="); 
         ShowProcessedResults(runResult); 
   } 
   catch (Exception ex) 
   { 
         Console.WriteLine(ex.Message); 
   } 
} 
C#
// Output Processed Results 
private static void ShowProcessedResults(AutoFormsRunResult runResult) 
{ 
   if (runResult == null) 
         return; 
 
   string resultsMessage = ""; 
   try 
   { 
         foreach (FormPage formPage in runResult.FormFields) 
            foreach (FormField field in formPage) 
               if (field != null) 
                     resultsMessage = $"{resultsMessage}{field.Name} = {(field.Result as TextFormFieldResult).Text}\n"; 
   } 
   catch (Exception ex) 
   { 
         Console.WriteLine(ex.Message); 
   } 
 
   if (string.IsNullOrEmpty(resultsMessage)) 
   { 
         Console.WriteLine("Field Processing Results"); 
         Console.WriteLine("No fields were processed"); 
         Console.WriteLine("Press any key to exit . . ."); 
         Console.ReadKey(true); 
   } 
   else 
   { 
         Console.WriteLine("Field Processing Results:"); 
         Console.WriteLine(resultsMessage); 
         Console.WriteLine("Press any key to exit . . ."); 
         Console.ReadKey(true); 
   } 
} 

Shipped and installed with the LEADTOOLS SDK are sample master form sets and sample filled forms for recognition and processing. This tutorial uses these samples. The sample files are installed at C:\Users\Public\Documents\LEADTOOLS Images\Forms

Shut Down the OCR engine

In the Main method, add the following code under the RecognizeAndProcessForm() method to properly shut down the OCR engine and dispose of the AutoFormsEngine. The Main(string[] args) section should now look like:

C#
static void Main(string[] args) 
{ 
   SetLicense(); 
   InitFormsEngines(); 
   RecognizeAndProcessForm(); 
 
   autoEngine.Dispose(); 
   if (ocrEngine != null && ocrEngine.IsStarted) 
         ocrEngine.Shutdown(); 
} 

Run the Project

Run the project by pressing F5 or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console displays the recognized form as well as the processed results from the filled form.

The console displays the recognized form and the processed results from the filled form

For this example, a W-9 form was used. It was correctly recognized, with a confidence of 100% (where 0 means no confidence and 100% means complete confidence).

Wrap-up

This tutorial showed how to create a master forms set using the DiskMasterFormsRepository class, and then recognized and processed a filled form using the AutoFormsEngine class.

See Also

Help Version 20.0.2020.4.23
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS SDK Forms Recognition Tutorials Help