Recognize Handwritten Text From Images With ICR - Console C#

This tutorial shows how to create a C# Windows Console application that runs ICR recognition to recognize handwritten text on a given image using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use LEADTOOLS Recognition SDK technology in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform Console C# Application
IDE Visual Studio 2019, 2022
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Recognize Handwritten Text From Images With ICR - Console C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

If using NuGet references, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add OCR Engine and Recognize Hand-written Text

With the project created, the references added, and the license set, coding can begin.

In Solution Explorer, open Program.cs. Add the following statements to the using block at the top:

C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Ocr; 

Add a new method called RunICR and call it in the Main method after the SetLicense call. Add the code below to run Intelligent Character Recognition on the loaded image and export the results to a searchable PDF.

C#
static void RunICR() 
{ 
    string _file = @"C:\LEADTOOLS22\Resources\Images\demoicr2.tif"; 
    using (IOcrEngine _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
    { 
        _ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"); 
        // Create an OCR document  
        using (IOcrDocument ocrDocument = _ocrEngine.DocumentManager.CreateDocument()) 
        { 
            IOcrPage _ocrPage = ocrDocument.Pages.AddPage(_file, null); 
            _ocrPage.AutoZone(null); 
 
            for (int i = 0; i < _ocrPage.Zones.Count; i++) 
            { 
                OcrZone tempZone = _ocrPage.Zones[i]; 
                tempZone.ZoneType = OcrZoneType.Icr; 
                _ocrPage.Zones[i] = tempZone; 
            } 
            _ocrPage.Recognize(null); 
 
            ocrDocument.Save(@"C:\LEADTOOLS22\Resources\Images\icr.pdf", Leadtools.Document.Writer.DocumentFormat.Pdf, null); 
        } 
    } 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application will auto-zone the loaded image, run ICR, then output the recognized image to a searchable PDF. This tutorial uses the sample image from this file path: <INSTALL_DIR>\LEADTOOLS22\Resources\Images\demoicr2.tif

Wrap-up

This tutorial showed how to run ICR on an image and output to a searchable PDF. Also it covered how to use the OcrZone structure.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.