1. Home
  2. Foxit PDF SDK
  3. Developer Guide for Foxit PDF SDK (Java API)

Developer Guide for Foxit PDF SDK (Java API)

Introduction to Foxit PDF SDK

Have you ever thought about building your own application that can do everything you want with PDF files? If your answer is “Yes”, congratulations! You just found the best solution in the industry that allows you to build stable, secure, efficient and full-featured PDF applications.

Why Foxit is your choice

Foxit is an Amazon-invested leading software provider of solutions for reading, editing, creating, organizing, and securing PDF documents. Customers choose Foxit products for the following reasons:

  • High performance – Very fast on PDF parsing, rendering and conversion.
  • Lightweight footprint – Do not exhaust system resource and deploys quickly.
  • Cross-platform support – Support Microsoft Windows, Linux etc.
  • Great value/affordability – Right features at right price with email and phone support.
  • Security – Safeguards confidential information.

In addition, Foxit products are fully supported by our dedicated support engineers if support and maintenance are purchased. Updates are released on a regular basis. Developers may focus more on their solution building rather than spending time on PDF specification. Foxit will be the right choice if you need solutions with excellent features and low cost!

Evaluation

Foxit PDF SDK allows users to download trial version to evaluate SDK. The trial version has no difference from a standard version except for the 30-day limitation trial period and the trail watermarks that will be generated on the PDF pages. After the evaluation period expires, customers should contact Foxit sales team and purchase licenses to continue using Foxit PDF SDK.

License

Developers should purchase licenses to use Foxit PDF SDK in their solutions. Licenses grant users permissions to release their applications based on PDF SDK libraries. However, users are prohibited to distribute any documents, sample codes, or source codes in the SDK released package to any third party without the permission from Foxit Software Incorporated.

About this guide

This guide is intended for developers who need to integrate Foxit PDF SDK for Java into their own applications. It aims at introducing installation package structure on desktop platform with Java, and the usage of SDK.

Getting Started

It’s very easy to setup Foxit PDF SDK and see it in action! This guide will provide you with a brief introduction about our SDK package. As a cross-platform product, Foxit PDF SDK supports the identical interfaces for desktop system of Windows, Linux, and Mac. The following sections introduce the contents of the installation package as well as how to run a demo, and create your own project.

System Requirements

Linux

Platform System Requirement JDK Version Memo
Windows Windows XP, Vista, 7, 8 and 10 (32-bit and 64-bit)
Windows Server 2003, 2008 and 2012 (32-bit and 64-bit)
At least 1.7, Better 1.8. It only supports for Windows 8/10 classic style, but not for Store App or Universal App.
At least 1.7, Better 1.8.

What is in the package

Package for Windows Java is named “foxitpdfsdk_6_0_win_java.zip” and package for Linux Java is named “foxitpdfsdk_6_0_linux_java.zip”. They have the same structure, so in this guide mainly introduce “foxitpdfsdk_6_0_win_java.zip” as an example.

Download the package for Windows Java and extract it to a new directory like “foxitpdfsdk_6_0_win_java”. The structure of the release package is shown in Figure 2 1.

NOTE: the highlighted rectangle in the figure is just the version of Foxit PDF SDK. Here the SDK version is 6.0, so it shows 6_0 in the package name. Other highlighted rectangles in other figures have the same meaning in this guide.

This package contains the following folders:

docs: API references, quick start guide
lib: libraries and license files
samples: sample projects and demos

Figure 2-1

Foxit PDF SDK provides “fsdk.jar” file in directory “\lib”. It contains 14 packages that are shown in Figure 2 2. Please refer to API Reference in directory “\doc” for more details about classes and APIs.

Figure 2-2

How to initialize Foxit PDF SDK

It is necessary for applications to initialize Foxit PDF SDK with license before calling any APIs. The static function Library.initialize(sn, key) is provided in com.foxit.sdk.common.Library. The trial license files can be found in the “lib” folder.
Following is an example:

// The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
// The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
int error_code = Library.initialize("sn", "key");
if (error_code != e_ErrSuccess) {
  return;
}

How to run a demo

Foxit PDF SDK provides several simple demos in directory “\examples”. All these demos can be run directly with the “.bat” files or “.sh” files in directory “\examples\simple_demo”:

  • Go to directory “\examples\simple_demo”, and run all demos by “RunAllDemo.bat” for Windows or by “RunAllDemo.sh” for Linux.
  • If you want to run a specific single demo, please locate to the directory of the demo, for example locate to “\examples\simple_demo\annotation”, and run this demo by “RunDemo.bat” for Windows or by “RunDemo.sh” for Linux.
  • “\examples\simple_demo\input_files” contains all the input files used among these demos. Some demos will generate output files (pdf, text or image files) to a folder named by the demo name under “example\simple_demo\output_files\”.

    How to create a simple project

    In this section, we will show you how to use Foxit PDF SDK for Windows Java (Linux Java is similar to Windows Java) to create a simple project that renders the first page of a PDF to a bitmap and saves it as a JPG image. For better writing code, we use Eclipse IDE to create a Java project called “test”. Then follow the steps below:

    1. 1) Copy “lib” folder from the download package to the project folder, and then refresh the project. The structure of the test project is shown in Figure 2 3.
    2. Figure 2-3
    3. Add “fsdk.jar” to the project. Right click the test project, select “Build Path > Configure Build Path > Libraries > Add JARs”, and choose the “fsdk.jar” in “test/lib” as shown in Figure 2 4.
    4. Figure 2-4
    5. Configure build path for .dll (for Windows) and .so (for Linux). Right click the test project, select “Build Path > Configure Build Path > Source > Native library location: (None) > Edit”, and locate to the “test/lib” folder as shown in Figure 2 5.
    6. Figure 2-5
    7. Create a new class file called test.java under “test/src/test” directory.
    8. Open the “test.java” file, import the classes that you need to use in “fsdk.jar”. Here, we just import the classes as follows:
    9. import com.foxit.sdk.PDFException;
      import com.foxit.sdk.common.Bitmap;
      import com.foxit.sdk.common.Image;
      import com.foxit.sdk.common.Library;
      import com.foxit.sdk.common.Renderer;
      import com.foxit.sdk.common.fxcrt.Matrix2D;
      import com.foxit.sdk.pdf.PDFDoc;
      import com.foxit.sdk.pdf.PDFPage;
    10. Initialize Foxit PDF SDK. This should be done before calling any other APIs in Foxit PDF SDK. The trial license files can be found in the “lib” folder.
    11. // The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
      // The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
      String sn = "";
      String key = "";		
      int error_code = Library.initialize(sn, key);
      if (error_code != e_ErrSuccess) {
      return;
      }
    12. Load a PDF document, and parse the first page of the document. Assume that you have already put a “Sample.pdf” to the “test” folder.
    13. // load a "Sample.pdf" document.
      PDFDoc doc = new PDFDoc("Sample.pdf");
      error_code = doc.load(null);
      if (error_code != e_ErrSuccess) {
      	return;
      }
       
      // Get the first page of the document.
      PDFPage page = doc.getPage(0);
       
      // Parse page.
      page.startParse(e_ParsePageNormal, null, false);
    14. Render a Page to a bitmap and save it as a JPG file.
    15. int width = (int) page.getWidth();
      int height = (int) page.getHeight();
      Matrix2D matrix = page.getDisplayMatrix(0, 0, width, height, page.getRotation());
       
      // Prepare a bitmap for rendering.
      Bitmap bitmap = new Bitmap(width, height, e_DIBArgb, null, 0);
      bitmap.fillRect(0xFFFFFFFF, null);
       
      // Render page
      Renderer render = new Renderer(bitmap, false);
      render.startRender(page, matrix, null);
       
      // Add the bitmap to image and save the image.
      Image image = new Image();
      image.addFrame(bitmap);
      image.saveAs("testpage.jpg");

      Note: The value of “sn” can be got from “gsdk_sn.txt” (the string after “SN=”) and the value of “key” can be got from “gsdk_key.txt” (the string after “Sign=”).

    16. Load a PDF document, and parse the first page of the document. Let us assume that you have already put a “Sample.pdf” to the “test_win\test_win” folder.
    17. PDFDoc doc("Sample.pdf");
      ErrorCode error_code = doc.Load();
      if (error_code!= foxit::e_ErrSuccess) return 0;
      PDFPage page = doc.GetPage(0);
      page.StartParse(foxit::pdf::PDFPage::e_ParsePageNormal, NULL, false);
    18. Render a Page to a bitmap and save it as a JPG file.
    19. int width = static_cast<int>(page.GetWidth());
      int height = static_cast</int><int>(page.GetHeight());
      Matrix matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
       
      // Prepare a bitmap for rendering.
      Bitmap bitmap(width, height, Bitmap::e_DIBArgb, NULL, 0);
      bitmap.FillRect(0xFFFFFFFF, NULL);
      // Render page.
      Renderer render(bitmap, false);
      render.StartRender(page, matrix, NULL);
       
      // Add the bitmap to image and save the image.
      Image img;
      img.AddFrame(bitmap);
      img.SaveAs("testpage.jpg");
      </int>
    20. Build and Run the project. Right-click the test project in package explorer, and then choose “Run as > Java Application” to run it. The “testpage.jpg” will be generated in the “test” folder (See Figure 2 6).
    21. Figure 2-6
      package test;
       
      import com.foxit.sdk.PDFException;
      import com.foxit.sdk.common.Bitmap;
      import com.foxit.sdk.common.Image;
      import com.foxit.sdk.common.Library;
      import com.foxit.sdk.common.Renderer;
      import com.foxit.sdk.common.fxcrt.Matrix2D;
      import com.foxit.sdk.pdf.PDFDoc;
      import com.foxit.sdk.pdf.PDFPage;
       
      import static com.foxit.sdk.common.Bitmap.e_DIBArgb;
      import static com.foxit.sdk.common.Constants.e_ErrSuccess;
      import static com.foxit.sdk.pdf.PDFPage.e_ParsePageNormal;
       
      public class test {
       
      	public static void main(String[] args) throws PDFException {
       
      		// The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
      		// The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
      		String sn = "";
      		String key = "";
      		int error_code = Library.initialize(sn, key);
      		if (error_code != e_ErrSuccess) {
      			return;
      		}
       
      		// load a "Sample.pdf" document.
      		PDFDoc doc = new PDFDoc("Sample.pdf");
      		error_code = doc.load(null);
      		if (error_code != e_ErrSuccess) {
      			return;
      		}
       
      		// Get the first page of the document.
      		PDFPage page = doc.getPage(0);
       
      		// Parse page.
      		page.startParse(e_ParsePageNormal, null, false);
       
      		int width = (int) page.getWidth();
      		int height = (int) page.getHeight();
      		Matrix2D matrix = page.getDisplayMatrix(0, 0, width, height, page.getRotation());
       
      		// Prepare a bitmap for rendering.
      		Bitmap bitmap = new Bitmap(width, height, e_DIBArgb, null, 0);
      		bitmap.fillRect(0xFFFFFFFF, null);
       
      		// Render page.
      		Renderer render = new Renderer(bitmap, false);
      		render.startRender(page, matrix, null);
       
      		// Add the bitmap to image and save the image.
      		Image image = new Image();
      		image.addFrame(bitmap);
      		image.saveAs("testpage.jpg");
      	}
      }

      Note:If you have not installed Eclipse IDE in your machine, you can create a Java file, and then use command line to build and run it. For example, we use the test.java file created in the previous Eclipse IDE, and then follow the steps below:

    1. Create a folder named “test_cm”. Put the test.java into this folder. (Please note that you need to delete the first line “package test;” in the “test.java”).
    2. Copy “lib” folder to the same folder (test_cm) with “test.java”.
    3. Put a PDF document named “Sample.pdf” into “test_cm” folder.
    4. Open command line window and navigate to “test_cm” folder, and then use the following commands to build and run the Java file.

    For Windows,

    javac -cp .;./lib/fsdk.jar *.java
    java -Djava.library.path=lib -classpath .;lib/fsdk.jar test

    For Linux,

    javac -cp .:./lib/fsdk.jar *.java
    java -Djava.library.path=lib -classpath .:lib/fsdk.jar test
Updated on June 20, 2018

Was this article helpful?

Related Articles