How to Split PDF with Java

Learn how to split a PDF into multiple documents using pdfRest Split PDF API Tool and Java
Share this page

Why Use Split PDF with Java?

The pdfRest Split PDF API Tool is a powerful resource for developers looking to automate the process of splitting PDF files into separate documents based on page ranges. This tutorial will demonstrate how to send an API call to the Split PDF endpoint using Java, a language widely used for backend development and automation tasks. The ability to programmatically split PDFs is essential for applications that need to manage and organize large documents efficiently.

Consider a scenario where a legal firm needs to extract specific pages from large case files to share with different departments or clients. Instead of manually selecting and extracting these pages, a Java application can use the Split PDF API to automate the process, saving time and reducing the risk of human error. This functionality is also beneficial for educational institutions, publishing houses, and any organization that handles document management and distribution.

Split 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 SplitPDF {

  // 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("pages[]", "1-3,5")
            .addFormDataPart("pages[]", "1-2")
            .addFormDataPart("output", "pdfrest_split")
            .build();
    Request request =
        new Request.Builder()
            .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
            .url("https://api.pdfrest.com/split-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) {
    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 Java code example above demonstrates how to make a multipart/form-data POST request to the Split PDF API endpoint. Let's break down the key components of the code:

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

This snippet creates a File object for the PDF to be split and wraps it in a RequestBody, indicating the media type as 'application/pdf'.

RequestBody requestBody = new MultipartBody.Builder()
    .setType(MultipartBody.FORM)
    .addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
    .addFormDataPart("pages[]", "1-3,5")
    .addFormDataPart("pages[]", "1-2")
    .addFormDataPart("output", "pdfrest_split")
    .build();

The RequestBody is built using a MultipartBody.Builder. It includes the PDF file, the page ranges to split ("1-3,5" and "1-2"), and an output prefix ("pdfrest_split").

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

The Request object includes the API key (retrieved from environment variables or the default value), the API endpoint URL, and the previously built request body. The API key should be kept confidential.

Response response = client.newCall(request).execute();

An OkHttpClient is used to execute the request. The response from the server is captured in a Response object.

System.out.println(prettyJson(response.body().string()));

The response body is printed to the console in a human-readable JSON format using the prettyJson method.

Beyond the Tutorial

By following the tutorial above, you've learned how to call the Split PDF API using Java to programmatically split a PDF document into separate files based on specified page ranges. This capability can be integrated into larger Java applications to automate document processing workflows.

To explore more possibilities and experiment with the pdfRest API Tools, you can visit the API Lab. For a comprehensive understanding of the available endpoints and their parameters, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads for the Split PDF API and other endpoints can be found at the pdf-rest-api-samples repository on GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us