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 theAdd References and Set a Licensetutorial, before working on theConvert Files with the Document Converter - Javatutorial.

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\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 toSetting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in theAdd References and Set a Licensetutorial.

Add the Convert Files Code

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

In thePackage Explorer, open the_Main.java类。添加以下importstatements to the import block at the top.

Java
importjava.io.*;importjava.nio.file.Files;importjava.nio.file.Paths;importleadtools.*;importleadtools.codecs.*;importleadtools.document.converter.*;importleadtools.document.writer.*;importleadtools.ocr.*;

Add two new methods to the_Mainclass calledConvertToRaster(String inputFile, DocumentConverter docConverter)andConvertToDocument(String inputFile, DocumentConverter docConverter, OcrEngine ocrEngine). call these methods inside themain()method after theSetLicense()call.

Java
publicstaticvoidmain(String[] args)throwsIOException{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();字符串inFile ="C:\\LEADTOOLS21\\Resources\\Images\\cannon.jpg";OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD);DocumentConverter docConverter =newDocumentConverter();ConvertToRaster(inFile, docConverter);// Converting Raster to Document requires valid OCR engineConvertToDocument(inFile, docConverter, ocrEngine);}

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

Java
staticvoidConvertToRaster(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());elseSystem.out.println("Successfully converted file to "+ outputFile);}

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

Java
staticvoidConvertToDocument(String inputFile, DocumentConverter docConverter, OcrEngine ocrEngine){DocumentWriter docWriter =newDocumentWriter();ocrEngine.startup(newRasterCodecs(), 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());elseSystem.out.println("Successfully converted file to "+ outputFile);}

Run the Project

Run the project by selectingRun->Run.

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

This sample JPEGimagecan 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 theDocumentConverterandDocumentConverterJobclasses, andDocumentConverterJobDatastructure.

See Also

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2021LEAD Technologies, Inc.All Rights Reserved.