This tutorial shows how to OCR an image and export the OCR results to a JSON file in a C# .NET Core application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to save OCR results to a JSON file in a C# .NET Core Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (2 KB) |
Platform | C# .NET Core Console Application |
IDE | Visual Studio 2017, 2019 |
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 the Export OCR Results to JSON - C# .NET Core 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.Sdk
Newtonsoft.Json
For 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:
Note
Adding LEADTOOLS NuGet references and setting a license are covered in more detail in the Add References and Set a License tutorial.
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 SetLicense()
method call, as shown below.
static void Main(string[] args)
{
if (!SetLicense())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
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;
}
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();
pageText.BuildText();
pageText.BuildWords();
documentPageTexts.Add(pageText);
}
//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 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
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.