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

In Eclipse, create a new Java project, and add the necessary LEADTOOLS references.

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>\LEADTOOLS21\Bin\Java

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.*; 

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 main() method after the SetLicense() call.

Java
public static void main(String[] args) throws IOException 
{ 
	Platform.setLibPath("C:\\LEADTOOLS21\\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:\\LEADTOOLS21\\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); 
} 

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

Java
static void ConvertToRaster(String inputFile, DocumentConverter docConverter) 
{ 
	String outputFile = "C:\\LEADTOOLS21\\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
static void ConvertToDocument(String inputFile, DocumentConverter docConverter, OcrEngine ocrEngine) 
{ 
	DocumentWriter docWriter = new DocumentWriter(); 
	ocrEngine.startup(new RasterCodecs(), docWriter, null, null); 
		 
	String outputFile = "C:\\LEADTOOLS21\\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 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.


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