Auto Recognize And Process A Form

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:

  1. Start Visual Studio .NET.
  2. Choose File->New->Project... from the menu.
  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application" in the Templates List.
  4. Type the project name as "Recognizing and Processing a Form" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.
  5. 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:

    • Leadtools.dll
    • Leadtools.Barcode.dll
    • Leadtools.Forms.Auto.dll
    • Leadtools.Forms.DocumentWriters.dll
    • Leadtools.Forms.Ocr.dll
    • Leadtools.Forms.Processing.dll
    • Leadtools.Forms.Recognition.dll
    • Leadtools.Codecs.dll
    • Leadtools.Codecs.Tif.dll
    • Leadtools.Codecs.Fax.dll
    • Leadtools.Forms.Ocr.Advantage.dll
  6. Add the following lines at the top of the Form1.cs/Form1.vb file:

    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; 
                      
         

    VB
    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 
                      
         

  7. Add the following members to the Form1 class:

    C#
    // Create the Auto Forms Engine 
    AutoFormsEngine autoEngine; 
                      
    RasterCodecs formsCodec; 
    // Create the OCR Engine 
    IOcrEngine ocrEngine; 
                      
    // Create the repository of master forms 
    DiskMasterFormsRepository formsRepository; 
                      
         

    VB
    ' Create the Auto Forms Engine 
    Dim autoEngine As AutoFormsEngine 
                     
    Dim formsCodec As RasterCodecs 
    ' Create the OCR Engine 
    Dim ocrEngine As IOcrEngine 
                      
    ' Create the repository of master forms 
    Dim formsRepository As DiskMasterFormsRepository             
                      
         

  8. 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.

    C#
    try 
    { 
       string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic"; 
       // Unlock LEADTOOLS special Support 
       string 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 forms 
       formsRepository = 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); 
    } 
                      
         

    VB
    Try 
       Dim MY_LICENSE_FILE As String = "d:\temp\TestLic.lic" 
       ' Unlock LEADTOOLS special Support 
       Dim 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 forms 
       formsRepository = 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 Exception 
       MessageBox.Show(ex.Message) 
    End Try 
                      
         

  9. 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:

    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); 
    }  
                      
         

    VB
    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 
                      
         

  10. Add the following function to the class:

    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"); 
    } 
                      
         

    VB
    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 
                      
         

  11. Add the following clean-up code to the FormClosing event handler:

    C#
    autoEngine.Dispose(); 
    if (ocrEngine != null && ocrEngine.IsStarted) 
       ocrEngine.Shutdown(); 
                      
         

    VB
    autoEngine.Dispose() 
    If Not ocrEngine Is Nothing AndAlso ocrEngine.IsStarted Then 
       ocrEngine.Shutdown() 
    End If 
                      
         

  12. Set the output folder of the project .exe to where LEADTOOLS .Net DLLs are installed. For example: <LEADTOOLS_INSTALLDIR>\Bin\Dotnet4\Win32.

  13. Run the project and test it. Click the "Recognize and Process Form" button which will load a sample form image, recognize and process it, and display the results. NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.
Help Version 19.0.2017.10.19
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document