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:
In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab, browse to the "<LEADTOOLS_INSTALLDIR>\Bin\Dotnet\Win32" folder, and select the following DLLs:
Add the following lines at the top of the Form1.cs/Form1.vb file:
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;
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.FormsImports Leadtools.Forms.OcrImports Leadtools.FOrms.ProcessingImports Leadtools.Forms.RecognitionImports Leadtools.Forms.AutoImports System.IO
Add the following members to the Form1 class:
// Create the Auto Forms EngineAutoFormsEngine autoEngine;RasterCodecs formsCodec;// Create the OCR EngineIOcrEngine ocrEngine;// Create the repository of master formsDiskMasterFormsRepository formsRepository;
' Create the Auto Forms EngineDim autoEngine As AutoFormsEngineDim formsCodec As RasterCodecs' Create the OCR EngineDim ocrEngine As IOcrEngine' Create the repository of master formsDim formsRepository As DiskMasterFormsRepository
Add the following code to the Load event of the form (Form1_Load()):
NOTE: The developer key and the .LIC file are provided within the licensed toolkit.
try{string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic";// Unlock LEADTOOLS special Supportstring MY_DEVELOPER_KEY = "abc123xyz";RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_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.ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);ocrEngine.Startup(formsCodec, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime");//Point repository to directory with existing master formsformsRepository = new DiskMasterFormsRepository(formsCodec, @"C:\Users\Public\Documents\LEADTOOLS Images\Forms\MasterForm Sets\OCR");autoEngine = new AutoFormsEngine(formsRepository, ocrEngine, null, AutoFormsRecognitionManager.Default | AutoFormsRecognitionManager.Ocr, 30, 80, true);}catch (Exception ex){MessageBox.Show(ex.Message);}
TryDim MY_LICENSE_FILE As String = "d:\temp\TestLic.lic"' Unlock LEADTOOLS special SupportDim MY_DEVELOPER_KEY As String = "abc123xyz"RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_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.ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False)ocrEngine.Startup(formsCodec, Nothing, Nothing, "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime")' Point repository to directory with existing master formsformsRepository = New DiskMasterFormsRepository(formsCodec, "C:\Users\Public\Documents\LEADTOOLS Images\Forms\MasterForm Sets\OCR")autoEngine = New AutoFormsEngine(formsRepository, ocrEngine, Nothing, AutoFormsRecognitionManager.Default Or AutoFormsRecognitionManager.Ocr, 30, 80, True)Catch ex As ExceptionMessageBox.Show(ex.Message)End Try
LEADTOOLS ships several sample master and filled forms which we will use for this tutorial. The path to the images is hard-coded to the default install location so if you chose another path, you will need to change it accordingly. Add a button to the form and set its Text property to "Recognize and Process Form" then double-click it and add the following code to its handler:
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 processingAutoFormsRunResult 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);}
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 processingDim runResult As AutoFormsRunResult = autoEngine.Run(formToRecognize, Nothing)If Not runResult Is Nothing ThenDim recognitionResult As FormRecognitionResult = runResult.RecognitionResult.ResultresultMessage = String.Format("This form has been recognized as a {0} with {1} confidence.", runResult.RecognitionResult.MasterForm.Name, recognitionResult.Confidence)End IfMessageBox.Show(resultMessage, "Recognition Results")ShowProcessedResults(runResult)Catch ex As ExceptionMessageBox.Show(ex.Message)End Try
Add the following function to the class:
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");elseMessageBox.Show(resultsMessage, "Field ProcessingResults");}
Private Sub ShowProcessedResults(ByVal runResult As AutoFormsRunResult)If runResult Is Nothing ThenReturnEnd IfDim resultsMessage As String = String.EmptyTryFor Each formPage As FormPage In runResult.FormFieldsFor Each field As FormField In formPageIf Not field Is Nothing ThenresultsMessage = String.Format("{0}{1} = {2}{3}", resultsMessage, field.Name, (CType(IIf(TypeOf field.Result Is TextFormFieldResult, field.Result, Nothing), TextFormFieldResult)).Text, Environment.NewLine)End IfNext fieldNext formPageCatch ex As ExceptionMessageBox.Show(ex.Message)End TryIf String.IsNullOrEmpty(resultsMessage) ThenMessageBox.Show("No fields were processed", "Field ProcessingResults")ElseMessageBox.Show(resultsMessage, "Field ProcessingResults")End IfEnd Sub
Add the following clean-up code to the FormClosing event handler:
autoEngine.Dispose();if (ocrEngine != null && ocrEngine.IsStarted)ocrEngine.Shutdown();
autoEngine.Dispose()If Not ocrEngine Is Nothing AndAlso ocrEngine.IsStarted ThenocrEngine.Shutdown()End If
Set the output folder of the project .exe to where LEADTOOLS .Net DLLs are installed. For example: <LEADTOOLS_INSTALLDIR>\Bin\Dotnet4\Win32.