Java technical notes
Learn about technical details of the Pdftools SDK for Java.
For the full API reference for the Pdftools SDK, see the Java API reference.
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.