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.
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.
-
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 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 as 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 next 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
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
<classifier>linux-x64</classifier>
<type>so</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
<classifier>linux-x64-glibc2.12</classifier>
<type>so</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
<classifier>osx-arm64</classifier>
<type>dylib</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
<classifier>osx-x64</classifier>
<type>dylib</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
<classifier>win-x64</classifier>
<type>dll</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.7.0</version>
<classifier>win-x86</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
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.4.0/pdftools-sdk-1.4.0-linux-x64.so");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.4.0/pdftools-sdk-1.4.0-linux-x64-glibc2.12.so");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.4.0/pdftools-sdk-1.4.0-osx-arm64.dylib");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.4.0/pdftools-sdk-1.4.0-osx-x64.dylib");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.4.0/pdftools-sdk-1.4.0-win-x64.dll");
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.4.0/pdftools-sdk-1.4.0-win-x86.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
System.loadLibrary("pdftools-sdk-1.4.0-linux-x64");
System.loadLibrary("pdftools-sdk-1.4.0-linux-x64-glibc2.12");
System.loadLibrary("pdftools-sdk-1.4.0-osx-arm64");
System.loadLibrary("pdftools-sdk-1.4.0-osx-x64");
System.loadLibrary("pdftools-sdk-1.4.0-win-x64");
System.loadLibrary("pdftools-sdk-1.4.0-win-x86");
-
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
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
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());
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.
Initialize the SDK
After loading the native binary files the final step before using the SDK is to initialize it with your license key. To initialize the Pdftools SDK with your license key, follow these steps:
-
Copy your license key. For more information, review Find the license key.
-
Replace the
<PDFSDK,V1,include-your-key-here>
in the following method with the value of your license key:Sdk.initialize("<PDFSDK,V1,include-your-key-here>");
Use the license key in the same format as you copied it. Include the less-than (
<
) and greater-than (>
) signs.
You can try the Pdftools SDK without a license with watermarked results. For more information, review Pdftools SDK license management. Without a valid license key the output files are watermarked. Get in touch with the Pdftools sales team through the Contact page to get a full license.
Implement your use case
- Find more use cases and sample projects at the Code samples page.
- For more technical information about the Pdftools SDK for Java, consult the Java technical notes.