In this brief tutorial, we will walk you through how to rotate PDF using Java. It contains detailed information to configure the IDE, a set of instructions to write the application, and a runnable sample code to rotate PDF pages using Java. An explanation will also be provided to change the orientation of the pages only or change the orientation of the content as well based upon application requirements.
Steps to Rotate All Pages in PDF using Java
- Set the IDE to add Aspose.PDF for Java to change PDF orientation
- Load the input PDF file to change the page orientation using the Document class
- Parse through all the pages to flip each page
- Change the page orientation by changing the length to width and width to height
- Rotate the page contents using the Page.setRotate() method
- Save the rotated PDF on the disk
These steps describe the programming tasks to rotate individual pages in PDF using Java. Initially, the collection of the pages in the loaded PDF file is parsed and the rectangular area of each page is utilized to change the width to height and vice versa. In this case, only the page length and width are interchanged however the contents remain in the same direction that can also be changed using the Page.setRotate() method.
Code to Rotate Pages of PDF using Java
In the aforementioned code, the process to rotate PDF per page using Java is demonstrated. The MediaBox property of the Page class returns the rectangular area that is used to switch the page dimensions by interchanging width with height. If you want to change the orientation of the page contents, use the on90, on180, or on270 value from the Rotation enumerator to set the angle using the Page.setRotate() method.
This topic has shared details on changing the PDF page and content orientation. If you want to learn the process to find and replace text in a PDF file, refer to the article on how to find and replace text in PDF using Java.