Export OCR Results to JSON - Console C#

This tutorial shows how to save OCR results to a JSON file in a C# Windows Console application that uses the LEADTOOLS SDK.

Summary This tutorial covers how to save OCR results to a JSON file in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows 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 Export OCR Results to JSON - Console C# tutorial.

Create the Project and Add the 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 packages:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:

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 and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Initialize the OcrEngine

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

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

using System; 
using System.IO; 
using System.Collections.Generic; 
using Newtonsoft.Json; 
using Leadtools; 
using Leadtools.Ocr; 
using Leadtools.Document; 

Add a new method called InitOcrEngine() to return the IOcrEngine and call it inside the Main() method after the SetLicense() method call, as shown below.

static void Main(string[] args) 
   string file = @"C:\LEADTOOLS21\Resources\Images\leadtools.pdf"; 
   IOcrEngine ocrEngine = InitOcrEngine(); 
   OCRandSaveResults(ocrEngine, file); 

Add the below code to the InitOcrEngine() method to initialize the IOcrEngine.

static IOcrEngine InitOcrEngine() 
   // Initialize OCR engine 
   IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); 
   ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime"); 
   return ocrEngine; 

Add the Text Recognition and Export to JSON Code

Create a new method in the Program class named OCRandSaveResults(IOcrEngine ocrEngine, string file) and call it in the Main() method after the IOcrEngine ocrEngine = InitOcrEngine(); line of code. Add the code below to the OCRandSaveResults() method to OCR the PDF document and export the recognition results to a JSON file.

static void OCRandSaveResults(IOcrEngine ocrEngine, string file) 
   using (var document = DocumentFactory.LoadFromFile(file, new LoadDocumentOptions { FirstPageNumber = 1, LastPageNumber = -1 })) 
      document.Text.OcrEngine = ocrEngine; 
      List<DocumentPageText> documentPageTexts = new List<DocumentPageText>(); 
      foreach (var page in document.Pages) 
         //parse the text and build the DocumentPageText object 
         var pageText = page.GetText(); 
      //Save the recognized words to JSON 
      var json = JsonConvert.SerializeObject(documentPageTexts, Formatting.Indented); 
      var jsonPath = Path.ChangeExtension(file, ".json"); 
      File.WriteAllText(jsonPath, json); 

Run the Project

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

If the steps were followed correctly, the application will OCR a document and create a JSON containing the recognition results. For the purposes of this tutorial, we used the PDF file located here: <INSTALL_DIR>\LEADTOOLS21\Resources\Images\leadtools.pdf

You can download the resulting JSON here.


This tutorial showed how to run OCR on a document and export the results to JSON. Also, it covered how to use the IOcrEngine interface, along with the LEADDocument, DocumentPage and DocumentPageText classes.

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.