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 |
|
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.
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
leadtools.jar
leadtools.annotations.engine.jar
leadtools.codecs.jar
leadtools.caching.jar
leadtools.document.jar
leadtools.document.converter.jar
leadtools.document.pdf.jar
leadtools.document.raster.jar
leadtools.document.writer.jar
leadtools.drawing.jar
leadtools.imageprocessing.core.jar
leadtools.ocr.jar
leadtools.pdf.jar
leadtools.svg.jar
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.
With the project created, the references added, and the license set, coding can begin.
In thePackage Explorer, open the_Main.java
类。添加以下import
statements to the import block at the top.
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_Main
class calledConvertToRaster(String inputFile, DocumentConverter docConverter)
andConvertToDocument(String inputFile, DocumentConverter docConverter, OcrEngine ocrEngine)
. call these methods inside themain()
method after theSetLicense()
call.
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 engine
ConvertToDocument(inFile, docConverter, ocrEngine);
}
Add the below code to convert the JPEG file to TIFF.
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());
else
System.out.println("Successfully converted file to "+ outputFile);
}
Add the below code to convert the JPEG file to searchable PDF.
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());
else
System.out.println("Successfully converted file to "+ outputFile);
}
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.
This tutorial showed how to convert files with the Document Converter. Also it covered how to use theDocumentConverter
andDocumentConverterJob
classes, andDocumentConverterJobData
structure.