Add and Remove Pages from a LEADDocument - Java

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

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 Add and Remove Pages from a LEADDocument - Java tutorial.

Create the Project and Add LEADTOOLS References

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

For this project, the following references are needed:

For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application

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 Manipulate LEADDocument Pages Code

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.

Java
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 main() 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.

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(); 
} 

Inside the main() method add the following code below the call to the SetLicense() method to create a new virtual LEADDocument.

Java
// 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 the LEADDocument in the DocumentViewer, or use the DocumentConverter class.

Add three new methods to the _Main class called insertPagesFromFile, removeDocumentPage and saveDocument. Call these methods inside the main() method after the SetLicense() call as shown below.

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(); 
 
   // Create and load the main document 
   CreateDocumentOptions createOptions = new CreateDocumentOptions(); 
   createOptions.setUseCache(true); 
   LEADDocument virtualDocument = DocumentFactory.create(createOptions); 
   insertPagesFromFile(virtualDocument, "C:\\LEADTOOLS21\\Resources\\Images\\leadtools.pdf", 0); 
       
   // Insert an external document page into position 3 
   insertPagesFromFile(virtualDocument, "C:\\LEADTOOLS21\\Resources\\Images\\ocr1.emf", 3); 
 
   // Remove the first page from the combined document 
   removeDocumentPage(virtualDocument, 0); 
 
   // Save the combined document 
   saveDocument(virtualDocument, "g:\\test\\combined.pdf"); 
} 

Add the code below to the insertPagesFromFile() method to add pages to the virtual LEADDocument from document files.

Java
static 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.

Java
static 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.

Java
static 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

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.

Wrap-up

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.

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.