This tutorial shows how to add and remove pages from a LEADDocument
in a Java application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to create a virtual LEADDocument , add and remove pages from it, and export the document to disk in a Java application. |
Completion Time | 30 minutes |
Project | Download tutorial project (2 KB) |
Platform | Java Application |
IDE | Eclipse / IntelliJ |
Runtime License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Add and Remove Pages from a LEADDocument - Java tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If that project is unavailable, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added by local .jar
files located at <INSTALL_DIR>\LEADTOOLS22\Bin\Java
.
For this project, the following references are needed:
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
For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application
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.
With the project created, the references added, and the license set, coding can begin.
Open the _Main.java
class in the Package Explorer. Add the following statements to the import
block at the top.
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import leadtools.*;
import leadtools.codecs.*;
import leadtools.document.*;
import leadtools.document.converter.*;
import leadtools.document.writer.*;
import leadtools.ocr.*;
Inside the run()
method, add the following to set the library path to where the C DLL files are located, as well as load the LEADTOOLS libraries that were previously imported.
public static void main(String[] args) throws IOException
{
new _Main().run(args);
}
private void run(String[] args) {
try {
Platform.setLibPath("C:\\LEADTOOLS22\\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();
}
catch(Exception ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
Inside the run()
method add the following code below the call to the SetLicense()
method to create a new virtual LEADDocument
.
// Create and load the main document
CreateDocumentOptions createOptions = new CreateDocumentOptions();
createOptions.setUseCache(true);
LEADDocument virtualDocument = DocumentFactory.create(createOptions);
Note
A virtual document is a
LEADDocument
object that exists in memory that is used to construct documents consisting of pages from various document files. Once you are ready to make the virtual document a physical document file, or "finalize" it, you can either set theLEADDocument
in theDocumentViewer
, or use theDocumentConverter
class.
Add three new methods to the _Main
class called insertPagesFromFile
, removeDocumentPage
and saveDocument
. Call these methods inside the run()
method after the `` as shown below.
public static void main(String[] args) throws IOException
{
new _Main().run(args);
}
private void run(String[] args) {
try {
Platform.setLibPath("C:\\LEADTOOLS22\\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();
// create and load the main document
CreateDocumentOptions createOptions = new CreateDocumentOptions();
createOptions.setUseCache(true);
LEADDocument virtualDocument = DocumentFactory.create(createOptions);
insertPagesFromFile(virtualDocument, "C:\\LEADTOOLS22\\Resources\\Images\\leadtools.pdf", 0);
// insert an external document page into position 3
insertPagesFromFile(virtualDocument, "C:\\LEADTOOLS22\\Resources\\Images\\ocr1.emf", 3);
// remove the first page from the combined document
removeDocumentPage(virtualDocument, 0);
// save the combined document
saveDocument(virtualDocument, "C:\\Temp\\combined.pdf");
}
catch(Exception ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
}
}
Add the code below to the insertPagesFromFile()
method to add pages to the virtual LEADDocument
from document files.
void insertPagesFromFile(LEADDocument mainDocument, String filename, int insertPosition)
{
LoadDocumentOptions loadOptions = new LoadDocumentOptions();
loadOptions.setUseCache(true);
LEADDocument loadedDocument = DocumentFactory.loadFromFile(filename, loadOptions);
mainDocument.getPages().addAll(insertPosition, loadedDocument.getPages());
}
Add the following code to the removeDocumentPage()
method to remove a DocumentPage
from the LEADDocument
.
void removeDocumentPage(LEADDocument mainDocument, int pageNumber)
{
mainDocument.getPages().remove(pageNumber);
}
Add the code below to the saveDocument
to "finalize" the virtual LEADDocument
by using the DocumentConverter
class to export the document as a PDF to disk.
void saveDocument(LEADDocument document, String outputFile)
{
OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD);
DocumentConverter docConverter = new DocumentConverter();
DocumentWriter docWriter = new DocumentWriter();
ocrEngine.startup(new RasterCodecs(), docWriter, null, null);
docConverter.setDocumentWriterInstance(docWriter);
docConverter.setOcrEngineInstance(ocrEngine, true);
DocumentConverterJobData jobData = DocumentConverterJobs.createJobData(document, outputFile, DocumentFormat.PDF);
jobData.setJobName("DocumentConversion");
RasterDefaults.setExecutorService(java.util.concurrent.Executors.newFixedThreadPool(5));
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 document conversion: " + error.getError().getMessage());
else
System.out.println("Successfully saved document to " + outputFile);
}
Run the project by pressing Ctrl + F11, or by selecting Run -> Run.
If the steps are followed correctly, the application runs and combines pages from two files into one document, removes one page from the combined document, then saves the document as a PDF to disk.
This tutorial showed how to manipulate pages in a document using the LEADDocument
class, then save the resulting document as PDF. Also, it covered how to use the DocumentConverter
class.