How to Rotate PDF File in Java

PDF documents are the most important file format when it comes to sending and receiving data online. PDF stands for "Portable Document Format" which allows a user to preserve the formatting, rotation and data in permanent form. It is important to have a PDF viewer and editor to work with these documents. PDFs also preserve page's original rotation angle which is helpful in preserving the data in its true format.

Managing PDFs programmatically using Java is an important usecase when it comes to generating reports, invoices or bills on demand. Moreover, it is also useful to be able to rotate pages of a PDF document to correct flaws in the view angle. Doing both of these tasks in Java can be quite challenging. Here, we will be using the IronPDF Java Library to rotate PDF pages.

IronPDF Java Library

IronPDF for Java helps Java developers create, edit and manipulate PDF documents. The library allows developers to work with nearly every aspect of a PDF document layout and formatting, such as the current rotation of one or more pages.

In addition to creating and manipulating PDFs, IronPDF is vastly effective at converting HTML files into pixel-perfect PDFs. IronPDF renders all images and text without losing any formatting. Buttons are clickable, and text boxes are editable in the PDF file.

IronPDF's JAR file can be downloaded and installed from Maven Central or from the product website directly.

Steps to Rotate Document using Java

Prerequisites

To create a PDF application which can rotate pages, you will need the following prerequisites downloaded and installed on your computer:

  1. JDK (Java Development Kit): Install the latest version of JDK on your computer to compile and run the PDF rotation application. The JDK can be downloaded from the official website.
  2. Maven: Maven needs to be installed as it is a build automation tool used primarily for Java projects. The Maven can be downloaded from the Apache Maven website.
  3. IronPDF Java Library: Now the IronPDF for Java library's latest version is needed and should be added as dependency. Add the following IronPDF Java dependency to your project's pom.xml file:

    <dependency>
       <groupId>com.ironsoftware</groupId>
       <artifactId>com.ironsoftware</artifactId>
       <version>2024.3.1</version>
    </dependency>
    XML
  4. You will also need to add Slf4j dependency in the pom.xml file.

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.5</version>
    </dependency>
    XML

Once all the prerequisites are downloaded and installed, the project can now be used for page orientation tasks in Java applications.

Adding Necessary Imports and License Key

First of all, add the following import statements to the top of the main Java source file:

import com.ironsoftware.ironpdf.PdfDocument;  
import com.ironsoftware.ironpdf.edit.PageSelection;  
import com.ironsoftware.ironpdf.page.PageRotation;  
import com.ironsoftware.ironpdf.render.*;  
import java.io.IOException;  
import java.nio.file.*;
JAVA

Next, in the main method, call License.setLicenseKey to set a valid product license key that you obtained at the time of purchase (skip this set if you do not have a license key, or sign up for a trial license key).

License.setLicenseKey("Your license key");
JAVA

Render PDF in Portrait or Landscape Orientation

IronPDF can rotate pages in both portrait and landscape orientation.

ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();  
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);  
PdfDocument newPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com", renderOptions);  
newPdf.saveAs(Paths.get("assets/LandscapePdf.pdf"));
JAVA

IronPDF uses portrait orientation by default. However, developers can override this orientation while converting content from HTML, RTFs, URL, etc into PDF documents with a ChromePdfRenderOptions object. The setPaperOrientation method takes a PaperOrientation value as an argument, which allows to alter the paper orientation of the resulting PDF as desired.

Above, we have set the PaperOrientation to LANDSCAPE. A PdfDocument class is used to convert a URL to PDF document using renderUrlAsPdf method with renderOptions as second argument.

Finally, the document is saved using saveAs method in specified directory.

How to Rotate PDF Pages Using Java - Figure 1

PDF of the IronPDF Home Page, rotated in Landscape orientation from Portrait orientation

Rotating pages by Rotation Angle

For existing documents, ChromePdfRenderOptions object cannot be used to change the page orientation. For these existing PDF documents, page orientation can only be adjusted with rotation based transformations.

// Use the rotatePage/rotateAllPages methods to adjust the page orientation for existing PDFs  
PdfDocument existingPdf = PdfDocument.fromFile(Paths.get("assets/LandscapePdf.pdf"));    

// Rotate the first page of the document only 90 degrees clockwise.  
existingPdf.rotatePage(PageRotation.CLOCKWISE_90, PageSelection.firstPage());  

// Rotate all pages of the document clockwise.  
existingPdf.rotateAllPages(PageRotation.CLOCKWISE_270);  

existingPdf.saveAs(Paths.get("assets/ExistingPdfRotated.pdf"));
JAVA

The above code modifies the PDF document that we created in the previous section. We generated the entire document in landscape previously; but here, IronPDF's rotatePage rotates only the firstPage of the existing document by 90 degrees clockwise (using CLOCKWISE_90). Afterwards, rotateAllPages rotates every page (including the first one) by CLOCKWISE_270.

How to Rotate PDF Pages Using Java - Figure 2: Rotated PDF Output

Rotated PDF Output

Read more about page orientation from the Code Examples section.

How to Rotate PDF Pages Using Java - Figure 3: IronPDF for Java

IronPDF for Java

Summary

In this article, we looked at creating a new document with landscape orientation.

IronPDF for Java is free to use but for deployment purpose it has a commercial license which starts from only $749. You can also access the free trial of full version of IronPDF to test its functionality in production mode.