This tutorial shows how to set up the LEAD OCR Engine to convert raster images to searchable PDFs in a Java application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to convert files to searchable PDFs in a Java application. |
Completion Time | 30 minutes |
Project | Download tutorial project (2 KB) |
Platform | Java Application |
IDE | Eclipse |
Runtime 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 Convert Images to Searchable PDF with OCR - Java 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. The following JAR files are needed for this tutorial:
The JAR files are located at <INSTALL_DIR>\LEADTOOLS23\Bin\Java
leadtools.jar
leadtools.codecs.jar
leadtools.document.writer.jar
leadtools.ocr.jar
leadtools.pdf.jar
For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java 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 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 Package Explorer, open the _Main.java
class. Add the following import
statements to the import block at the top.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import leadtools.*;
import leadtools.codecs.*;
import leadtools.document.writer.*;
import leadtools.ocr.*;
To initialize and run the OCR Engine, add a new OCR(String input, String output)
method and call it inside the run()
method.
public static void main(String[] args) throws IOException
{
new _Main().run(args);
}
private void run(String[] args) {
try {
Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64");
Platform.loadLibrary(LTLibrary.LEADTOOLS);
Platform.loadLibrary(LTLibrary.CODECS);
Platform.loadLibrary(LTLibrary.DOCUMENT_WRITER);
Platform.loadLibrary(LTLibrary.OCR);
SetLicense();
String input = "C:\\LEADTOOLS23\\Resources\\Images\\OCR1.TIF";
String output = "C:\\LEADTOOLS23\\Resources\\Images\\ocroutputPDF.pdf";
OCR(input, output);
}
catch(Exception ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
Add the code below to the OCR()
method to convert the given image to a searchable PDF file.
void OCR(String input, String output)
{
OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD);
ocrEngine.startup(new RasterCodecs(), new DocumentWriter(), null, null);
ocrEngine.getAutoRecognizeManager().run(input, output, DocumentFormat.PDF, null);
System.out.println("OCR output saved to " + output);
}
Run the project by selecting Run -> Run.
If the steps were followed correctly, the application converts the "OCR1.TIF" image to a PDF format and saves it to the specified location (as a searchable PDF).
This tutorial showed how to initialize the LEAD OCR Engine, process the specified input file and output the recognition results to the specified format.