Get started with Java
This guide walks you through the steps to use a sample project, 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 for free.
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 sample project and convert a PDF file to an image.
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
-
Download a sample project, and then unzip the file.
-
In the command line, navigate to the root directory of the unzipped sample project, and then compile the Java source file:
- macOS and Linux
- Windows
javac -cp jar/com.pdftools.jar:. PdfToolsPdf2ImgSimple.java
javac -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.tiff
java -cp jar/com.pdftools.jar;bin;. PdfToolsPdf2ImgSimple PdfPrimerWhitepaper.pdf tiff_output.tiff
The code sample 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
-
Download a sample project, and then unzip the file.
-
From the unzipped file, import the
.project
file into the Eclipse IDE. The source filePdfToolsPdf2ImgSimple.java
is compiled automatically. -
Right-click the
java
file, 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 samples. 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)
- osx-arm64
- osx-x64
- win-x64
- win-x86
- win-arm64
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
<classifier>linux-x64</classifier>
<type>so</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
<classifier>linux-x64-glibc2.12</classifier>
<type>so</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
<classifier>osx-arm64</classifier>
<type>dylib</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
<classifier>osx-x64</classifier>
<type>dylib</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
<classifier>win-x64</classifier>
<type>dll</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
<classifier>win-x86</classifier>
<type>dll</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.8.1</version>
<classifier>win-arm64</classifier>
<type>dll</type>
</dependency>
- Download the Pdftools SDK for Java product kit, and then unzip the file.
- Add the
jar
file to your project's classpath. - Copy the
lib
folder 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)
- osx-arm64
- osx-x64
- win-x64
- win-x86
- win-arm64
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.8.1/pdftools-sdk-1.8.1-linux-x64.so");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.8.1/pdftools-sdk-1.8.1-linux-x64-glibc2.12.so");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.8.1/pdftools-sdk-1.8.1-osx-arm64.dylib");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.8.1/pdftools-sdk-1.8.1-osx-x64.dylib");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.8.1/pdftools-sdk-1.8.1-win-x64.dll");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.8.1/pdftools-sdk-1.8.1-win-x86.dll");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.8.1/pdftools-sdk-1.8.1-win-arm64.dll");
-
Alternatively, use the system library path:
-
Add the Pdftools SDK
lib
directory 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)
- osx-arm64
- osx-x64
- win-x64
- win-x86
- win-arm64
System.loadLibrary("pdftools-sdk-1.8.1-linux-x64");
System.loadLibrary("pdftools-sdk-1.8.1-linux-x64-glibc2.12");
System.loadLibrary("pdftools-sdk-1.8.1-osx-arm64");
System.loadLibrary("pdftools-sdk-1.8.1-osx-x64");
System.loadLibrary("pdftools-sdk-1.8.1-win-x64");
System.loadLibrary("pdftools-sdk-1.8.1-win-x86");
System.loadLibrary("pdftools-sdk-1.8.1-win-arm64");
-
In the default configuration of the Pdftools SDK, the lib
directory has the following structure:
lib
├── linux-x64
├── 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)
- 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/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.
You can try the Pdftools SDK without a license key for free. This section is optional if you want to evaluate this SDK. Initialization only lets you remove watermarks added to output files.
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:
-
Locate your license key. For more information, review Find the license key.
-
Before you call any function of the Pdftools SDK, first call the
Sdk.initialize
method.Sdk.initialize("insert-license-key-here");
Replace the
insert-license-key-here
with the value of your license key. Include the less-than (<
) and greater-than (>
) signs.
To get a working code sample with the Sdk.initialize
method that you can use as a reference for your code, follow these steps:
- On the Code samples page, download and unzip a Java code sample. For example: Download Convert PDF to image sample.
- Unzip the file, and then find the
Sdk.initialize
method in the main Java file of the sample. For example: Convert PDF to image sample includesSDK.initialize
method in thePdfToolsPdf2ImgSimple.java
file. - Uncomment the method and replace
insert-license-key-here
with your license key.
Implement your use case
- Find more use cases and sample projects on the Code samples page.
- For more technical information about the Pdftools SDK for Java, consult the Java technical notes.