How to Linearize PDF with Java

Learn how to use Linearize PDF API Tool from Datalogics with Java to optimize PDFs for fast web view
Share this page

Why Use Linearize PDF with Java?

The pdfRest Linearize PDF API Tool provides a straightforward way to optimize PDF files for faster web view. Linearization, also known as "web optimization" or "fast web view," restructures a PDF document so that its pages can be displayed one by one as they are downloaded, rather than waiting for the entire file to be downloaded first. This tutorial will demonstrate how to send an API call to Linearize PDF using Java, which is particularly useful for developers working in Java environments who need to integrate PDF optimization into their applications.

Consider a scenario where a user is trying to access a large PDF document from a web server. If the PDF is not linearized, the user might experience significant delays before being able to view the first page, leading to a poor user experience. By using the Linearize PDF API, the document can be prepared for efficient online viewing, allowing end-users to start reading the PDF almost immediately while the rest of the document continues to download in the background.

Linearize PDF with Java Code Example

import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import org.json.JSONObject;

public class LinearizedPDF {

  // Specify the path to your file here, or as the first argument when running the program.
  private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf";

  // Specify your API key here, or in the environment variable PDFREST_API_KEY.
  // You can also put the environment variable in a .env file.
  private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

  public static void main(String[] args) {
    File inputFile;
    if (args.length > 0) {
      inputFile = new File(args[0]);
    } else {
      inputFile = new File(DEFAULT_FILE_PATH);
    }

    final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();

    final RequestBody inputFileRequestBody =
        RequestBody.create(inputFile, MediaType.parse("application/pdf"));
    RequestBody requestBody =
        new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
            .addFormDataPart("output", "pdfrest_linearized")
            .build();
    Request request =
        new Request.Builder()
            .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
            .url("https://api.pdfrest.com/linearized-pdf")
            .post(requestBody)
            .build();
    try {
      OkHttpClient client =
          new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

      Response response = client.newCall(request).execute();
      System.out.println("Result code " + response.code());
      if (response.body() != null) {
        System.out.println(prettyJson(response.body().string()));
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  private static String prettyJson(String json) {
    // https://stackoverflow.com/a/9583835/11996393
    return new JSONObject(json).toString(4);
  }
}

The provided code is sourced from the pdf-rest-api-samples repository on GitHub.

Breaking Down the Code

The code example above demonstrates how to make a multipart API call to the pdfRest Linearize PDF endpoint. Let's break down the key components of this code:

// Specify the path to your file here, or as the first argument when running the program.
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf";

This line sets the default file path for the PDF to be linearized. The user can also provide the file path as an argument when running the program.

// Specify your API key here, or in the environment variable PDFREST_API_KEY.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

The API key is necessary for authentication with the pdfRest API. It can be hardcoded or stored as an environment variable.

final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();

This snippet initializes the Dotenv library, which is used to load environment variables from a .env file, if available.

final RequestBody inputFileRequestBody =
    RequestBody.create(inputFile, MediaType.parse("application/pdf"));

This creates a request body with the PDF file's content and specifies the media type as "application/pdf".

RequestBody requestBody =
    new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
        .addFormDataPart("output", "pdfrest_linearized")
        .build();

This code constructs the multipart request body, including the file and the desired output filename.

Request request =
    new Request.Builder()
        .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
        .url("https://api.pdfrest.com/linearized-pdf")
        .post(requestBody)
        .build();

The request is built with the API key header, the endpoint URL, and the POST method containing the request body.

Upon executing the request, the program prints the response code and, if successful, outputs the pretty-printed JSON response.

Beyond the Tutorial

By following the steps above, we have successfully called the Linearize PDF API using Java to optimize a PDF file for faster web viewing. This can greatly enhance the user experience by allowing immediate access to the content of large PDF documents on the web.

To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information on how to use the API, refer to the API Reference Guide. Remember, this is an example of a multipart API call, and code samples using JSON payloads can be found at the pdf-rest-api-samples repository.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.