How to rearrange PDF pages in Java

Rearrange PDF Pages – Move, Swap, and Delete PDF Pages in Java.

PDF (Portable Document Format) is a widely used file format for sharing and preserving documents. It is often necessary to rearrange the pages within a large PDF document to organize the content or make it more convenient for reading. In Java programming, you can accomplish this task easily using the GroupDocs.Merger Cloud SDK for Java. The article will walk you through the step-by-step process of how to move, reorder, remove, and rearrange pages in PDF documents programmatically in Java.

The following topics shall be covered in this article:

Java REST API to Rearrange PDF Files and SDK Installation

GroupDocs.Merger Cloud SDK for Java is a feature-rich software development kit that allows developers to work on various formats, including PDF documents. It provides a comprehensive set of features to perform various operations on PDF files, including merging, splitting, moving, rotating, extracting, and, of course, rearranging pages. The SDK is easy to use and provides a convenient API for integrating into Java-based applications efficiently.

You can either download the API’s JAR file or install it using Maven by adding the following repository and dependency into your project’s pom.xml file:

Maven Repository:

<repository>
    <id>groupdocs-artifact-repository</id>
    <name>GroupDocs Artifact Repository</name>
    <url>https://repository.groupdocs.cloud/repo</url>
</repository>

Maven Dependency:

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-merger-cloud</artifactId>
    <version>23.2</version>
    <scope>compile</scope>
</dependency>

Next, you need to sign up for a free trial account or purchase a subscription plan on the GroupDocs website to get your API key. Once you have the Client Id and Client Secret, add below code snippet to a Java-based application:

Now, let’s dive in and learn how to move, reorder, and rearrange PDF pages using Java effectively.

Rearrange PDF Pages Online in Java using REST API

In this section, we will write the steps and an example code snippet to move pages within a PDF document programmatically in Java.

The steps are:

  • Firstly, create an instance of the PagesApi class.
  • Secondly, create an instance of the FileInfo class.
  • Next, set the input PDF document path.
  • Now, create an instance of MoveOptions class.
  • Then provide the setFileInfo and setOutputPath.
  • Provide the page number for setPageNumber and setNewPageNumber.
  • After that, create the MoveRequest class instance and pass the MoveOptions parameter.
  • Finally, call the move method and pass the MoveRequest parameter.

The following code snippet shows how to rearrange pages in PDF documents using Java and REST API:

Finally, the above code snippet will save the rearranged PDF pages on the cloud.

How to Swap PDF Pages in Java using REST API

Similarly, this section will cover how to swap the positions of two pages in a PDF document using GroupDocs.Merger Cloud SDK for Java. Here are steps and a sample code to achieve this:

  • Firstly, create an instance of the PagesApi class.
  • Secondly, create an instance of the FileInfo class.
  • Next, set the input PDF file path.
  • Then, create an instance of the SwapOptions.
  • Then set the setFileInfo and setOutputPath.
  • Provide the page number for setPageNumber and setNewPageNumber.
  • After that, create the SwapRequest class instance and pass the SwapOptions parameter.
  • Finally, call the swap method and pass the SwapRequest parameter.

The following code snippet elaborates on how to change the order of pages in PDF using Java:

How to Remove PDF Pages in Java using REST API

Here, you will learn how to delete unnecessary pages from a PDF document using the GroupDocs.Merger Cloud SDK. Here are the steps and an example code snippet:

  • Firstly, create an instance of the PagesApi class.
  • Secondly, create an instance of the FileInfo class.
  • Next, set the input PDF document path.
  • Then, create an instance of the RemoveOptions.
  • Then provide the setFileInfo and setOutputPath.
  • Now, set the page number to be deleted in setPages as the array list.
  • After that, create the RemoveRequest class instance and pass the RemoveOptions parameter.
  • Finally, call the remove method and pass the RemoveRequest parameter.

The following code snippet elaborates on how to delete PDF document pages in Java using REST API:

Conclusion

In conclusion, you can confidently rearrange PDF pages in Java using GroupDocs.Merger Cloud SDK for Java. The following is what you have learned in this article:

  • how to rearrange PDF pages online programmatically using Java;
  • how to reorder and swap PDF file pages programmatically in Java;
  • and how to remove PDF document pages programmatically using Java.

We also provide an API Reference section that lets you visualize and communicate with our APIs directly through the browser. Java SDK’s complete source code is freely available on Github. Please check the GroupDocs.Merger Cloud SDK for Java Examples here.

Furthermore, we suggest you follow our Getting Started guide for detailed steps and API usage.

Finally, we keep writing new blog posts on different document operations using REST API. So, please get in touch for the regular updates.

Ask a question

If you have any questions about how to rearrange PDF pages, please feel free to ask us on the forum.

FAQs

How do I install the GroupDocs.Merger Cloud SDK for Java?

You can download the SDK from the official GroupDocs website or include it as a Maven dependency in your project.

How can I move pages within a PDF using the GroupDocs.Merger Cloud SDK for Java?

You can specify the source and destination positions to move a page to a new location within the document.

Can I swap the positions of two pages in a PDF using the GroupDocs.Merger Cloud SDK for Java?

Yes, the SDK allows you to swap the positions of two pages, effectively changing their order within the document.

Why is page rearrangement important in PDF documents?

Page rearrangement helps in organizing pages, improving document flow, correcting page order, and merging pages from different PDFs.

See Also

Here are some related articles that you may find helpful: