Crop PDF via Java

PDF cropping programmatically using Aspose.PDF for Java Library

Crop PDF with Java

Do you need to crop PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. With Java libraries like Aspose.PDF – are stand-alone solutions that don’t rely on other software and are ready for commercial use. They cover all possible needs of professional Java developers.

This Java library enables developers to crop PDF documents quickly and efficiently through programming. With this library, you can easily crop PDF with a number of properties, such as the width, height, bleed-, crop- and trimbox. Aspose.PDF allows you to access these properties. It is a standalone solution for processing PDF documents in Java that does not require the installation of any third-party software.

In order to crop PDF file, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful, and easy-to-use document manipulation API for the Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

How to Crop PDF via Java


You need Aspose.PDF for Java to try the code in your environment.

  1. Open a PDF document using Document object.
  2. Choose the type of box and create new Box Rectagle
  3. Save the updated document to the new path using the Save() method.

If you want to crop a PDF page, a cropping solution can help. With this powerful Aspose.PDF Java library, you can change your PDF while keeping its quality. This professional tool uses Java to crop PDF effectively. The following example demonstrates how to modify a PDF document in Java:

Crop PDF Files - Java

This sample code shows how to Crop PDF Page - Java

Input file:

File not added

Output format:

Output file:


	    // Open document
        Document pdfDocument = new Document(_dataDir + "sample.pdf");

        public static void CropPagesPDF() {
        Document pdfDocument = new Document("crop_page.pdf");
        Page page = pdfDocument.getPages().get_Item(1);

        System.out.println(page.getCropBox());
        System.out.println(page.getTrimBox());
        System.out.println(page.getArtBox());
        System.out.println(page.getBleedBox());
        System.out.println(page.getMediaBox());

        // Create new Box Rectagle
        Rectangle newBox = new Rectangle(100, 100, 80, 10);

        page.setCropBox(newBox);
        page.setTrimBox(newBox);
        page.setArtBox(newBox);
        page.setBleedBox(newBox);

        // Save output document
        pdfDocument.save(_dataDir + "crop_page_modified.pdf");
    }

About Aspose.PDF for Java API

Aspose.PDF for Java API is a library that enables developers to add PDF processing capabilities to their applications. It can be used to build any type of 32-bit and 64-bit applications to generate or read, convert and manipulate PDF files without the use of Adobe Acrobat. Aspose.PDF for Java allows developers to insert tables, graphs, images, hyperlinks, custom fonts - and more - into PDF documents. Moreover, it is also possible to crop PDF. Aspose.PDF for Java provides excellent security features to develop secure PDF files.

You can find more information about Aspose.PDF for Java API on documentation and examples on how to use API. Some of the key features of Aspose.PDF for Java API include support for various file formats including HTML, XFA, TXT, PCL, XML, XPS and image file formats, support for various PDF versions, and extensive hyperlink functionality.