Minimum Code Required to OCR Files - C# .NET Core

This tutorial shows how to create a C# .NET Core Console application that sets up the LEAD OCR Engine to process OCR using the minimum code required.

Overview  
Summary This tutorial covers how to set up the LEAD OCR Engine in a C# .NET Core Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

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 Minimum Code Required to OCR Files - C# .NET Core 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 the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

This tutorial requires the following NuGet package:

For a complete list of which DLL files are required for your application, refer to Files to be Included With 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:

Note

Adding LEADTOOLS NuGet references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the OCR Code

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

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

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

Add a new method named OCR(string inputFile, string outputFile) to the Program class and call it inside the Main() method below SetLicense().

C#
static void Main(string[] args) 
{ 
    SetLicense(); 
    string input = @"C:\LEADTOOLS21\Resources\Images\OCR1.TIF"; 
    string output = @"C:\LEADTOOLS21\Resources\Images\OCR1.PDF"; 
    OCR(input, output); 
} 

Add the code below to the new method, to initialize the IOcrEngine, run OCR on the input image, and export the file to searchable PDF.

C#
static void OCR(string inputFile, string outputFile) 
{ 
    using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
    { 
        // Startup the LEADTOOLS OCR Engine 
        ocrEngine.Startup(null, null, null, null); 
        // Run the AutoRecognizeManager and specify PDF format 
        ocrEngine.AutoRecognizeManager.Run(inputFile, outputFile, DocumentFormat.Pdf, null, null); 
        Console.WriteLine($"OCR output saved to {outputFile}"); 
    } 
} 

Run the Project

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

If the steps were followed correctly, the console appears and the application converts the input image to PDF format and saves it to the specified location (as a searchable PDF).

Wrap-up

This tutorial showed how to create a simple OCR .NET Core Application that initializes the LEAD OCR Engine, takes a specified input file and outputs the recognition results to the specified output file in the specified format, using the minimum code required.

See Also

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

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