Page orientation depends on the content layout of the page. As a developer, we can programmatically change the orientation of each page to the one it suits. In this article, we will learn to change the orientation of a page in a Word document in Java. Separately, we will see how to change the landscape orientation to portrait and then how to change the portrait orientation to landscape with the Java applications.

Change Page Orientation of DOC/DOCX in Java

Java API for Changing Word Page Orientation

GroupDocs.Merger for Java is the API to deal with document pages within the Java applications. It allows changing the orientation of Word document pages and many other supporting document formats. We will use it to change the page orientation 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>

Change Orientation of a Word Page to Landscape in Java

Start with how to programmatically make a page landscape in a Word document. The following steps allow changing the page orientation of a Word DOC/DOCX document to landscape in Java:

  • Prepare the orientation option and set it to landscape mode.
  • Load the Word document using Merger.
  • Change the orientation using the changeOrientation() method.
  • Save the updated document using the appropriate save() method.

The following Java code changes the portrait orientation of some pages of a Word document to the landscape.

Change Orientation of a Word Page to Portrait in Java

Likewise, you can set any set of pages of the Word document in portrait orientation. The following steps allow changing the orientation of a Word document page to portrait in Java:

  • Load the Word document using Merger.
  • Prepare the orientation option and set the mode to portrait.
  • Use the changeOrientation() method to change the orientation.
  • Use the appropriate save() method to save the updated document.

The following Java code changes the portrait orientation of some pages of a Word document to portrait.

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 how to change the page orientation of Word documents within Java applications. We discussed the code snippet that changed the landscape orientation of the selected pages of a DOCX file to the portrait. Similarly, we changed the portrait orientation of the selected pages to landscape using the Java code example. Start building your own application that can toggle the orientation of Word pages.

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

See Also