This tutorial shows how to create a set of Master Forms, then automatically recognize and process a form in a C# .NET Core application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to recognize and process a form using the High-Level AutoFormsEngine class in a C# .NET Core Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET Core Console Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
Try it in another language |
|
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 - C# .NET Core tutorial.
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 via NuGet packages.
This tutorial requires the following NuGet package:
Leadtools.Document.Sdk
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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.
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
.
using System;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms.Auto;
using Leadtools.Forms.Processing;
using Leadtools.Forms.Recognition;
using Leadtools.Ocr;
Add the below global variables to the Program
class.
static AutoFormsEngine autoEngine;
static RasterCodecs codecs;
static IOcrEngine ocrEngine;
static DiskMasterFormsRepository formsRepository;
Add a new method to the Program
class named InitFormsEngines()
. Call the InitFormsEngines()
method inside the Main()
method below the set license call, as shown below.
static void Main(string[] args)
{
if (!SetLicense())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
InitFormsEngines();
}
Add the code below to the InitFormsEngines()
method to initialize the AutoFormsEngine
, RasterCodecs
, IOcrEngine
, and DiskMasterFormsRepository
objects.
static void InitFormsEngines()
{
codecs = new RasterCodecs();
ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);
ocrEngine.Startup(codecs, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime");
formsRepository = new DiskMasterFormsRepository(codecs, @"C:\LEADTOOLS21\Resources\Images\Forms\MasterForm Sets\OCR");
autoEngine = new AutoFormsEngine(formsRepository, ocrEngine, null, AutoFormsRecognitionManager.Default | AutoFormsRecognitionManager.Ocr, 30, 80, true);
Console.WriteLine("Engines initialized successfully!");
}
In the Program
class add two new methods named RecognizeAndProcessForm()
and ShowProcessedResults(AutoFormsRunResult runResult)
. Call the RecognizeAndProcessForm()
method inside the Main
method after the InitFormsEngines()
method, as shown in the next section. Call the ShowProcessedResults()
method, passing runResult
as the overload, at the bottom of the RecognizeAndProcessForm()
method, as shown below.
Add the code below to the RecognizeAndProcessForm()
method to recognize the given form based on the Master Forms and process the form to extract the desired information.
static void RecognizeAndProcessForm()
{
try
{
string resultMessage = "Form not recognized";
string formToRecognize = @"C:\LEADTOOLS21\Resources\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);
}
}
Add the code below to the ShowProcessedResults()
method to display the processed results from the RecognizeAndProcessForm()
method to the console.
// 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);
}
}
Note
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
<INSTALL_DIR>\LEADTOOLS21\Resources\Images\Forms
.
In the Main
method, add autoEngine.Dispose()
and ocrEngine.Shutdown()
code under the RecognizeAndProcessForm()
method to properly dispose of the AutoFormsEngine
and shut down the OCR engine. The Main(string[] args)
section should now look like:
static void Main(string[] args)
{
if (!SetLicense())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
InitFormsEngines();
RecognizeAndProcessForm();
autoEngine.Dispose();
if (ocrEngine != null && ocrEngine.IsStarted)
ocrEngine.Shutdown();
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the console appears and displays the following data results:
For this example, a W-9 form was used. It was correctly recognized, with a confidence of 98% (where 0 means no confidence and 100% means complete confidence).
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.