How to Change Size of PDF Page in Java

This sharp tutorial explains how to change size of PDF page in Java. It has all the details to configure the development environment, a detailed sequence of steps that can be followed to implement the feature, and a runnable sample code that demonstrates the process to change page size of PDF in Java. You will also learn the process to calculate the page size in points that is to be provided to the methods in this application.

Steps to Edit PDF Page Size in Java

  1. Set the development environment to use Aspose.PDF for Java to change page size
  2. Load the source PDF using the Document class object to modify the page dimensions
  3. Access the default page collection in the loaded PDF file
  4. Access the target page whose size is to be modified
  5. Set the page size width and height in points
  6. Save the updated PDF file on the disk or to a stream with a specified page size

These steps describe the procedure to change PDF paper size in Java. You may load the source PDF file into the Document class object that supports many other overloads also to control the document loading process. The Document class contains the PageCollection object that can be accessed using the getPages() method ignorer to get the reference to the target page. The Page class has the method setPageSize() that requires the new width and height for the updated page.

Code to Change the Size of a PDF Page in Java

The above code demonstrates the process to modify PDF page size in Java. You have to calculate the new page size in points such that one inch equals to 72 points in Aspose.PDF. Now if you want to calculate the size in points for a page having dimensions 8.3 x 11.7 inches, you may multiply 8.3 by 72 and 11.7 by 72 to get the dimensions in points that are to be provided to the setPageSize() method like Envelop #10 size is demonstrated in this sample code.

We have learned here the process to change PDF page dimensions in Java. If you want to learn the process to protect the output PDF file, refer to the article on how to protect PDF with password in Java.