Convert Images to Searchable PDF with OCR - C# .NET Core

This tutorial shows how to set up the LEAD OCR Engine to OCR a rasterized image in a C# .NET Core application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use the LEAD OCR Engine to convert a raster image to a searchable PDF 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 Convert Images to Searchable PDF with 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:

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 IOcrEngine and Convert to Searchable PDF 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 System; 
using System.IO; 
using Leadtools; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 

Inside the Main() method, below the set license code, create two new strings named input and output. Set the input string value to rasterized image file path. Set the output string value to the file path you wish to save the created searchable PDF to. For the purposes of this tutorial, the sample image in the following file path is used: C:\LEADTOOLS21\Resources\Images\OCR1.TIF

C#
static void Main(string[] args) 
{ 
    if (!SetLicense()) 
        Console.WriteLine("Error setting license"); 
    else 
        Console.WriteLine("License file set successfully"); 
 
    string input = @"C:\LEADTOOLS21\Resources\Images\OCR1.TIF"; 
    string output = @"C:\LEADTOOLS21\Resources\Images\OCR1.PDF"; 
 
    OCR(input, output); 
} 

Add a new method to the Program class named OCR(string inputFile, string outputFile). Call the OCR() method inside the Main() method below the string values, as shown above. Add the code below to the OCR() method to initialize the IOcrEngine, OCR the raster image, and export the image to a searchable PDF document.

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 OCR1.TIF 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 console-based OCR application that initializes the LEAD OCR Engine, takes a specified input file and converts it to a searchable PDF document file using the IOcrEngine and IOcrAutoRecognizeManager interfaces.

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.