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.
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
- Eclipse IDE
- Command line
-
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
-
Clone the sdk-examples-java repository:
git clone https://github.com/pdf-tools/sdk-examples-java.git -
Compile the Java source file:
- linux-x64
- linux-x64 (with older version of glibc)
- osx-arm64
- osx-x64
- win-x64
- win-x86
javac -cp jar/com.pdftools.jar:. PdfToolsPdf2ImgSimple.javajavac -cp jar/com.pdftools.jar:. PdfToolsPdf2ImgSimple.javajavac -cp jar/com.pdftools.jar:. PdfToolsPdf2ImgSimple.javajavac -cp jar/com.pdftools.jar:. PdfToolsPdf2ImgSimple.javajavac -cp jar/com.pdftools.jar;. PdfToolsPdf2ImgSimple.javajavac -cp jar/com.pdftools.jar;. PdfToolsPdf2ImgSimple.java -
Run the sample:
- linux-x64
- linux-x64 (with older version of glibc)
- osx-arm64
- osx-x64
- win-x64
- win-x86
java -cp jar/com.pdftools.jar:bin:. PdfToolsPdf2ImgSimple <inputpath> <outputpath>java -cp jar/com.pdftools.jar:bin:. PdfToolsPdf2ImgSimple <inputpath> <outputpath>java -cp jar/com.pdftools.jar:bin:. PdfToolsPdf2ImgSimple <inputpath> <outputpath>java -cp jar/com.pdftools.jar:bin:. PdfToolsPdf2ImgSimple <inputpath> <outputpath>java -cp jar/com.pdftools.jar;bin;. PdfToolsPdf2ImgSimple <inputpath> <outputpath>java -cp jar/com.pdftools.jar;bin;. PdfToolsPdf2ImgSimple <inputpath> <outputpath>
The sample runs without a license key, and the output carries a watermark. To remove the watermark, open the main Java file (for example, PdfToolsPdf2ImgSimple.java) and uncomment the Sdk.initialize method call in main(). Replace the "insert-license-key-here" placeholder with your license key. Include the less-than (<) and greater-than (>) signs.
You can apply a similar procedure as 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 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.6.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
<classifier>linux-x64</classifier>
<type>so</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
<classifier>linux-x64-glibc2.12</classifier>
<type>so</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
<classifier>osx-arm64</classifier>
<type>dylib</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
<classifier>osx-x64</classifier>
<type>dylib</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
<classifier>win-x64</classifier>
<type>dll</type>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.pdftools</groupId>
<artifactId>pdftools-sdk</artifactId>
<version>1.6.1</version>
<classifier>win-x86</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)
- osx-arm64
- osx-x64
- win-x64
- win-x86
System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.6.1/pdftools-sdk-1.6.1-linux-x64.so");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.6.1/pdftools-sdk-1.6.1-linux-x64-glibc2.12.so");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.6.1/pdftools-sdk-1.6.1-osx-arm64.dylib");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.6.1/pdftools-sdk-1.6.1-osx-x64.dylib");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.6.1/pdftools-sdk-1.6.1-win-x64.dll");System.load(System.getProperty("user.home") + "/.m2/repository/com/pdftools/pdftools-sdk/1.6.1/pdftools-sdk-1.6.1-win-x86.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)
- osx-arm64
- osx-x64
- win-x64
- win-x86
System.loadLibrary("pdftools-sdk-1.6.1-linux-x64");System.loadLibrary("pdftools-sdk-1.6.1-linux-x64-glibc2.12");System.loadLibrary("pdftools-sdk-1.6.1-osx-arm64");System.loadLibrary("pdftools-sdk-1.6.1-osx-x64");System.loadLibrary("pdftools-sdk-1.6.1-win-x64");System.loadLibrary("pdftools-sdk-1.6.1-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.
Optional: Initialize the SDK
The Pdftools SDK runs without an explicit Sdk.initialize call by auto-activating an internal test license that watermarks the output. Call Sdk.initialize with your license key to remove the watermark.
Contact the Pdftools sales team through the Contact page to get a full license.
Call Sdk.initialize once 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.
For a working reference, see the Sdk.initialize method in the Pdf2ImgSimple Java sample. In SDK samples the call is commented out by default with an "insert-license-key-here" placeholder; uncomment it and replace the 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.