This tutorial shows how to create a C# Windows Console application that sets up the LEAD OCR Engine to process OCR.
Overview | |
---|---|
Summary | This tutorial covers how to set up the LEAD OCR Engine in a C# Windows Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (2 KB) |
Platform | C# Windows Console Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Convert Images to Searchable PDF With OCR - Console C# tutorial.
In Visual Studio, create a new C# Windows Console project, and add the following necessary LEADTOOLS references.
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:
Leadtools.Ocr
If using local DLL references, the following DLLs are needed:
The local DLLs are installed at <INSTALL_DIR>\LEADTOOLS 20\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.Tif.dll
Leadtools.Codecs.Fax.dll
Leadtools.Document.Writer.dll
Leadtools.Ocr.dll
Leadtools.Ocr.LEADEngine.dll
For a complete list of which Codec DLLs are required for specific formats, refer to Files to be Included in Your Application.
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 and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, and the license set, coding can begin.
In Solution Explorer, open Program.cs
. To initialize and run the OCR Engine, add a new OCR(string inputFile, string outputFile)
method and call it inside the Main
method.
Add the following statements to the using
block at the top of Program.cs
:
using Leadtools;
using Leadtools.Ocr;
using Leadtools.Document.Writer;
// Using block at the top
using System;
using Leadtools;
using Leadtools.Document.Writer;
using Leadtools.Ocr;
static void OCR(string inputFile, string outputFile)
{
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false))
{
//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}");
}
}
static void Main(string[] args)
{
SetLicense();
string input = @"C:\Users\Public\Documents\LEADTOOLS Images\OCR1.TIF";
string output = @"C:\Users\Public\Documents\LEADTOOLS Images\OCR1.PDF";
OCR(input, output);
}
Run the project by pressing F5 or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the console appears and confirms that the license was set correctly, and then the application converts the OCR1.TIF image to a PDF format and saves it to the specified location (as a searchable PDF).
This tutorial showed how to create a simple console-based OCR 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.