How to Rearrange Pages in Word using Java

Rearrange Word Pages in Java

Word processing documents are one of the most common file formats that are used to draft documents. While dealing with multiple large files, it is never easy to move pages without losing the formatting. In order to rearrange the pages, this article discusses, how to programmatically move pages in the Word documents (DOC/DOCX) in Java.

Java API to Move Word Document Pages

GroupDocs.Merger provides the Java API to deal with documents and their pages. It allows moving, removing, splitting of documents, and extraction of pages, changes in page orientation, and rotation of document pages within the Java applications. I will use this API to move pages of DOC/DOCX files. 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>

Move Pages in Word Documents using Java

Just order a page to move to the new position, it will. The following are the steps that rearrange the pages of a Word document in Java.

  • Set the page number of the target page and its new position using the MoveOptions class.
  • Load the DOC/DOCX file using the Merger class.
  • Use movePage() method to move the set page.
  • Save the rearranged document using the save() method.

The following Java source code rearranges the pages of a Word document. Precisely, it moves the 7th page of a DOCX document to the 2nd place.

Get a Free API License

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

Conclusion

To sum up, we learned how to change page order of a Word document in Java. We saw the source code example that changed the page position in a DOCX file. You can build your own online application to rearrange Word pages online. For more details about the API, visit the documentation. For queries, contact us via the forum.

See Also