Build a Java PDF library app with Apryse Server SDK

This guide shows how to build a simple Java PDF application that uses the Apryse Server SDK and PDFNet library to generate a PDF programmatically. You’ll set up a minimal project, install the SDK, and add the required code to create a blank PDF document. This example provides a practical foundation for building more advanced document‑generation workflows.

To get started, choose your preferred platform from the following tabs.

Prerequisites

Before you start:

Set the JAVA_HOME environment variable to your JDK installation directory so your system can locate Java. If it's not set correctly, you may encounter errors when running your project.

  • For 32-bit binary packages and slimmer binaries (including Java libraries), visit the C++ docs for Server SDK.
  • Install Apache Maven or Gradle if you plan to use a build tool to manage dependencies and run your project.
  • Get your Apryse trial key.

License Key

Apryse collects some data regarding your usage of the SDK for product improvement.

If you wish to continue without data collection, contact us and we will email you a no-tracking trial key for you to get started.

Run Apryse SDK in production

A commercial license key is required for use in a production environment. Contact sales to purchase a commercial license key.

Keep your commercial license key confidential

License keys are uniquely generated and strictly confidential. Don't publish or store them in any public location, including public GitHub repositories. 

1. Set up your project

This section uses a manual setup, where you create a simple Java project and add the Apryse Server SDK using the JAR files from the downloaded package. This approach is useful for quick testing and understanding how the SDK works. If you’re using a build tool such as Maven or Gradle in an existing Java project, skip to the next section.

  1. Download and unzip the PDFNetJava.zip file to a location of your choice.
  2. Open the extracted /PDFNetJava/Samples folder in Visual Studio Code.
  3. In the Samples directory, create a MyApp/java folder structure.
  4. In the java folder, create a MyApp.java file. Your project should now include a similar structure:

Text

1SAMPLES/
2├── MyApp/
3│ └── java/
4│ └── MyApp.java
5├── OCRTest/
6├── OfficeTemplateTest/
7└── ... (other sample folders)

2. Add the Apryse SDK

Next, you'll integrate the Apryse Server SDK into your Java application and add the code needed to generate a PDF. You can use a manual setup by adding the SDK JAR files directly, or use Maven or Gradle to include the SDK as a dependency.

  1. Open the MyApp.java file in Visual Studio Code or your preferred code editor.
  2. Add the following code to the MyApp.java file, update your license key, and save your changes:

Java

MyApp/java/MyApp.java

1import java.io.File;
2import java.io.IOException;
3
4// Import exception handling
5import com.pdftron.common.PDFNetException;
6// Import core PDF functionality
7import com.pdftron.pdf.*;
8// Import low-level SDF (structured document format) classes
9import com.pdftron.sdf.SDFDoc;
10
11public class MyApp
12{
13 public static void main(String[] args)
14 {
15 // Initialize PDFNet library
16 // Replace with your license key
17 PDFNet.initialize("YOUR_LICENSE_KEY");
18 try
19 {
20 // Create new PDF and add blank page
21 PDFDoc doc = new PDFDoc();
22 Page page = doc.pageCreate();
23 doc.pagePushBack(page);
24
25 // Save as a linearized (web-optimized) PDF
26 doc.save("output.pdf", SDFDoc.SaveMode.LINEARIZED, null);
27 }
28 // Handle PDFTron-specific exceptions
29 catch(PDFNetException e)
30 {
31 e.printStackTrace();
32 System.out.println(e);
33 }
34
35 // Print simple console output to confirm program ran
36 System.out.println("Blank PDF created successfully: output.pdf");
37 }
38}

Info

If you're signed in with an Apryse account, your license key is automatically prepopulated in all code snippets.

With this code, you can:

  • Import the required Apryse PDFNet namespaces.
  • Define a main method to serve as the starting point for execution.
  • Initialize the Apryse SDK with a license key.
  • Create a new PDF document, add a blank page, and save it as a linearized PDF.
  • Log errors to the console.
  • Output a simple message to confirm the program ran successfully.

3. Verify your output

Finally, build and run your Java application to confirm that the Apryse Server SDK is working correctly. After the application runs successfully, it will generate a blank PDF file locally. Instructions are provided for manual, Maven, and Gradle-based integrations.

  1. Navigate to the PDFNetJava/Samples/MyApp/java directory.
  2. In the terminal, run this command to compile MyApp.java with the PDFNet.jar file added to the Java classpath:

PowerShell

1javac -cp ".;..\..\..\Lib\PDFNet.jar" MyApp.java

3. Launch the application and load the Apryse Server SDK Java and native library dependencies:

PowerShell

1java --% -Djava.library.path=..\..\..\Lib -cp ".;..\..\..\Lib\PDFNet.jar" MyApp

A successful output looks similar to:

PowerShell

1PDFNet is running in demo mode.
2PackageV2: base
3Blank PDF created successfully: output.pdf

4. Verify a blank output.pdf file was generated in the /PDFNetJava/Samples/MyApp/java directory. The folder structure looks similar to:

Text

1PDFNetJava/
2├── Doc/
3├── Samples/
4│ ├── MyApp/
5│ │ └── java/
6│ │ ├── output.pdf
7│ │ ├── MyApp.class
8│ │ └── MyApp.java
9│ ├── HandwritingOCRTest/
10│ └── ... (other sample folders)

Next steps

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales