Parse Data with the Document Analyzer - C# .NET Core

This tutorial shows how to retrieve data from an unstructured document in a C# .NET Core Console application using the LEADTOOLS SDK. The data is extracted using a ruleset defined in a JSON file paired with the LEADTOOLS Document Analyzer.

Overview  
Summary This tutorial covers how to retrieve data from an unstructured document utilizing the DocumentAnalyzer in a C# .NET Core Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core 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 Parse Data Using Document Analyzer - .NET Core Console C# tutorial.

Create the Project and Add LEADTOOLS References

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

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

This tutorial requires the following NuGet packages:

For a complete list of which DLL files are required for your application, refer to Files to be Included with 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 references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Code to Parse Unstructured Document Data

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

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

C#
// Using block at the top 
using System; 
using System.Collections.Generic; 
using Leadtools; 
using Leadtools.Document; 
using Leadtools.Document.Analytics; 
using Leadtools.Document.Data; 
using Leadtools.Document.Unstructured; 
using Leadtools.Ocr; 

All of the necessary code will be written within the Main() method. Add the following code to the Main method to initialize the IOcrEngine, load the unstructured document, and display the extracted data to the console using the DocumentAnalyzer class.

C#
static void Main(string[] args) 
{ 
    try 
    { 
        string ruleset = @"C:\LEADTOOLS21\Resources\Images\Forms\Unstructured\MedicareCard.json"; 
        string file = @"C:\LEADTOOLS21\Resources\Images\Forms\Unstructured\MedicareCard.png"; 
 
        if (!SetLicense()) 
            Console.WriteLine("Error setting license"); 
        else 
            Console.WriteLine("License file set successfully"); 
 
        using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
        { 
            ocrEngine.Startup(null, null, null, null); 
 
            LEADDocument document = DocumentFactory.LoadFromFile(file, new LoadDocumentOptions()); 
            document.Text.OcrEngine = ocrEngine; 
            OcrEngines engines = UnstructuredOcrEngines.Defaults(@"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime"); 
 
            DocumentAnalyzer analyzer = new DocumentAnalyzer 
            { 
                Reader = new UnstructuredDataReader { OcrEngines = engines.Engines }, 
                QueryContext = new FileRepositoryContext(ruleset) 
            }; 
 
            DocumentAnalyzerRunOptions options = new DocumentAnalyzerRunOptions { ElementQuery = new RepositoryQuery() }; 
 
            List<ElementSetResult> results = analyzer.Run(document, options); 
 
            foreach (ElementSetResult result in results) 
                foreach (ElementResult item in result.Items) 
                    Console.WriteLine(item.Value); 
        } 
    } 
    catch (Exception ex) 
    { 
        Console.WriteLine(ex.ToString()); 
    } 
    Console.WriteLine("Press any key to exit..."); 
    Console.ReadKey(true); 
} 

Run the Project

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

If the steps were followed correctly, the console appears and the application displays the extracted data from an unstructured document. For the purposes of this tutorial, the below sample files were used for testing.

Wrap-up

This tutorial showed how to extract and display information about the unstructured document utilizing the DocumentAnalyzer with a JSON ruleset.

See Also

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

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