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 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:
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);
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.
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");
}
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).
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 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.
NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET