Edit PDF Page Size in Java | Change Paper Size in PDF

The PDF’s page size determines how its content appears, prints, or processes. We commonly require changing paper size in PDF documents while preparing for print, converting formats, or standardizing layouts. In this article, you will learn how to edit PDF page size of any PDF file in Java. We will walk you through a step-by-step guide with complete code examples.

This article covers the following:

Java SDK to Edit PDF Page Size

We will use Aspose.PDF for Java to edit PDF page size in Java. It allows developers to create, modify, and convert PDF files programmatically without Adobe Acrobat. The API is easy to integrate, simple to use, and supports powerful PDF manipulation features.

You can download the SDK from the downloads page or include it via Maven:

<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>aspose-pdf</artifactId>
  <version>24.5</version> <!-- Use the latest version -->
</dependency>

Once the library is included in your project, you can begin working with PDF documents using Java code.

Edit PDF Page Size in Java

To change a paper size in a PDF document using the standard page sizes like A3, A4, and Letter in Aspose.PDF for Java, follow these steps:

  1. Load an existing PDF using the Document class.
  2. Iterate through pages and apply a standard PageSize using the Resize() method.
  3. Save the modified PDF using the save() method.

Here’s how to edit PDF page size using standard dimensions like A3, A4, and Letter in Java:

How it Works:

  • PageSize.getA4(): Provides standard A4 page size (595 x 842 points).
  • resize(Pagesize): Resizes the page to the specified standard format.

This is useful when you want to ensure a consistent print format for all pages.

Change Paper Size in PDF with Custom Width and Height

To set custom dimensions for PDF pages in Java, follow these steps:

  1. Open the PDF file with the Document class.
  2. Specify the new width and height in points.
  3. Apply these dimensions using the setPageSize() method for each page.
  4. Save the output file using the save() method.

The following Java code snippet shows how to edit PDF page size to custom dimensions:

How it Works:

  • You define your preferred dimensions manually.
  • Applies flexible layout control, ideal for specific design requirements.

This method is ideal when working with non-standard page sizes or layout corrections.

Get a Free License

You can explore the complete feature set of Aspose.PDF with a free temporary license. No restrictions, no upfront cost. It’s the perfect way to evaluate the full capabilities in your Java project.

Edit PDF Page Size Online

Try resizing PDFs online using the Aspose PDF Resize Tool. It is free, easy to use, and lets you adjust page sizes quickly and accurately.

Image

Resize PDF – Free Resources

Explore more tools and documentation to enhance your development:

Conclusion

In this blog post, you learned how to edit PDF page size in Java using Aspose.PDF for Java. Whether changing paper size to standard sizes like A4 or defining custom dimensions, the library offers complete control over your PDF layout.

For further help or questions, please visit our support forum.

See Also