Export OCR Results to JSON - Java

This tutorial shows how to create a Java application that uses the LEADTOOLS SDK to save OCR results to JSON.

Summary This tutorial covers how to use LEADTOOLS OCR SDK technology in a Java application
Completion Time 30 minutes
Eclipse Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
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 - Java 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 that project is unavailable, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by local .jar files located at <INSTALL_DIR>\LEADTOOLS21\Bin\Java.

For this project, the following references are needed:

For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java 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 local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Initialize the OcrEngine and Set PDF Options

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

Open the _Main.java class in the Project Explorer. Add the following statements to the import block at the top.

// Import block at the top 
import com.google.gson.*; 
import java.io.File; 
import java.io.IOException; 
import java.nio.file.*; 
import java.nio.file.attribute.*; 
import java.util.*; 
import leadtools.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 

Inside the main() method, add the following to set the library path to where the C DLL files are located, as well as load the LEADTOOLS libraries that were previously imported. Also, add two new String values that will represent the input file and the directory to output the JSON and searchable PDF to. For the purposes of this tutorial, the PDF in the following file path is used: C:\LEADTOOLS21\Resources\Images\leadtools.pdf

public static void main(String[] args) throws IOException  
   String file = "C:\\LEADTOOLS21\\Resources\\Images\\leadtools.pdf"; 
   String outputDirectory = "C:\\LEADTOOLS21\\Resources\\Images"; 
   OcrEngine ocrEngine = InitOcrEngine(); 
   OCRandSaveResults(ocrEngine, file, outputDirectory); 

Add a new method to the _Main class called InitOcrEngine(). Call this method inside the main() method below the call to the SetLicense() method and the setting of String values, as shown above. Add the below code to return the initialized OcrEngine and set the PdfDocumentOptions.

static OcrEngine InitOcrEngine() 
   // Initialize OCR engine 
   OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
   ocrEngine.startup(null, null, null, "C:\\LEADTOOLS21\\Bin\\Common\\OcrLEADRuntime"); 
   // Set the PDF output options to overlay the original image over the recognized text 
   PdfDocumentOptions pdfOptions = (PdfDocumentOptions) ocrEngine.getDocumentWriterInstance().getOptions(DocumentFormat.PDF); 
   ocrEngine.getDocumentWriterInstance().setOptions(DocumentFormat.PDF, pdfOptions); 
   return ocrEngine; 

Add the Text Recognition and Export to JSON Code

Add another method at the bottom of the _Main class called OCRandSaveResults(OcrEngine ocrEngine, String file, String outputDirectory). Call the newly created method in the main() method after the OcrEngine ocrEngine = InitOcrEngine(); line of code, as shown above.

static void OCRandSaveResults(OcrEngine ocrEngine, String file, String outputDirectory) throws IOException  
   // Get the filename and create the output directory 
   File fileWithoutExt = new File(file.substring(0, file.lastIndexOf("."))); 
   String fileName = fileWithoutExt.getName(); 
   String outputDir = PathCombine(outputDirectory, fileName + "-results"); 
   if (Files.exists(Path.of(outputDir)) && Files.isDirectory(Path.of(outputDir))) 
      Files.walkFileTree(Path.of(outputDir), new SimpleFileVisitor<>() 
         public FileVisitResult visitFile(Path file, 
            BasicFileAttributes attrs) throws IOException { 
               System.out.println("Deleting file: " + file); 
               return CONTINUE; 
            public FileVisitResult postVisitDirectory(Path dir, 
               IOException exc) throws IOException { 
                  System.out.println("Deleting dir: " + dir); 
                  if (exc == null) { 
                     return CONTINUE; 
                  } else { 
                     throw exc; 
   // Create the OCR Document 
   OcrDocument ocrDocument = ocrEngine.getDocumentManager().createDocument(); 
   // Add the input file from first to last page 
   ILeadStream fileStream = new LeadFileStream(file); 
   ocrDocument.getPages().addPages(fileStream, 1, -1, null); 
   // Auto rotate each page 
   ocrDocument.getPages().autoPreprocess(OcrAutoPreprocessPageCommand.ROTATE, null); 
   // Recognize the entire document 
   // Loop through the pages, create the JSON of each page with the recognized words and save all pages to a file at the end 
   List<OCRResults> results = new ArrayList<>(); 
   for (var ocrPage : ocrDocument.getPages()) 
      OcrPageCharacters pageChars = ocrPage.getRecognizedCharacters(); 
      for (OcrZoneCharacters zoneChars : pageChars) 
         OCRResults oCRResults = new OCRResults(); 
         oCRResults.setPageNumber(ocrDocument.getPages().indexOf(ocrPage) + 1); 
         oCRResults.setZoneNumber(pageChars.indexOf(zoneChars) + 1); 
   // Save the recognized words to JSON 
   Gson gson = new Gson(); 
   var json = gson.toJson(results); 
   var jsonPath = PathCombine(outputDir, fileName + "-words.json"); 
   Files.write(Path.of(jsonPath), json.getBytes()); 
   // Save the recognized PDF to a file 
   var outputPath = PathCombine(outputDir, fileName + "-recognized.pdf"); 
   ocrDocument.save(outputPath, DocumentFormat.PDF, null); 

Next, add a new method to the _Main class called PathCombine(String... paths). This method will be called a total of 3 times inside the OCRandSaveResults() method, as shown above. Add the below code to combine the file paths and return the corresponding String.

public static String PathCombine(String... paths) 
   File file = new File(paths[0]); 
   for (int i = 1; i < paths.length ; i++) { 
      file = new File(file, paths[i]); 
   return file.getPath(); 

Add a Custom OCRResults Class

Create a new class named OCRResults and place it at the bottom of the _Main.java file, below the _Main class. Add the below code to store the OCR results.

// Custom class to store only the OCR Results 
class OCRResults 
   private int _pageNumber; 
   private int _zoneNumber; 
   private List<OcrWord> _words; 
   public OCRResults() { } 
   public int getPageNumber() { return _pageNumber; } 
   public void setPageNumber(int pageNumber) {_pageNumber = pageNumber;} 
   public int getZoneNumber() { return _zoneNumber; } 
   public void setZoneNumber(int zoneNumber) {_zoneNumber = zoneNumber;} 
   public List<OcrWord> getWords() { return _words; } 
   public void setWords(List<OcrWord> words) { _words = words; } 

Run the Project

Run the project by pressing Ctrl + F11, or by selecting Run -> Run.

If the steps were followed correctly, the application will OCR the specified PDF document and create a JSON file containing the recognition results in the specified output directory.

Download the resulting JSON here.


This tutorial showed how to run OCR on a document and export the results to JSON. It also covered how to use the OcrEngine, OcrDocument, and OcrPageCharacters classes.

See Also

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