How to Change the Order of Pages in PDF using Java

This tutorial explains how to change the order of pages in PDF using Java. It contains all the steps to establish the environment, and write the application and also provides a runnable sample code to move PDF pages using Java. You will learn different techniques to insert pages at the end of the document or between some existing pages as per the requirement.

Steps to Change Order of Pages in PDF using Java

  1. Set the IDE to use Aspose.PDF for Java to rearrange PDF pages
  2. Create a new PDF file using the document class object to change order of pages
  3. Add a few pages and set some sample text on each page to test the feature
  4. Get the reference to the page to be moved
  5. Add this page to the pages collection of the PDF that adds it to the end of the list
  6. Delete the source page and repeat the page moving process for more pages before saving it on the disk

These steps describe the process to change the order of pages in PDF using Java. In these steps, the process is explained to move a page to the end of the document however the sample code will also demonstrate the process to insert a page at a particular index within the collection of pages. Note that once the page is added or inserted somewhere, the original page is deleted from the collection.

Code to Reorder Pages in a PDF using Java

This code demonstrates the process of how to rearrange pages in a PDF using Java. The PageCollection.add() method is used to add the page at the end of the collection whereas the insert() method takes the index where the page is to be inserted along with the reference to the source page. In this program, the intermediate files are saved to show the result of page movements at different stages.

This article has taught us how to change sequence of pages in PDF using Java. If you want to learn the process to change the size of a PDF page in Java, refer to the article on how to change size of PDF in Java.

 English