This tutorial shows how to create a set of Master Forms, then automatically recognize and process a form in a C# .NET 6 Console 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 6 Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (2 KB) |
Platform | C# .NET 6 Console Application |
IDE | Visual Studio 2022 |
Runtime Target | .NET 6 or Higher |
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 this 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:
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 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)
{
InitLEAD();
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:\LEADTOOLS23\Bin\Common\OcrLEADRuntime");
formsRepository = new DiskMasterFormsRepository(codecs, @"C:\LEADTOOLS23\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 below.
static void Main(string[] args)
{
InitLEAD();
InitFormsEngines();
RecognizeAndProcessForm();
}
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. Call the ShowProcessedResults
method, passing runResult
as the parameter, to display the processed results.
static void RecognizeAndProcessForm()
{
try
{
string resultMessage = "Form not recognized";
string formToRecognize = @"C:\LEADTOOLS23\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);
}
}
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>\LEADTOOLS23\Resources\Images\Forms
.
Add the code below to the ShowProcessedResults
method to display the processed results from the RecognizeAndProcessForm
method to the console.
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);
}
}
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
method should now look like:
static void Main(string[] args)
{
InitLEAD();
InitFormsEngines();
RecognizeAndProcessForm();
autoEngine.Dispose();
if (ocrEngine != null && ocrEngine.IsStarted)
ocrEngine.Shutdown();
}
If you would like to load the form using memory stream, then add the following code to the RecognizeAndProcessForm
method under string formToRecognize = @"C:\LEADTOOLS23\Resources\Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif";
.
using (var stream = File.OpenRead(formToRecognize))
{
var options = new LoadDocumentOptions();
using (var document = DocumentFactory.LoadFromStream(stream, options))
{
// Console commands to double check that the document was loaded properly
Console.WriteLine(document.DocumentId);
Console.WriteLine("Document loaded");
}
}
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:
Processed filled-in data (e.g., Business Name, Address, etc.)
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. Also it covered how to recognize and process a filled form using the AutoFormsEngine
class.