Get started with Java
This guide walks you through the steps to use a code example, and then explains how to integrate the Pdftools SDK into your application with the Java programming language.
Try the Pdftools SDK without a license key.
Prerequisites
The Pdftools SDK for Java requires Java version 8 or higher.
Getting started with a sample project
Learn how to use the Pdftools SDK using a Java code example. The Pdf2ImgSimple example converts a PDF file to an image. Use similar steps to run any other code example from the sdk-examples-java repository.
Compile and run the sample
Switch between the following tabs to display steps for the command line interface or for the Eclipse IDE. To compile and run the sample, follow these steps:
- Command line
- Eclipse IDE
-
Clone the sdk-examples-java repository:
git clone https://github.com/pdf-tools/sdk-examples-java.git -
Navigate to the
Pdf2ImgSimplecode example:cd sdk-examples-java/Pdf2ImgSimple -
Compile the Java source file:
- macOS and Linux
- Windows
javac -cp jar/com.pdftools.jar:. PdfToolsPdf2ImgSimple.javajavac -cp jar/com.pdftools.jar;. PdfToolsPdf2ImgSimple.java -
Run the sample:
- macOS and Linux
- Windows
java -cp jar/com.pdftools.jar:bin:. PdfToolsPdf2ImgSimple PdfPrimerWhitepaper.pdf tiff_output.tiffjava -cp jar/com.pdftools.jar;bin;. PdfToolsPdf2ImgSimple PdfPrimerWhitepaper.pdf tiff_output.tiff
The code example takes:
- The input and output files are represented as a file name or a file path with the file name.
- Both file paths (input and output) can be relative or absolute.
Review the following snippet with a placeholder and compare it to the last step of the previous procedure:
- macOS and Linux
- Windows
java -cp jar/com.pdftools.jar:bin:. PdfToolsPdf2ImgSimple INPUT_FILE OUTPUT_FILE
java -cp jar/com.pdftools.jar;bin;. PdfToolsPdf2ImgSimple INPUT_FILE OUTPUT_FILE
-
Clone the sdk-examples-java repository:
git clone https://github.com/pdf-tools/sdk-examples-java.git -
From the cloned repository, import the
.projectfile into the Eclipse IDE. The source filePdfToolsPdf2ImgSimple.javais compiled automatically. -
Right-click the
javafile, and then select PdfToolsPdf2ImgSimple.java > Run As > Run configurations…. -
Create a new configuration for a Java Application, and then add the required arguments, for example:
PdfPrimerWhitepaper.pdf OutputPdfPrimerWhitePaper.tiff
You can apply a similar procedure described in this tutorial for other code examples. For more information, see Code samples page.
Integrate the SDK into your application
Integrate and initialize the Pdftools SDK into your application by following the instructions in the following sections.
Add the SDK to your project
- Maven
- Manual
The Pdftools SDK for Java is available on Maven. To add the Pdftools SDK for Java to your project, select your operating system and system architecture and add the following to your pom.xml:
- linux-x64
- linux-x64 (with older version of glibc)
- linux-arm64
- osx-arm64
- osx-x64
- win-x64
- win-x86
- win-arm64
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
<classifier>linux-x64</classifier>
<type>so</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
<classifier>linux-x64-glibc2.12</classifier>
<type>so</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
<classifier>linux-arm64</classifier>
<type>so</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
<classifier>osx-arm64</classifier>
<type>dylib</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
<classifier>osx-x64</classifier>
<type>dylib</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
<classifier>win-x64</classifier>
<type>dll</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
<classifier>win-x86</classifier>
<type>dll</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.14.0</version>
<classifier>win-arm64</classifier>
<type>dll</type>
</dependency>
- Download the Pdftools SDK for Java product kit, and then unzip the file.
- Add the
jarfile to your project’s classpath. - Copy the
libfolder containing the native binary files to your project, for example<project-dir>/lib.
Load the SDK
The Pdftools SDK for Java requires you to load the underlying native binary files suitable for your operating system and system architecture at runtime.
- Maven
- Manual
There are two ways how to load the Pdftools SDK for Java with Maven:
-
If you are consuming Java libraries directly from the local Maven repository, load the native binary files using the following code:
- linux-x64
- linux-x64 (with older version of glibc)
- linux-arm64
- osx-arm64
- osx-x64
- win-x64
- win-x86
- win-arm64
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.14.0/pdftools-sdk-1.14.0-linux-x64.so");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.14.0/pdftools-sdk-1.14.0-linux-x64-glibc2.12.so");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.14.0/pdftools-sdk-1.14.0-linux-arm64.so");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.14.0/pdftools-sdk-1.14.0-osx-arm64.dylib");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.14.0/pdftools-sdk-1.14.0-osx-x64.dylib");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.14.0/pdftools-sdk-1.14.0-win-x64.dll");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.14.0/pdftools-sdk-1.14.0-win-x86.dll");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.14.0/pdftools-sdk-1.14.0-win-arm64.dll"); -
Alternatively, use the system library path:
-
Add the Pdftools SDK
libdirectory to the system library path.- Windows: Environment variable
PATH - Linux and macOS: Defined by
LD_LIBRARY_PATH - Or specify the path using the VM arg
-Djava.library.path=.
- Windows: Environment variable
-
Load the library using:
- linux-x64
- linux-x64 (with older version of glibc)
- linux-arm64
- osx-arm64
- osx-x64
- win-x64
- win-x86
- win-arm64
System.loadLibrary("pdftools-sdk-1.14.0-linux-x64");System.loadLibrary("pdftools-sdk-1.14.0-linux-x64-glibc2.12");System.loadLibrary("pdftools-sdk-1.14.0-linux-arm64");System.loadLibrary("pdftools-sdk-1.14.0-osx-arm64");System.loadLibrary("pdftools-sdk-1.14.0-osx-x64");System.loadLibrary("pdftools-sdk-1.14.0-win-x64");System.loadLibrary("pdftools-sdk-1.14.0-win-x86");System.loadLibrary("pdftools-sdk-1.14.0-win-arm64");
-
In the default configuration of the Pdftools SDK, the lib directory has the following structure:
lib
├── linux-x64
├── linux-arm64
├── osx-arm64
├── osx-x64
├── win-x64
└── win-x86
└── win-arm64
If you are using the default directory structure, load the library from the respective subfolder in lib directory appropriate for your operating system and system architecture.
There are two ways to load the Pdftools SDK for Java manually:
-
Load the Pdftools SDK using the system library path:
-
Add the directory for your operating system to the system library path.
- Windows: environment variable
PATH. - Linux and macOS: defined by
LD_LIBRARY_PATH. - Specify the path using the Java VM argument
-Djava.library.path=.
- Windows: environment variable
-
Load the library from the system’s library path by using:
System.loadLibrary("PdfToolsSdk");
-
-
Alternatively, load the Pdftools SDK by specifying the absolute path:
- linux-x64
- linux-x64 (with older version of glibc)
- linux-arm64
- osx-arm64
- osx-x64
- win-x64
- win-x86
- win-arm64
System.load(new File("lib/linux-x64/libPdfToolsSdk.so").getAbsolutePath());System.load(new File("lib/linux-x64/libPdfToolsSdk.so").getAbsolutePath());noteEnsure to download a proper package. Native libraries suitable for glibc2.12+ are provided as a separate download.
System.load(new File("lib/linux-arm64/libPdfToolsSdk.so").getAbsolutePath());System.load(new File("lib/osx-arm64/libPdfToolsSdk.dylib").getAbsolutePath());System.load(new File("lib/osx-x64/libPdfToolsSdk.dylib").getAbsolutePath());System.load(new File("lib/win-x64/PdfToolsSdk.dll").getAbsolutePath());System.load(new File("lib/win-x86/PdfToolsSdk.dll").getAbsolutePath());System.load(new File("lib/win-arm64/PdfToolsSdk.dll").getAbsolutePath());
Note the difference between:
System.load(..): Loads the library from an absolute file path.System.loadLibrary(..): Load the library from the system’s library path.
If you are shipping your application, ensure to ship it with the native binary files.
Note that you must load the native binary files from the file system and cannot load them from within a jar file or other bundle.
Optional: Initialize the SDK
Learn how to remove watermarked output of the Pdftools SDK using a valid license key.
Initialization removes watermarks from output files and readies the SDK for production. Without a license key, output files carry a watermark.
Contact the Pdftools sales team through the Contact page to get a full license. For additional information, review Pdftools SDK license management.
To remove watermarks, follow these steps:
-
Find your license key. For more information, review Find the license key.
-
Call
Sdk.initializeonce at application startup, before any other Pdftools SDK method call:Sdk.initialize("YOUR_LICENSE_KEY");Use the license key in the same format you copied it. Include the less-than (
<) and greater-than (>) signs.
To get a working reference with the Sdk.initialize method, follow these steps:
- On the Code samples page, clone a sample repository. For example, clone sdk-examples-java and navigate to
sdk-examples-java/Pdf2ImgSimple. - Find the
Sdk.initializemethod in the main Java file of the sample. For example: Convert PDF to image sample includesSdk.initializemethod in thePdfToolsPdf2ImgSimple.javafile. - Uncomment the call and replace the
"insert-license-key-here"placeholder with your license key.
Implement your use case
- Find more use cases and code examples on the Code samples page.
- For more technical information about the Pdftools SDK for Java, consult the Java technical notes.
- If you need to configure a proxy, review Configure proxy documentation section.