Pdftools SDK for Java
The Pdftools SDK for Java lets you create PDF files using Java.
The Pdftools SDK for Java requires Java version 8 or higher.
For the full API reference for the Pdftools SDK, see the Java API reference.
Compilation
When using the Java interface, the Java archive jar\com.pdftools.jar
needs to be on the CLASSPATH
. This can be done by either adding
it to the environment variable CLASSPATH
, or by specifying it using the -classpath
or -cp
switch:
- Windows
- Linux
- macOS
javac -cp ".;C:\Program Files\PDF Tools AG\jar\com.pdftools.jar" ^
sampleApplication.java
javac -cp ".:/path/to/com.pdftools.jar" ^
sampleApplication.java
javac -cp ".:/path/to/com.pdftools.jar" ^
sampleApplication.java
Execution
Additionally, the library needs to be in one of the system’s library directories or added to the Java system property java.library.path
.
- Windows
- Linux
- macOS
On Windows, the library is defined by the environment variable PATH
.
On Linux, the library is defined by LD_LIBRARY_PATH
.
On macOS, the library is defined by LD_LIBRARY_PATH
.
This can be achieved by either adding this system property dynamically at program startup before using the API, or by specifying it using the switch -Djava.library.path
when starting the Java VM.
Choose the correct subdirectory for the native library depending on the operating system and the platform of the Java VM. If the wrong data model is used, there is an error message similar to this: “Can't load IA 32-bit .dll on a AMD 64-bit platform”.
- Windows
- Linux
- macOS
On Windows, two builds are available: win-x64
and win-x86
.
java -cp ".;C:\Program Files\PDF Tools AG\com.pdftools.jar" ^
"-Djava.library.path=C:\Program Files\PDF Tools AG\lib\win-x64" sampleApplication
On Linux, one build is available: linux-x64
java -cp ".:/path/to/com.pdftools.jar" ^
"-Djava.library.path=/opt/pdftools.com/lib/linux-x64" sampleApplication
On macOS, two builds are available: osx-arm64
and osx-x64
java -cp ".:/path/to/com.pdftools.jar" ^
"-Djava.library.path=/opt/pdftools.com/lib/osx-arm64" sampleApplication
Note the different path separators on Windows vs. Linux or macOS
AutoCloseable objects
Objects that must be closed explicitly (e.g. com.pdftools.pdf.Document
, com.pdftools.image.Document
, com.pdftools.crypto.providers.Provider
, or sub-classes) implement the java.lang.AutoCloseable
interface. Instead of calling close()
directly, it is recommended that you use the “try with resources” statement:
try (Document document = ...) {
...
} // document.close() is called implicitly here
Properties
Properties are modeled with setter and getter methods.
Error handling
Errors are reported using exceptions.
Where applicable, the SDK maps errors to corresponding native runtime exceptions, such as java.lang.IllegalArgumentException
, java.lang.IllegalStateException
, java.lang.UnsupportedOperationException
, or java.io.IOException
.
The remaining errors are modeled using exception classes that inherit from a base class PdfToolsException
.
Streams
The native stream interfaces cannot be used, because they are lacking two important features:
- The PDF file format is based on random access. Native Java streams have only limited support for this.
- The ability to read from an output stream is crucial for processing large files.
Instead, a custom stream interface com.pdftools.sys.Stream
is provided.
A FileStream
implementation for files is provided, backed by java.io.RandomAccessFile
.
For in-memory processing, a MemoryStream
implementation is provided.
Lists & Iterables
The API uses different concepts for returning collections of elements depending on the context:
- lists (
java.util.List
) are usually used when the elements are already in memory and random access is possible. Depending on the context manipulation (add, remove, etc.) might also be possible. - iterables (
java.util.Iterable
) are usually used, when elements are retrieved on demand. They do not allow random access, but instead only allow iterating through the elements.
Maps
Maps implement the native Java map interface java.util.Map
.
Package
The Pdftools SDK uses the package com.pdftools
.
We recommend to use a custom, company specific package for your code.
Using the com.pdftools
package like the SDK can lead to problems with ambiguous class names, especially when using multiple of our SDKs in the same project.