LEADTOOLS Support
Document
Document SDK Examples
HOW TO: Combine Documents in Java Using a Virtual Document
#1
Posted
:
Tuesday, June 5, 2018 3:32:52 PM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 199
Was thanked: 28 time(s) in 28 post(s)
Combining documents is very straight forward using the
LEADTOOLS Documents SDK. Below I've highlighted the key steps necessary for the combining process.
First, create the virtual document using the
DocumentFactory.Create(CreateDocumentOptions) method:
Code:
// Create the virtual document
CreateDocumentOptions createOptions = new CreateDocumentOptions();
createOptions.setUseCache(false);
LEADDocument virtualDocument = DocumentFactory.create(createOptions);
Next, for each input document:
- Load the document using the DocumentFactory.LoadFromFile(LoadDocumentOptions) method
Code:
String inputFile = "Some path";
LoadDocumentOptions loadOptions = new LoadDocumentOptions();
loadOptions.setUseCache(false);
LEADDocument tempDocument = DocumentFactory.loadFromFile(inputFile, loadOptions);
- Append all the pages to the virtual document. You can access the list of pages using the LEADDocument.Pages property
Code:
for (DocumentPage page : tempDocument.getPages())
virtualDocument.getPages().add(page);
Create a
DocumentConverter for exporting the virtual document
Code:
DocumentConverter documentConverter = new DocumentConverter();
If you need to perform OCR on the input documents:
- Create an OcrEngine using the OcrEngineManager.CreateEngine(OcrEngineType) method (there is only OcrEngineType.LEAD for Java)
Code:
OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD);
- Startup the OCR engine using the OcrEngine.Startup method (we have a guide for the various parameters here)
Code:
ocrEngine.startup(null, documentConverter.getDocumentWriterInstance(), null, null);
- Set the OCR engine for the document converter using the DocumentConverter.SetOcrEngineInstance(OcrEngine,bool) method (in my example project I set autoDispose to true, refer to the documentation page for more information)
Code:
documentConverter.setOcrEngineInstance(ocrEngine, true);
Now the the virtual document is created, you'll need to save it:
The RunJob method is synchronous, so once this call is complete you will be able to check the results:
- The status of the job can be found by accessing the DocumentConverterJob.Status property
Code:
if (job.getStatus() == DocumentConverterJobStatus.SUCCESS)
System.out.println("Finished successfully");
- If there were any errors during the conversion, you can access them via the DocumentConverterJob.Errors property
Code:
for (DocumentConverterJobError error : job.getErrors()) {
Exception ex = error.getError();
System.out.println(ex.getMessage());
ex.printStackTrace();
}
You can also download a working sample project here:
Anthony Northrup
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS Support
Document
Document SDK Examples
HOW TO: Combine Documents in Java Using a Virtual Document
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.