Take the following steps to start a project and to add some code that processes a filled form:
- Start Visual Studio .NET.
- Open the project that you created in the tutorial, "Recognizing A Form From A Set Of Master Forms".
-
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
and browse to LEADTOOLS For .NET " \LEAD Technologies\LEADTOOLS
16.5\Bin\DotNet\Win32 " folder and select the following DLL:
- Leadtools.Forms.Processing.dll
-
Add the following lines at the top of the Form1.cs file:
[C#]
using Leadtools.Forms.Processing;
[Visual Basic]
Imports Leadtools.Forms.Processing
-
Add the following members to the Form1 class:
[C#]
//Create the processing engine FormProcessingEngine processingEngine = new FormProcessingEngine();
'Create the processing engine Dim processingEngine As FormProcessingEngine = New FormProcessingEngine()
-
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)
-
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
-
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 beenrecognized as a {0}",Path.GetFileNameWithoutExtension(masterFileName)); ProcessForm(image, alignment); break; }
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
- Run the project and test it using the following steps.
- Click the "Create Fields" button which will create two sample text fields for the form.
- 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.
- Click the "Recognize Form" button which will load a sample form, recognize the form, and then process the defined fields.