How to Rotate PDF File in Java
Managing PDFs programmatically in Java is crucial for generating on-demand reports, invoices, or bills. It's also valuable to rotate PDF pages to fix view angle issues. Both tasks can be challenging in Java. This article will use the IronPDF Java Library to simplify PDF page rotation.
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 highly effective at converting HTML files into pixel-perfect PDFs. IronPDF renders all images and text without losing any formatting. Form components are supported 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 that can rotate pages, you will need the following prerequisites downloaded and installed on your computer:
- 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.
- Maven: Maven needs to be installed as it is a build automation tool used primarily for Java projects. Maven can be downloaded from the Apache Maven website.
IronPDF Java Library: Now you need the latest version of the IronPDF for Java library and should add it as a dependency. Add the following IronPDF Java dependency to your project's
pom.xml
file:<dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf-jdk8</artifactId> <version>2021.9.3663</version> </dependency>
<dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf-jdk8</artifactId> <version>2021.9.3663</version> </dependency>
XMLYou will also need to add the Slf4j dependency in the
pom.xml
file.<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>2.0.5</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>2.0.5</version> </dependency>
XML
Once you have downloaded and installed all the prerequisites, you can use the project 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.*;
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.*;
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 step if you do not have a license key, or sign up for a trial license key).
License.setLicenseKey("Your license key");
License.setLicenseKey("Your license key");
Render PDF in Portrait or Landscape Orientation
IronPDF can rotate pages in both portrait and landscape orientation.
// Create render options with landscape orientation
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);
// Render the URL as a PDF document
PdfDocument newPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com", renderOptions);
// Save the document to the specified path
newPdf.saveAs(Paths.get("assets/LandscapePdf.pdf"));
// Create render options with landscape orientation
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);
// Render the URL as a PDF document
PdfDocument newPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com", renderOptions);
// Save the document to the specified path
newPdf.saveAs(Paths.get("assets/LandscapePdf.pdf"));
IronPDF uses portrait orientation by default. However, developers can override this orientation while converting content from HTML, RTFs, URLs, etc into PDF documents with a ChromePdfRenderOptions
object. The setPaperOrientation
method takes a PaperOrientation
value as an argument, which allows you to alter the paper orientation of the resulting PDF as desired.
In the above code, the PaperOrientation
is set to LANDSCAPE
. A PdfDocument
class is used to convert a URL to a PDF document using the renderUrlAsPdf
method with renderOptions
as the second argument.
Finally, the document is saved using the saveAs
method in the specified directory.
The output PDF file
Rotating Pages by Rotation Angle
For existing documents, the ChromePdfRenderOptions
object cannot be used to change the page orientation. For these existing PDF documents, the page orientation can only be adjusted with rotation-based transformations.
// Load an existing PDF document from the specified path
PdfDocument existingPdf = PdfDocument.fromFile(Paths.get("assets/LandscapePdf.pdf"));
// Rotate the first page of the document 90 degrees clockwise
existingPdf.rotatePage(PageRotation.CLOCKWISE_90, PageSelection.firstPage());
// Rotate all pages of the document 270 degrees clockwise
existingPdf.rotateAllPages(PageRotation.CLOCKWISE_270);
// Save the modified document to the specified path
existingPdf.saveAs(Paths.get("assets/ExistingPdfRotated.pdf"));
// Load an existing PDF document from the specified path
PdfDocument existingPdf = PdfDocument.fromFile(Paths.get("assets/LandscapePdf.pdf"));
// Rotate the first page of the document 90 degrees clockwise
existingPdf.rotatePage(PageRotation.CLOCKWISE_90, PageSelection.firstPage());
// Rotate all pages of the document 270 degrees clockwise
existingPdf.rotateAllPages(PageRotation.CLOCKWISE_270);
// Save the modified document to the specified path
existingPdf.saveAs(Paths.get("assets/ExistingPdfRotated.pdf"));
The above code modifies the PDF document that was created in the previous section. It 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
.
Rotated PDF Output
Read more about page orientation in the Code Examples section.
IronPDF for Java
Summary
This article demonstrates how to create a new document with landscape orientation.
IronPDF also offers developers methods to render PDF documents into images and extract text and content from a PDF. Additionally, IronPDF is also capable of rendering charts in PDFs, enhancing security with passwords, and even handling digital signatures programmatically.
IronPDF for Java is free to use, but for deployment purposes, it requires a commercial license which starts from only $749. You can also access the free trial of the full version of IronPDF to test its functionality in production mode.
Frequently Asked Questions
How can I rotate PDF pages in Java?
To rotate PDF pages in Java, you can use IronPDF's Java library. Use the rotatePage
method to rotate individual pages or rotateAllPages
for all pages within a document. These methods allow you to specify rotation angles such as 90 or 270 degrees.
What are the setup requirements for rotating PDFs using Java?
To rotate PDFs using IronPDF in Java, ensure you have JDK, Maven, and the IronPDF library installed. You must also include the IronPDF and Slf4j dependencies in your project's pom.xml
file.
Can IronPDF convert web pages to PDFs in Java?
Yes, IronPDF can convert web pages to PDFs by rendering HTML files into pixel-perfect PDFs, maintaining accurate text and image formatting.
Is there a cost associated with using IronPDF for Java?
IronPDF for Java is free for development purposes. However, a commercial license is required for deployment, with pricing available starting from a basic tier.
How do I change the paper orientation of a PDF in Java?
To change the paper orientation of a PDF in Java using IronPDF, use the ChromePdfRenderOptions
class and set the PaperOrientation
property to either portrait or landscape before rendering the PDF.
Where can I obtain the IronPDF Java library?
You can download the IronPDF Java library from Maven Central or the official IronPDF product website.
Does IronPDF support form fields in PDFs?
Yes, IronPDF supports form fields within PDFs, allowing developers to create and manipulate form components programmatically.
What additional features does IronPDF provide for PDF manipulation?
IronPDF offers a variety of features for PDF manipulation, including rendering PDFs into images, extracting text and content, rendering charts, and enhancing document security with passwords and digital signatures.