How to Rearrange PDF Pages in Java

PDF is among one of the most in-use portable file formats. For large documents, it is always challenging to change the order of pages without losing the formatting. This article discusses, how to rearrange PDF pages programmatically in Java.

Rearrange PDF Pages in Java

Java API to Rearrange PDF Pages & Merge Documents

GroupDocs provides GroupDocs.Merger for Java to change the order of pages in documents. This API enables merging multiple documents, removing, splitting, and extraction of pages, rotation, and changes in page orientation of document pages within the applications. For the details and other features of the API, you can visit theĀ documentation.

Download and Configure

Get the library from the downloads section. For your Maven-based Java application, just add the following pom.xml configuration. After this, you can try the examples of this article as well the many more example available on GitHub. For the details, you may visit the API Reference.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-merger</artifactId>
        <version>22.2</version> 
</dependency>

Rearrange PDF Pages in Java

The following are the steps that help you change the sequence of PDF document pages in Java.

  • Define the existing and new position of the page in MoveOptions class.
  • Load the PDF document using the Merger class.
  • Use the movePage() method to rearrange pages according to defined options.
  • Save the reordered PDF file using the save() method.

The following Java source code rearranges the pages of PDF documents. Precisely, it moves the 6th page of the document to the 1st place.

Here is the output of the above code.

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To conclude, we learned to reorder documents by changing the page sequence of the PDF files in Java within the application. We have seen the running example that changes the position of the page. You can try building a simple application that can organize the PDF files by easily shuffling their pages.

For more details about the API, visit the documentation. For queries, contact us via the forum.

See Also