Preprocess an Image for OCR - C# .NET Core

This tutorial shows how to preprocess an image for OCR in a C# .NET Core application using the LEADTOOLS SDK.

Summary This tutorial covers how to preprocess and image for OCR using the AutoPreprocess method 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
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 Preprocess an Image for OCR - 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 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 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:


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 Image Preprocessing 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.

using System; 
using Leadtools; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 

Add a new method to the Program class named OCRPreProcessing(). Call the OCRPreProcessing() method inside the Main() method below the set license code, as shown below.

static void Main(string[] args) 
    if (!SetLicense()) 
        Console.WriteLine("Error setting license"); 
        Console.WriteLine("License file set successfully"); 

Add the code below to the OCRPreProcessing() method to initialize the IOcrEngine, preprocess the loaded image, and run OCR exporting to a searchable PDF.

static void OCRPreProcessing() 
   string tifFileName = @"C:\LEADTOOLS21\Resources\Images\Clean.tif"; 
   string pdfFileName = @"C:\LEADTOOLS21\Resources\Images\Clean.pdf"; 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime"); 
      // Create an OCR document 
      using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) 
         // Add this image to the document 
         IOcrPage ocrPage = ocrDocument.Pages.AddPage(tifFileName, null); 
         // Auto-preprocess it 
         ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.Deskew, null); 
         ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.Invert, null); 
         ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.Rotate, null); 
         // Recognize it and save it as PDF 
         ocrDocument.Save(pdfFileName, 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 console appears and the application will "clean-up" the loaded image and export it as a searchable PDF.


This tutorial showed how to preprocess an image for OCR using the AutoPreprocess method inside the IOcrPage interface, and save it as a searchable PDF. Also, we covered how to use the IOcrDocument and IOcrEngine interfaces.

See Also

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