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 |
|
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.
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>\LEADTOOLS23\Bin\Java
.
For this project, the following references are needed:
leadtools.annotations.engine.jar
leadtools.annotations.rendering.java.jar
leadtools.caching.jar
leadtools.codecs.jar
leadtools.document.converter.jar
leadtools.document.jar
leadtools.document.pdf.jar
leadtools.document.writer.jar
leadtools.drawing.jar
leadtools.imageprocessing.core.jar
leadtools.jar
leadtools.pdf.annotations.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 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.
With the project created, the references added, and the license set, coding can begin. 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.
In the Project Explorer, open the _Main.java
class and add the following import statements to the import
block at the top.
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:
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()
public static void main(String[] args) throws IOException {
Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64");
Platform.loadLibrary(LTLibrary.CODECS);
Platform.loadLibrary(LTLibrary.DOCUMENT);
Platform.loadLibrary(LTLibrary.DOCUMENT_WRITER);
Platform.loadLibrary(LTLibrary.LEADTOOLS);
String pdfFile = "C:\\LEADTOOLS23\\Resources\\Images\\leadtools.pdf";
String annFile = "C:\\LEADTOOLS23\\Resources\\Images\\leadtools.xml";
SetLicense();
InitConverter();
LoadAnnotations(pdfFile, annFile);
if (virtualDocument != null) {
BurnAnnotations(virtualDocument);
EmbedAnnotations(virtualDocument);
ExternalSaveAnnotations(virtualDocument);
}
}
Add the code below for the InitConverter()
method that will initialize the DocumentConverter
class
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());
}
}
Add the following code for the LoadAnnotations(String pdfFile, String annFile)
method that will create a new PDF and loads the annotations to it
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());
}
}
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
static void BurnAnnotations(LEADDocument virtualDocument) {
try {
var jobData = new DocumentConverterJobData();
jobData.setAnnotationsMode(DocumentConverterAnnotationsMode.OVERLAY);
jobData.setDocument(virtualDocument);
jobData.setOutputDocumentFileName("C:\\LEADTOOLS23\\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.
static void EmbedAnnotations(LEADDocument virtualDocument) {
try {
var jobData = new DocumentConverterJobData();
jobData.setAnnotationsMode(DocumentConverterAnnotationsMode.EMBED);
jobData.setDocument(virtualDocument);
jobData.setOutputDocumentFileName("C:\\LEADTOOLS23\\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.
static void ExternalSaveAnnotations(LEADDocument virtualDocument) {
try {
var jobData = new DocumentConverterJobData();
jobData.setAnnotationsMode(DocumentConverterAnnotationsMode.EXTERNAL);
jobData.setDocument(virtualDocument);
jobData.setOutputDocumentFileName("C:\\LEADTOOLS23\\Resources\\Images\\ExternalAnnotationsDoc.pdf");
jobData.setOutputAnnotationsFileName("C:\\LEADTOOLS23\\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 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.
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.