How to Crop PDF with Java
Why Crop PDF Files with Java?
The pdfRest Set Page Boxes API Tool allows you to programmatically define the page boxes for PDF documents, specifically the CropBox, which controls the visible content area. This tutorial will guide you through the process of sending an API call to set the CropBox using Java to crop PDF files.
Consider a scenario where you need to automate the process of removing unwanted margins from a batch of PDF documents. By using the Set Page Boxes API with Java to adjust the CropBox, you can efficiently crop PDFs to focus on the essential content for improved viewing or further processing.
Crop 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.JSONArray; import org.json.JSONObject; public class PDFWithPageBoxesSet { // 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/your/file.pdf"; // Replace with your file path // 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 = "YOUR_API_KEY"; // Replace with your actual API key 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(); JSONObject page = new JSONObject(); page.put("range", "1"); page.put("left", 50); // Adjust these values to your desired crop page.put("top", 50); page.put("bottom", 50); page.put("right", 50); JSONArray pagesArray = new JSONArray(); pagesArray.put(page); JSONObject box = new JSONObject(); box.put("box", "crop"); // Set the box type to "crop" box.put("pages", pagesArray); JSONArray boxesArray = new JSONArray(); boxesArray.put(box); JSONObject boxOptionsObject = new JSONObject(); boxOptionsObject.put("boxes", boxesArray); final RequestBody inputFileRequestBody = RequestBody.create(inputFile, MediaType.parse("application/pdf")); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) .addFormDataPart("boxes", boxOptionsObject.toString()) .addFormDataPart("output", "cropped_example_out.pdf") .build(); Request request = new Request.Builder() .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) .url("https://api.pdfrest.com/pdf-with-page-boxes-set") .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); } }
Source: GitHub Repository (Note: This example has been modified to focus on cropping)
Breaking Down the Code for Cropping
The code imports necessary libraries for HTTP requests and JSON handling. It defines placeholders for the input file path and API key. Ensure you replace "/path/to/your/file.pdf"
with the actual path to your PDF file and "YOUR_API_KEY"
with your actual pdfRest API key.
JSONObject page = new JSONObject(); page.put("range", "1"); page.put("left", 50); // Adjust these values to your desired crop page.put("top", 50); page.put("bottom", 50); page.put("right", 50);
This section creates a JSON object to define the cropping boundaries for the first page. The left
, top
, bottom
, and right
values specify the new margins in points after the PDF is cropped. Adjust these values to achieve your desired cropping effect.
JSONObject box = new JSONObject(); box.put("box", "crop"); // Set the box type to "crop" box.put("pages", pagesArray);
Here, a JSON object sets the "box"
type to "crop"
, indicating that we want to adjust the CropBox.
RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) .addFormDataPart("boxes", boxOptionsObject.toString()) .addFormDataPart("output", "cropped_example_out.pdf") .build();
This part of the code constructs the multipart request body, including the PDF file, the JSON object containing the CropBox settings, 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/pdf-with-page-boxes-set") .post(requestBody) .build();
The API request is built with your API key in the header and the URL for the Set Page Boxes endpoint. The POST method is used to send the request with the constructed body.
Further Exploration for PDF Cropping
This tutorial demonstrated how to use Java and the pdfRest API to crop PDF files by specifically setting the CropBox. You can modify the left
, top
, bottom
, and right
values in the JSON payload to achieve different cropping results. You can also extend this example to apply different cropping settings to multiple pages or page ranges within a PDF document.
To explore more about the Set Page Boxes API and other pdfRest tools, visit the API Lab and consult the comprehensive API Reference Guide.
Note: This is an example of a multipart API call. JSON payload examples for setting page boxes, including the CropBox, can be found here.