Auto Recognize and Parse an Unstructured Document - Console C#

This tutorial shows how to automatically recognize a form and retrieve data from an unstructured document using LEADTOOLS SDK in a C# .NET Console application.

The form is recognized using the AutoFormsEngine paired with the LEADTOOLS Forms Recognition. The data is extracted using a ruleset defined in a JSON file paired with the LEADTOOLS Document Analyzer.

Summary This tutorial covers how to recognize and parse an unstructured form in a C# Windows Console application.
Completion Time 20 minutes
Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2019, 2022
Runtime 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 Parse an Unstructured Document - Console C# tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

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 packages:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, 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:

Initialize AutoFormsEngine, RasterCodecs, IOcrEngine, and DiskMasterFormsRepository

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

In order to run the application you will need a master forms set and filled forms to recognize and process. Each unstructured master form set will have the following files:

The files used for this tutorial can be downloaded in this link.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top:

using System; 
using System.IO; 
using System.Collections.Generic; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Forms.Recognition; 
using Leadtools.Forms.Auto; 
using Leadtools.Document; 
using Leadtools.Document.Analytics; 
using Leadtools.Document.Data; 
using Leadtools.Document.Unstructured; 
using Newtonsoft.Json; 

Add the following global members:

// Add these global members 
static AutoFormsEngine autoEngine;  
static RasterCodecs codecs;  
static IOcrEngine ocrEngine;  
static DiskMasterFormsRepository formsRepository; 
static string formToRecognize; 

In Program.cs, create a new method named InitFormsEngine() and add the code below to initialize the AutoFormsEngine, RasterCodes, IOcrEngine, and DiskMasterFormsRepository objects. This method will be called inside the Main() method below SetLicense();.

private static void InitFormsEngine() 
    codecs = new RasterCodecs(); 
    ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); 
    ocrEngine.Startup(codecs, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"); 
    formsRepository = new DiskMasterFormsRepository(codecs, @"FILE PATH TO DIRECTORY WITH MASTER FORMS"); 
    autoEngine = new AutoFormsEngine(formsRepository, ocrEngine, null, AutoFormsRecognitionManager.Default | AutoFormsRecognitionManager.Ocr, 30, 80, true); 
    Console.WriteLine("Engines initialized successfully!"); 

Add the Forms Recognition Code

Add a new method named RecognizeForm() to the Program class. This method will be called below InitFormsEngines(); in the Main() method. Add the code below to the new method to recognize a given form.

private static void RecognizeForm() 
    var json = JsonConvert.SerializeObject(codecs); 
    string resultMessage = "Form not recognized"; 
    formToRecognize = @"FILE PATH TO FILLED FORM"; 
    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:"); 

Add the Analyze and Parse Text Code

In the Program class, create a new method named AnalyzeAndParseDocument(). Call this method below the RecognizeForm() method in the Main() method.

private static void AnalyzeAndParseDocument() 
    string _dir = Path.GetDirectoryName(formToRecognize); 
    string _fileName = Path.GetFileNameWithoutExtension(formToRecognize); 
    string ruleset = Path.Combine(_dir, _fileName + ".json"); 
    LEADDocument document = DocumentFactory.LoadFromFile(formToRecognize, new LoadDocumentOptions()); 
    document.Text.OcrEngine = ocrEngine; 
    // Create Analyzer  
    DocumentAnalyzer analyzer = new DocumentAnalyzer() 
        Reader = new UnstructuredDataReader(), 
        QueryContext = new FileRepositoryContext(ruleset) 
    DocumentAnalyzerRunOptions options = new DocumentAnalyzerRunOptions { ElementQuery = new RepositoryQuery() }; 
    List<ElementSetResult> results = analyzer.Run(document, options); 
    string resultsMessage = string.Empty; 
    foreach (ElementSetResult result in results) 
        foreach (ElementResult item in result.Items) 
            Console.WriteLine($"{(item.GetFriendlyName())} = {(item.Value)}"); 

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 can be found at this file path: <INSTALL_DIR>\LEADTOOLS22\Resources\Images\Forms

Shut Down the OCR engine

Add the code below in the Main() method to properly dispose the resources used in this application.

static void Main(string[] args) 
        if (ocrEngine != null && ocrEngine.IsStarted) 
    catch (Exception ex) 
    Console.WriteLine("Press any key to exit..."); 

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 parsed results from the filled form.

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

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


This tutorial showed how to create a master forms set using the DiskMasterFormsRepository class, then how to recognize and parse a filled form using the AutoFormsEngine and DocumentAnalyzer classes.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.