Process A Form

Show in webframe

Take the following steps to start a project and to add some code that processes a filled form:

  1. Start Visual Studio .NET.
  2. Open the project that you created in the tutorial, "Recognizing A Form From A Set Of Master Forms".
  3. 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 DLL:
    • Leadtools.Forms.Processing.dll
    Click Select and then click OK to add the above DLL to the application.
  4. Add the following lines at the top of the Form1.cs file:

    [C#]

    
                using Leadtools.Forms.Processing;
                
    

    [Visual Basic]

    
                Imports Leadtools.Forms.Processing
                
    
  5. Add the following members to the Form1 class:

    [C#]

    
                //Create the processing engine
                FormProcessingEngine processingEngine = new FormProcessingEngine();
                
    
    [Visual Basic]
    
                'Create the processing engine
                Dim processingEngine As FormProcessingEngine = New FormProcessingEngine()
                
    
  6. In order to process a form, we must define fields on the master form. Each field represents an area on the form from which we wish to extract filled data. For this tutorial, we will create two text fields for the W9 form shipped with the SDK. The location of the fields has already been determined. Add a button to the form and set its Text property to "Create Fields" then double-click it and add the following code to its handler:

    [C#]

    
                processingEngine.Pages.Clear();
                //Create a new page for the form. In this case, we know the DPI
                //of the W9 form we ship is 150.
                FormPage formPage = new FormPage(1, 150, 150);
                TextFormField textField = new TextFormField();
                textField.Name ="Business Name";
                textField.Bounds = new LogicalRectangle(196,327, 1402, 40, LogicalUnit.Pixel);
                formPage.Add(textField);
                textField = new TextFormField();
                textField.Name = "Address";
                textField.Bounds = new LogicalRectangle(196, 496, 892, 35,LogicalUnit.Pixel);
                formPage.Add(textField); 
                //Add the page with the above defined fields to the engine
                processingEngine.Pages.Add(formPage);
                
    

    [Visual Basic]

    
                processingEngine.Pages.Clear()
                'Create a new page for the form. In this case, we know the DPI 'of the W9 form we ship is 150.
                Dim formPage As FormPage = New FormPage(1, 150, 150)
                DimtextField As TextFormField = New TextFormField()
                textField.Name ="Business Name"
                textField.Bounds = New LogicalRectangle(196, 327,1402, 40, LogicalUnit.Pixel)
                formPage.Add(textField) textField =New TextFormField()
                textField.Name = "Address"
                textField.Bounds =New LogicalRectangle(196, 496, 892, 35, LogicalUnit.Pixel)
                formPage.Add(textField)
                'Add the page with the above defined fields to the engine
                processingEngine.Pages.Add(formPage) 
                
    
  7. Add the following function to the class.

    [C#]

    
                private void ProcessForm(RasterImage image,List<PageAlignment> alignment)
                {
                   processingEngine.OcrEngine= formsOCREngine;
                   string resultsMessage = String.Empty;
                   try
                   {
                         processingEngine.Process(image, alignment);
                         foreach(FormPage formPage in processingEngine.Pages)
                         {
                            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", "FieldProcessing Results");
                   else
                      MessageBox.Show(resultsMessage, "Field ProcessingResults");
                }
                
    

    [Visual Basic]

    
                Private Sub ProcessForm(ByVal image As RasterImage, ByValalignment As List(Of PageAlignment))
                   processingEngine.OcrEngine = formsOCREngine 
                   Dim resultsMessage As String = String.Empty
                   Try 
                      processingEngine.Process(image, alignment)
                      For Each formPage As FormPage In processingEngine.Pages
                         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", "FieldProcessing Results")
                   Else
                      MessageBox.Show(resultsMessage, "Field ProcessingResults")
                   End If
                End Sub
                
    
  8. Modify the section of the "Recognize Form" event handler shown below (Find if(recognitionResult.Confidence >= 80){...} and replace its content with new code below).

    [C#]

    
                if(recognitionResult.Confidence >= 80)
                {
                   List<PageAlignment> alignment = new List<PageAlignment>();
                   for (int k = 0; k < recognitionResult.PageResults.Count;k++)
                   {
                      alignment.Add(recognitionResult.PageResults[k].Alignment);
                   }
                   resultMessage = String.Format("This form has been recognized as a {0}",Path.GetFileNameWithoutExtension(masterFileName));
                   ProcessForm(image, alignment);
                   break;
                }
                
    
    [Visual Basic]
    
                If recognitionResult.Confidence >= 80 Then
                   Dim alignment As List(Of PageAlignment) = New List(OfPageAlignment)()
                   Dim k As Integer = 0
                   Do While k < recognitionResult.PageResults.Count
                      alignment.Add(recognitionResult.PageResults(k).Alignment)
                      k += 1
                   Loop
                   resultMessage = String.Format("This form has been recognized as a {0}",Path.GetFileNameWithoutExtension(masterFileName))
                   ProcessForm(image, alignment)
                   break
                End If
                
    
  9. Run the project and test it using the following steps.

    NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.

    1. Click the "Create Fields" button which will create two sample text fields for the form.
    2. Click the "Create Master Form Attributes" button to create the master forms attributes. This step only needs to be run once so if you created the master form attribute in the previous tutorial and they still exist, this step can be skipped.
    3. Click the "Recognize Form" button which will load a sample form, recognize the form, and then process the defined fields.

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.