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 a filled form:
[C#]
using Leadtools; using Leadtools.Codecs; using Leadtools.Forms; using Leadtools.Forms.Ocr; using Leadtools.Forms.Recognition; using Leadtools.Forms.Recognition.Ocr; using System.IO;
[Visual Basic]
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Forms Imports Leadtools.Forms.Ocr Imports Leadtools.Forms.Recognition Imports Leadtools.Forms.Recognition.Ocr Imports System.IO
[C#]
//Create the recognition engine FormRecognitionEngine recognitionEngine = new FormRecognitionEngine(); RasterCodecs codecs; //Create the OCR Engine to use in the recognition IOcrEngine formsOCREngine;
[Visual Basic]
'Create the recognition engine Dim recognitionEngine As FormRecognitionEngine = New FormRecognitionEngine() Dim codecs As RasterCodecs 'Create the OCR Engine to use in the recognition Dim formsOCREngine As IOcrEngine
[C#]
try { RasterSupport.SetLicense(RasterSupportType.Forms, "Enter valid key here"); RasterSupport.SetLicense(RasterSupportType.OcrPlus, "Enter valid key here"); RasterSupport.SetLicense(RasterSupportType.Document, "Enter valid key here"); codecs = new RasterCodecs(); //Create a LEADTOOLS OCR Plus OCR Engine and start it formsOCREngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false); formsOCREngine.Startup(codecs, null, null, null); //Add an OCRObjectManager to the recognition engines //ObjectManager collection OcrObjectsManager ocrObjectsManager = new OcrObjectsManager(formsOCREngine); ocrObjectsManager.Engine = formsOCREngine; recognitionEngine.ObjectsManagers.Add(ocrObjectsManager); } catch (Exception ex) { MessageBox.Show(ex.Message); }
[Visual Basic]
Try RasterSupport.SetLicense(RasterSupportType.Forms, "Enter valid key here") RasterSupport.SetLicense(RasterSupportType.OcrPlus, "Enter valid key here") RasterSupport.SetLicense(RasterSupportType.Document, "Enter valid key here") codecs = New RasterCodecs() 'Create a LEADTOOLS OCR Plus OCR Engine and start it formsOCREngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False) formsOCREngine.Startup(codecs, Nothing, Nothing, Nothing) 'Add an OCRObjectManager to the recognition engines 'ObjectManager collection Dim ocrObjectsManager As OcrObjectsManager = New OcrObjectsManager(formsOCREngine) ocrObjectsManager.Engine = formsOCREngine recognitionEngine.ObjectsManagers.Add(ocrObjectsManager) Catch ex As Exception MessageBox.Show(ex.Message) End Try
[C#]
try { //Get master form filenames //You may need to update the below path to point to the "Leadtools Images\Forms\MasterForm Sets\OCR" directory. string [] masterFileNames = Directory.GetFiles(@"C:\Users\Public\Documents\LEADTOOLS Images\Forms\MasterForm Sets\OCR", "*.tif", SearchOption.AllDirectories); foreach (string masterFileName in masterFileNames) { string formName = Path.GetFileNameWithoutExtension(masterFileName); //Load the master form image RasterImage image = codecs.Load(masterFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1); //Create a new master form FormRecognitionAttributes masterFormAttributes = recognitionEngine.CreateMasterForm(formName, Guid.Empty, null); for (int i = 0; i < image.PageCount; i++) { image.Page = i + 1; //Add the master form page to the recognition engine recognitionEngine.AddMasterFormPage(masterFormAttributes, image, null); } //Close the master form and save it's attributes recognitionEngine.CloseMasterForm(masterFormAttributes); File.WriteAllBytes(formName + ".bin", masterFormAttributes.GetData()); } MessageBox.Show("Master Form Processing Complete", "Complete"); } catch(Exception ex) { MessageBox.Show(ex.Message); }
[Visual Basic]
Try 'Get master form filenames 'You may need to update the below path to point to the "Leadtools Images\Forms\MasterForm Sets\OCR" directory. Dim masterFileNames As String() = Directory.GetFiles("C:\Users\Public\Documents\LEADTOOLS Images\Forms\MasterForm Sets\OCR", "*.tif", SearchOption.AllDirectories) For Each masterFileName As String In masterFileNames Dim formName As String = Path.GetFileNameWithoutExtension(masterFileName) 'Load the master form image Dim image As RasterImage = codecs.Load(masterFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1) 'Create a new master form Dim masterFormAttributes As FormRecognitionAttributes = recognitionEngine.CreateMasterForm(formName, Guid.Empty, Nothing) Dim i As Integer = 0 Do While i < image.PageCount image.Page = i + 1 'Add the master form page to the recognition engine recognitionEngine.AddMasterFormPage(masterFormAttributes, image, Nothing) i += 1 Loop 'Close the master form and save it's attributes recognitionEngine.CloseMasterForm(masterFormAttributes) File.WriteAllBytes(formName & ".bin", masterFormAttributes.GetData()) Next masterFileName MessageBox.Show("Master Form Processing Complete", "Complete") Catch ex As Exception MessageBox.Show(ex.Message) End Try
[C#]
try { //For this tutorial, we will use the sample W9 sample filled form. //You may need to update the below path to point to "\LEADTOOLS Images\Forms\Images\W9_OCR_Filled.tif". string formToRecognize = @"C:\Users\Public\Documents\LEADTOOLS Images\Forms\Images\W9_OCR_Filled.tif"; RasterImage image = codecs.Load(formToRecognize, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1); //Load the image to recognize FormRecognitionAttributes filledFormAttributes = recognitionEngine.CreateForm(null); for (int i = 0; i < image.PageCount; i++) { image.Page = i + 1; //Add each page of the filled form to the recognition engine recognitionEngine.AddFormPage(filledFormAttributes, image, null); } recognitionEngine.CloseForm(filledFormAttributes); string resultMessage = "The form could not be recognized"; //Compare the attributes of each master form to the attributes of the filled form string[] masterFileNames = Directory.GetFiles(Application.StartupPath, "*.bin"); foreach (string masterFileName in masterFileNames) { FormRecognitionAttributes masterFormAttributes = new FormRecognitionAttributes(); masterFormAttributes.SetData(File.ReadAllBytes(masterFileName)); FormRecognitionResult recognitionResult = recognitionEngine.CompareForm(masterFormAttributes, filledFormAttributes, null); //In this example, we consider a confidence equal to or greater //than 90 to be a match if(recognitionResult.Confidence >= 80) { resultMessage = String.Format("This form has been recognized as a {0}", Path.GetFileNameWithoutExtension(masterFileName)); break; } } MessageBox.Show(resultMessage, "Recognition Results"); } catch (Exception ex) { MessageBox.Show(ex.Message); }
[Visual Basic]
Try 'For this tutorial, we will use the sample W9 sample filled form. 'You may need to update the below path to point to "\LEADTOOLS Images\Forms\Images\W9_OCR_Filled.tif". Dim formToRecognize As String = "C:\Users\Public\Documents\LEADTOOLS Images\Forms\Images\W9_OCR_Filled.tif" Dim image As RasterImage = codecs.Load(formToRecognize, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1) 'Load the image to recognize Dim filledFormAttributes As FormRecognitionAttributes = recognitionEngine.CreateForm(Nothing) Dim i As Integer = 0 Do While i < image.PageCount image.Page = i + 1 'Add each page of the filled form to the recognition engine recognitionEngine.AddFormPage(filledFormAttributes, image, Nothing) i += 1 Loop recognitionEngine.CloseForm(filledFormAttributes) Dim resultMessage As String = "The form could not be recognized" 'Compare the attributes of each master form to the attributes of the filled form Dim masterFileNames As String() = Directory.GetFiles(Application.StartupPath, "*.bin") For Each masterFileName As String In masterFileNames Dim masterFormAttributes As FormRecognitionAttributes = New FormRecognitionAttributes() masterFormAttributes.SetData(File.ReadAllBytes(masterFileName)) Dim recognitionResult As FormRecognitionResult = recognitionEngine.CompareForm(masterFormAttributes, filledFormAttributes, Nothing) 'In this example, we consider a confidence equal to or greater than 90 to be a match If recognitionResult.Confidence >= 80 Then resultMessage = String.Format("This form has been recognized as a {0}", Path.GetFileNameWithoutExtension(masterFileName)) Exit For End If Next masterFileName MessageBox.Show(resultMessage, "Recognition Results") Catch ex As Exception MessageBox.Show(ex.Message) End Try
[C#]
if (formsOCREngine != null && formsOCREngine.IsStarted) formsOCREngine.Shutdown();
[Visual Basic]
If Not formsOCREngine Is Nothing AndAlso formsOCREngine.IsStarted Then formsOCREngine.Shutdown() End If
NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.