Take the following steps to start a project and to add some code that creates a set of master forms and uses them to recognize and process a form using LEADTOOLS High Level Forms Interface:
[C#]
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms;
using Leadtools.Forms.Ocr;
using Leadtools.Forms.Processing;
using Leadtools.Forms.Recognition;
using Leadtools.Forms.Auto;
using System.IO;
[Visual Basic]
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Forms
Imports Leadtools.Forms.Ocr
Imports Leadtools.FOrms.Processing
Imports Leadtools.Forms.Recognition
Imports Leadtools.Forms.Auto
Imports System.IO
[C#]
//Create the Auto Forms Engine
AutoFormsEngine autoEngine;
RasterCodecs formsCodec;
//Create a collection of OCR Engines
List<IOcrEngine> ocrEngines;
//Create the repository of master forms
DiskMasterFormsRepository formsRepository;
[Visual Basic]
'Create the Auto Forms Engine
Dim autoEngine As AutoFormsEngine
Dim formsCodec As RasterCodecs
'Create a collection of OCR Engines
Dim ocrEngines As List(Of IOcrEngine)
'Create the repository of master forms
Dim formsRepository As DiskMasterFormsRepository
[C#]
try
{
string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic";
// Unlock the OCR Advantage support
string MY_OCRADVDEVELOPER_KEY = "xyz123abc";
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_OCRADVDEVELOPER_KEY);
// Unlock the Forms support
string MY_FORMSDEVELOPER_KEY = "abc123xyz";
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_FORMSDEVELOPER_KEY);
// Unlock the Document support
string MY_DOCDEVELOPER_KEY = "123xyzabc";
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DOCDEVELOPER_KEY);
formsCodec = new RasterCodecs();
//Create an OCR Engine for each processor on the machine. This
//allows for optimal use of thread during recognition and processing.
ocrEngines = new List<IOcrEngine>();
for (int i = 0; i < Environment.ProcessorCount; i++)
{
ocrEngines.Add(OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false));
ocrEngines[i].Startup(formsCodec, null, null, @"C:\LEADTOOLS 18\Bin\Common\OcrAdvantageRuntime");
}
//Point repository to directory with existing master forms
formsRepository = new DiskMasterFormsRepository(formsCodec, @"C:\Users\Public\Documents\LEADTOOLS Images\Forms\MasterForm Sets\OCR");
autoEngine = new AutoFormsEngine(formsRepository, ocrEngines, null, AutoFormsRecognitionManager.Default | AutoFormsRecognitionManager.Ocr, 30, 80, true);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
[Visual Basic]
Try
Dim MY_LICENSE_FILE As String = "d:\temp\TestLic.lic"
' Unlock the OCR Advantage support
Dim MY_OCRADVDEVELOPER_KEY As String = "xyz123abc"
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_OCRADVDEVELOPER_KEY)
' Unlock the Forms support
Dim MY_FORMSDEVELOPER_KEY As String = "abc123xyz
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_FORMSDEVELOPER_KEY)
' Unlock the Document support
Dim MY_DOCDEVELOPER_KEY As String = "123xyzabc"
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DOCDEVELOPER_KEY)
formsCodec = New RasterCodecs()
'Create an OCR Engine for each processor on the machine. This
'allows for optimal use of thread during recognition and processing.
ocrEngines = New List(Of IOcrEngine)()
Dim i As Integer = 0
Do While i < Environment.ProcessorCount
ocrEngines.Add(OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False))
ocrEngines(i).Startup(formsCodec, Nothing, Nothing, @"C:\LEADTOOLS 18\Bin\Common\OcrAdvantageRuntime");
i += 1
Loop
'Point repository to directory with existing master forms
formsRepository = New DiskMasterFormsRepository(formsCodec, "C:\Users\Public\Documents\LEADTOOLS Images\Forms\MasterForm Sets\OCR")
autoEngine = New AutoFormsEngine(formsRepository, ocrEngines, Nothing, AutoFormsRecognitionManager.Default Or AutoFormsRecognitionManager.Ocr, 30, 80, True)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
[C#]
try
{
//For this tutorial, we will use the sample W9 sample filled form.
string resultMessage = "Form not recognized";
string formToRecognize = @"C:\Users\Public\Documents\LEADTOOLS Images\Forms\Images\W9_OCR_Filled.tif";
//Run the recognition and processing
AutoFormsRunResult runResult = autoEngine.Run(formToRecognize, null);
if (runResult != null)
{
FormRecognitionResult recognitionResult = runResult.RecognitionResult.Result;
resultMessage = String.Format("This form has been recognized as a {0} with {1} confidence.", runResult.RecognitionResult.MasterForm.Name, recognitionResult.Confidence);
}
MessageBox.Show(resultMessage, "Recognition Results");
ShowProcessedResults(runResult);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
[Visual Basic]
Try
'For this tutorial, we will use the sample W9 sample filled form.
Dim resultMessage As String = "Form not recognized"
Dim formToRecognize As String = "C:\Users\Public\Documents\LEADTOOLS Images\Forms\Images\W9_OCR_Filled.tif"
'Run the recognition and processing
Dim runResult As AutoFormsRunResult = autoEngine.Run(formToRecognize, Nothing)
If Not runResult Is Nothing Then
Dim recognitionResult As FormRecognitionResult = runResult.RecognitionResult.Result
resultMessage = String.Format("This form has been recognized as a {0} with {1} confidence.", runResult.RecognitionResult.MasterForm.Name, recognitionResult.Confidence)
End If
MessageBox.Show(resultMessage, "Recognition Results")
ShowProcessedResults(runResult)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
[C#]
private void ShowProcessedResults(AutoFormsRunResult runResult)
{
if (runResult == null)
return;
string resultsMessage = String.Empty;
try
{
foreach (FormPage formPage in runResult.FormFields)
{
foreach (FormField field in formPage)
{
if (field != null)
{
resultsMessage = String.Format("{0}{1} = {2}{3}",
resultsMessage,
field.Name,
(field.Result as TextFormFieldResult).Text,
Environment.NewLine);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (String.IsNullOrEmpty(resultsMessage))
MessageBox.Show("No fields were processed", "Field ProcessingResults");
else
MessageBox.Show(resultsMessage, "Field ProcessingResults");
}
[Visual Basic]
Private Sub ShowProcessedResults(ByVal runResult As AutoFormsRunResult)
If runResult Is Nothing Then
Return
End If
Dim resultsMessage As String = String.Empty
Try
For Each formPage As FormPage In runResult.FormFields
For Each field As FormField In formPage
If Not field Is Nothing Then
resultsMessage = String.Format("{0}{1} = {2}{3}", resultsMessage, field.Name, (CType(IIf(TypeOf field.Result Is TextFormFieldResult, field.Result, Nothing), TextFormFieldResult)).Text, Environment.NewLine)
End If
Next field
Next formPage
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
If String.IsNullOrEmpty(resultsMessage) Then
MessageBox.Show("No fields were processed", "Field ProcessingResults")
Else
MessageBox.Show(resultsMessage, "Field ProcessingResults")
End If
End Sub
[C#]
autoEngine.Dispose();
foreach (IOcrEngine ocrEngine in ocrEngines)
{
if (ocrEngine != null && ocrEngine.IsStarted)
ocrEngine.Shutdown();
}
[Visual Basic]
autoEngine.Dispose()
For Each ocrEngine As IOcrEngine In ocrEngines
If Not ocrEngine Is Nothing AndAlso ocrEngine.IsStarted Then
ocrEngine.Shutdown()
End If
Next ocrEngine
NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.