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

This tutorial shows how to set up the LEAD OCR Engine to convert a raster image to a searchable PDF in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to set up the LEAD OCR Engine in a C# .NET 6 Console application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 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 this 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>\LEADTOOLS23\Bin\net:

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 the OCR Code

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 of Program.cs:

C#
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 
using Leadtools.Document; 

To initialize and run the OCR Engine, add a new OCR(string inputFile, string outputFile) method and call it inside the Main method.

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, @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime"); 
   //Run the AutoRecognizeManager and specify PDF format 
   ocrEngine.AutoRecognizeManager.Run(inputFile, outputFile, DocumentFormat.Pdf, null, null); 
   Console.WriteLine($"OCR output saved to {outputFile}"); 
} 
C#
static void Main(string[] args) 
{ 
    InitLEAD(); 
    string input = @"C:\LEADTOOLS23\Resources\Images\OCR1.TIF"; 
    string output = @"C:\LEADTOOLS23\Resources\Images\OCR1.PDF"; 
    OCR(input, output); 
} 

Handling Streams

To use MemoryStream with the files, replace the existing code in the OCR method with the following:

C#
static void OCR(string inputFile, string outputFile) 
{ 
   using IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); 
   using var stream = File.OpenRead(inputFile); 
   var options = new LoadDocumentOptions(); 
   using var document = DocumentFactory.LoadFromStream(stream, options); 
   // Console commands to double check that the file was loaded properly 
   Console.WriteLine(document.DocumentId); 
   Console.WriteLine(document.DocumentId); 
   Console.WriteLine("Document loaded"); 
   //Startup the LEADTOOLS OCR Engine 
   ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime"); 
   //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 application converts the OCR1.TIF image to a searchable PDF and saves it to the specified location.

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 outputs the recognition results to the specified output file in the specified format.

See Also

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

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