Crop PDF Pages in Java

Aspose.PDF for Java lets you crop pages either by explicit box coordinates or based on detected content.

Crop a page by setting page boxes

Use this example when you need to apply the same crop area to the main page boxes.

  1. Open the source PDF Document.
  2. Create the new crop Rectangle.
  3. Apply the rectangle to the crop-related page boxes and save the document.
public static void cropPage(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Rectangle newBox = new Rectangle(200, 220, 2170, 1520, true);
        document.getPages().get_Item(1).setCropBox(newBox);
        document.getPages().get_Item(1).setTrimBox(newBox);
        document.getPages().get_Item(1).setArtBox(newBox);
        document.getPages().get_Item(1).setBleedBox(newBox);
        document.save(outputFile.toString());
    }
}

Crop a page by detected content

Use this example when the crop area should be derived from the first detected image on the page.

  1. Open the source PDF Document.
  2. Use ImagePlacementAbsorber to detect image placements.
  3. Set the crop box to the image rectangle if one is found, then save the document.
public static void cropPageByContent(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ImagePlacementAbsorber absorber = new ImagePlacementAbsorber();
        document.getPages().get_Item(1).accept(absorber);
        if (absorber.getImagePlacements().size() > 0) {
            document.getPages().get_Item(1).setCropBox(absorber.getImagePlacements().get_Item(1).getRectangle());
        } else {
            System.out.println("No images found on the first page");
        }
        document.save(outputFile.toString());
    }
}