Take the following steps to start a project and to add some code that processes a filled form:
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:
Click Select and then click OK to add the above DLL to the application.
Add the following lines at the top of the Form1.cs file:
using Leadtools.Forms.Processing; Imports Leadtools.Forms.Processing Add the following members to the Form1 class:
//Create the processing engineFormProcessingEngine processingEngine = new FormProcessingEngine();
'Create the processing engineDim 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:
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 LeadRect(196,327, 1402, 40);formPage.Add(textField);textField = new TextFormField();textField.Name = "Address";textField.Bounds = new LeadRect(196, 496, 892, 35);formPage.Add(textField);//Add the page with the above defined fields to the engineprocessingEngine.Pages.Add(formPage);
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 LeadRect(196, 327,1402, 40)formPage.Add(textField) textField =New TextFormField()textField.Name = "Address"textField.Bounds =New LeadRect(196, 496, 892, 35)formPage.Add(textField)'Add the page with the above defined fields to the engineprocessingEngine.Pages.Add(formPage)
Add the following function to the class.
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");elseMessageBox.Show(resultsMessage, "Field ProcessingResults");}
Private Sub ProcessForm(ByVal image As RasterImage, ByValalignment As List(Of PageAlignment))processingEngine.OcrEngine = formsOCREngineDim resultsMessage As String = String.EmptyTryprocessingEngine.Process(image, alignment)For Each formPage As FormPage In processingEngine.PagesFor Each field As FormField In formPageIf Not field Is NothingThenresultsMessage = 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", "FieldProcessing Results")ElseMessageBox.Show(resultsMessage, "Field ProcessingResults")End IfEnd 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).
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;}
If recognitionResult.Confidence >= 80 ThenDim alignment As List(Of PageAlignment) = New List(OfPageAlignment)()Dim k As Integer = 0Do While k < recognitionResult.PageResults.Countalignment.Add(recognitionResult.PageResults(k).Alignment)k += 1LoopresultMessage = String.Format("This form has been recognized as a {0}",Path.GetFileNameWithoutExtension(masterFileName))ProcessForm(image, alignment)breakEnd If
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.
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
