Export Annotations to Output using the Document Converter - Java

This tutorial shows how to create a new PDF document, load annotations to it in an AnnContainer, and save the annotations to the output in a Java application using the LEADTOOLS. SDK.

Overview  
Summary This tutorial covers how to load annotations into a new PDF document and save them to the output in a Java application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Burn Annotations to a LEADDocument - Java tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you don't have that project, 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:

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 NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Setup Libraries and Input Paths

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

In the Project Explorer, open the _Main.java class and add the following import statements to the import block at the top.

Java
import java.io.*; 
import java.nio.file.*; 
import java.net.*; 
import leadtools.*; 
import leadtools.annotations.rendering.*; 
import leadtools.document.*; 
import leadtools.document.converter.*; 
import leadtools.document.writer.*; 

Add the following objects as global variables:

Java
private static DocumentConverter docConverter; 
private static LEADDocument virtualDocument; 

Call the required methods in the Main() methods to load the needed LEADTOOLS libraries.

Add the paths to the Source Document file to be loaded and the annotations XML file.

In addition, Add and call the methods below after SetLicence()

Java
public static void main(String[] args) throws IOException { 
	Platform.setLibPath("C:\\LEADTOOLS22\\Bin\\CDLL\\x64"); 
	Platform.loadLibrary(LTLibrary.CODECS); 
	Platform.loadLibrary(LTLibrary.DOCUMENT); 
	Platform.loadLibrary(LTLibrary.DOCUMENT_WRITER); 
	Platform.loadLibrary(LTLibrary.LEADTOOLS); 
 
	String pdfFile = "C:\\LEADTOOLS22\\Resources\\Images\\leadtools.pdf"; 
	String annFile = "C:\\LEADTOOLS22\\Resources\\Images\\leadtools.xml"; 
	SetLicense(); 
	InitConverter(); 
	LoadAnnotations(pdfFile, annFile); 
	if (virtualDocument != null) { 
		BurnAnnotations(virtualDocument); 
		EmbedAnnotations(virtualDocument); 
		ExternalSaveAnnotations(virtualDocument); 
	} 
} 

Intialize the Document Converter

Add the code below for the InitConverter() method that will initialize the DocumentConverter class

Java
static void InitConverter() { 
	try { 
		docConverter = new DocumentConverter(); 
		docConverter.setDocumentWriterInstance(new DocumentWriter()); 
		docConverter.setAnnRenderingEngineInstance(new AnnJavaRenderingEngine()); 
		 
		System.out.println(String.format("Document Converter Initialized.")); 
 
	} catch (Exception e) { 
		System.out.println(e.getMessage()); 
	} 
} 

Create a New PDF, Add Pages from the Source, and Load Annotations

Add the following code for the LoadAnnotations(String pdfFile, String annFile) method that will create a new PDF and loads the annotations to it

Java
static void LoadAnnotations(String documentFile, String annFile) { 
	try { 
		virtualDocument = DocumentFactory.create(new CreateDocumentOptions()); 
		URI annUri = new File(annFile).toURI(); 
		LoadDocumentOptions loadOptions = new LoadDocumentOptions(); 
		loadOptions.setAnnotationsUri(annUri); 
		LEADDocument childDocument = DocumentFactory.loadFromFile(documentFile, loadOptions); 
 
		virtualDocument.getPages().add(childDocument.getPages().get(0)); 
	} catch (Exception e) { 
		System.out.println(e.getMessage()); 
	} 
} 

Save Annotations

Use the code below to save the annotations into the output using three approaches.

The BurnAnnotations(LEADDocument virtualDocument) method will overlay (burn) the annotations into a rasterized image output

Java
static void BurnAnnotations(LEADDocument virtualDocument) { 
	try { 
		var jobData = new DocumentConverterJobData(); 
		jobData.setAnnotationsMode(DocumentConverterAnnotationsMode.OVERLAY); 
		jobData.setDocument(virtualDocument); 
		jobData.setOutputDocumentFileName("C:\\LEADTOOLS22\\Resources\\Images\\BurnAnnotationsDoc.pdf"); 
		jobData.setRasterImageFormat(RasterImageFormat.RAS_PDF); 
		jobData.setDocumentFormat(DocumentFormat.USER); 
 
		var job = docConverter.getJobs().createJob(jobData); 
		docConverter.getJobs().runJob(job); 
		for (var error : job.getErrors()) 
			System.out.println(String.format("There was an error: %s", error.getError())); 
 
		if (job.getErrors().isEmpty()) 
			System.out.println( 
					String.format("- Annotations Overlaid(Burned) to: %s", jobData.getOutputDocumentFileName())); 
	} catch (Exception e) { 
		System.out.println(e.getMessage()); 
	} 
} 

The EmbedAnnotations(LEADDocument virtualDocument) method will embed the annotations into a PDF output.

Java
static void EmbedAnnotations(LEADDocument virtualDocument) { 
	try { 
		var jobData = new DocumentConverterJobData(); 
		jobData.setAnnotationsMode(DocumentConverterAnnotationsMode.EMBED); 
		jobData.setDocument(virtualDocument); 
		jobData.setOutputDocumentFileName("C:\\LEADTOOLS22\\Resources\\Images\\EmbedAnnotationsDoc.pdf"); 
		jobData.setDocumentFormat(DocumentFormat.PDF); 
 
		var job = docConverter.getJobs().createJob(jobData); 
		docConverter.getJobs().runJob(job); 
		for (var error : job.getErrors()) 
			System.out.println(String.format("There was an error: %s", error.getError())); 
 
		if (job.getErrors().isEmpty()) 
			System.out.println(String.format("- Annotations Embedded in: %s", jobData.getOutputDocumentFileName())); 
	} catch (Exception e) { 
		System.out.println(e.getMessage()); 
	} 
} 

The ExternalSaveAnnotations(LEADDocument virtualDocument) method will save the annotations into an external XML file.

Java
static void ExternalSaveAnnotations(LEADDocument virtualDocument) { 
	try { 
		var jobData = new DocumentConverterJobData(); 
		jobData.setAnnotationsMode(DocumentConverterAnnotationsMode.EXTERNAL); 
		jobData.setDocument(virtualDocument); 
		jobData.setOutputDocumentFileName("C:\\LEADTOOLS22\\Resources\\Images\\ExternalAnnotationsDoc.pdf"); 
		jobData.setOutputAnnotationsFileName("C:\\LEADTOOLS22\\Resources\\Images\\ExternalAnnotationsDoc.xml"); 
		jobData.setDocumentFormat(DocumentFormat.PDF); 
 
		var job = docConverter.getJobs().createJob(jobData); 
		docConverter.getJobs().runJob(job); 
		for (var error : job.getErrors()) 
			System.out.println(String.format("There was an error: %s", error.getError())); 
 
		if (job.getErrors().isEmpty()) 
			System.out.println(String.format("- Output document saved in: %s \n  Annotations saved in: %s", 
					jobData.getOutputDocumentFileName(), jobData.getOutputAnnotationsFileName())); 
	} catch (Exception e) { 
		System.out.println(e.getMessage()); 
	} 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and creates virtual document, adds a PDF document to the virtual document, loads the annotations from a specified file, and saves the annotations using three approaches.

The annotation XML file used by this tutorial can be found here. You can right-click and use Save link as.. to save a copy.

Wrap-up

This tutorial showed how to create a new virtual document, load annotations, and export them to the output. Also it covered how to use the LEADDocument, LoadDocumentOptions, and DocumentConverter classes.

See Also

Help Version 22.0.2024.2.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.