This tutorial shows how to OCR an image and export the results to a JSON file in a C# .NET 6 application that uses the LEADTOOLS SDK.
| Overview | |
|---|---|
| Summary | This tutorial covers how to save OCR results to a JSON file in a C# .NET 6 Console application. |
| Completion Time | 20 minutes |
| Visual Studio Project | Download tutorial project (1 KB) |
| Platform | C# .NET 6 Console Application |
| IDE | Visual Studio 2022 |
| Runtime Target | .NET 6 or higher |
| Development License | Download LEADTOOLS |
| Try it in another language |
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.
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 packages:
Leadtools.Document.SdkNewtonsoft.JsonFor a complete list of which DLL files are required for your application, refer to Files to be Included With 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:
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 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 InitLEAD() method call, as shown below.
string file = @"C:\LEADTOOLS23\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 engineIOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime");return ocrEngine;}
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 objectvar pageText = page.GetText();pageText.BuildText();pageText.BuildWords();documentPageTexts.Add(pageText);}//Save the recognized words to JSONvar json = JsonConvert.SerializeObject(documentPageTexts, Formatting.Indented);var jsonPath = Path.ChangeExtension(file, ".json");File.WriteAllText(jsonPath, json);}}
Alternatively, you could use memory stream to handle the document. To do that, add the following code into the program and call this method in the Main(string[] args) method:
static void OCRStreamandSaveResults(IOcrEngine ocrEngine){ // load the specified documentvar fileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf");using (var stream = File.OpenRead(fileName)){var options = new LoadDocumentOptions();using (var document = DocumentFactory.LoadFromStream(stream, options)){document.Text.OcrEngine = ocrEngine;Console.WriteLine("Doc loaded");List<DocumentPageText> documentPageTexts = new List<DocumentPageText>();foreach (var page in document.Pages){//parse the text and build the DocumentPageText objectvar pageText = page.GetText();pageText.BuildText();pageText.BuildWords();documentPageTexts.Add(pageText);}//Save the recognized words to JSONvar json = JsonConvert.SerializeObject(documentPageTexts, Formatting.Indented);var jsonPath = Path.ChangeExtension(fileName, ".json");File.WriteAllText(jsonPath, json);}}}
Be sure to add the LEAD_VARS class so that ImagesDir can be accessed.
static class LEAD_VARS{public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";}
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>\LEADTOOLS23\Resources\Images\leadtools.pdf
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.