Convert Files with the Document Converter - Java

This tutorial shows how to use the LEADTOOLS SDK to create a Java application that converts files using the Document Converter.

Overview  
Summary This tutorial covers how to use LEADTOOLS Document Converter SDK technology 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

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 Convert Files with the Document Converter - Java tutorial.

Create the Project and Add 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. The following JAR files are needed for this tutorial:

The JAR files are located at <INSTALL_DIR>\LEADTOOLS23\Bin\Java

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:

Note: Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Convert Files Code

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.

Java
import java.io.*; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.document.converter.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 

Under SetLicense() set up three variables. One for the path of the input file, one for the OcrEngine, and one for the DocumentConverter.

Java
String inFile = "C:\\LEADTOOLS23\\Resources\\Images\\cannon.jpg"; 
OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
DocumentConverter docConverter = new DocumentConverter(); 

Add two new methods to the _Main class called ConvertToRaster(String inputFile, DocumentConverter docConverter) and ConvertToDocument(String inputFile, DocumentConverter docConverter, OcrEngine ocrEngine). call these methods inside the run() method after the variable that were just created.

Java
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); 
		Platform.loadLibrary(LTLibrary.DOCUMENT_CONVERTER); 
		Platform.loadLibrary(LTLibrary.DOCUMENT_WRITER); 
		Platform.loadLibrary(LTLibrary.IMAGE_PROCESSING_CORE); 
		Platform.loadLibrary(LTLibrary.OCR); 
			 
		SetLicense(); 
			 
		String inFile = "C:\\LEADTOOLS23\\Resources\\Images\\cannon.jpg"; 
		OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
		DocumentConverter docConverter = new DocumentConverter(); 
 
		ConvertToRaster(inFile, docConverter); 
		// Converting Raster to Document requires valid OCR engine 
		ConvertToDocument(inFile, docConverter, ocrEngine); 
	}  
	catch(Exception ex) { 
		System.err.println(ex.getMessage()); 
		ex.printStackTrace(); 
	} 
} 

Add the below code to convert the JPEG file to TIFF.

Java
void ConvertToRaster(String inputFile, DocumentConverter docConverter) 
{ 
	String outputFile = "C:\\LEADTOOLS23\\Resources\\Images\\rasterConverter.tif"; 
	DocumentConverterJobData jobData = DocumentConverterJobs.createJobData(inputFile, outputFile, RasterImageFormat.TIF); 
	jobData.setJobName("RasterConversion"); 
		 
	DocumentConverterJob job = docConverter.getJobs().createJob(jobData); 
	docConverter.getJobs().runJob(job); 
		 
	if (job.getErrors().size() > 0) 
		for (DocumentConverterJobError error : job.getErrors()) 
			System.out.println("\nError during conversion: " + error.getError().getMessage()); 
	else 
		System.out.println("Successfully converted file to " + outputFile); 
} 

Add the below code to convert the JPEG file to searchable PDF.

Java
void ConvertToDocument(String inputFile, DocumentConverter docConverter, OcrEngine ocrEngine) 
{ 
	DocumentWriter docWriter = new DocumentWriter(); 
	ocrEngine.startup(new RasterCodecs(), docWriter, null, null); 
		 
	String outputFile = "C:\\LEADTOOLS23\\Resources\\Images\\documentConverter.pdf"; 
		 
	docConverter.setDocumentWriterInstance(docWriter); 
	docConverter.setOcrEngineInstance(ocrEngine, true); 
	DocumentConverterJobData jobData = DocumentConverterJobs.createJobData(inputFile, outputFile, DocumentFormat.PDF); 
	jobData.setJobName("DocumentConversion"); 
		 
	DocumentConverterJob job = docConverter.getJobs().createJob(jobData); 
	docConverter.getJobs().runJob(job); 
		 
	if (job.getErrors().size() > 0) 
		for (DocumentConverterJobError error : job.getErrors()) 
			System.out.println("\nError during conversion: " + error.getError().getMessage()); 
	else 
		System.out.println("Successfully converted file to " + outputFile); 
} 

Run the Project

Run the project by selecting Run -> Run.

If the steps were followed correctly, the application converts the specified input file to raster TIF and searchable PDF files.

This sample JPEG image can be used as an input test file.

Wrap-up

This tutorial showed how to convert files with the Document Converter. Also it covered how to use the DocumentConverter and DocumentConverterJob classes, and DocumentConverterJobData structure.

See Also

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

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.