Some scanned PDF documents may contain pages displayed in the wrong orientation (e.g., upside-down), which could cause great inconvenience while reading. Rotating pages can help you solve this problem and provide readers with a better reading experience. This article will introduce how to rotate pages in PDF in Java using Spire.PDF for Java.
Install Spire.PDF for Java
First, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>10.10.7</version> </dependency> </dependencies>
Rotate a Specific Page in PDF in Java
Rotation is based on 90-degree increments. You can rotate a PDF page by 0/90/180/270 degrees. The following are the steps to rotate a PDF page:
- Create an instance of PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the desired page by its index (zero-based) using PdfDocument.getPages().get(pageIndex) method.
- Get the original rotation angle of the page using PdfPageBase.getRotation().getValue() method.
- Increase the original rotation angle by desired degrees.
- Apply the new rotation angle to the page using PdfPageBase.setRotation() method.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.PdfPageRotateAngle; public class RotatePdfPage { public static void main(String []args){ //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a PDF document pdf.loadFromFile("Sample.pdf"); //Get the first page PdfPageBase page = pdf.getPages().get(0); //Get the original rotation angle of the page int rotation = page.getRotation().getValue(); //Rotate the page 180 degrees clockwise based on the original rotation angle rotation += PdfPageRotateAngle.Rotate_Angle_180.getValue(); page.setRotation(PdfPageRotateAngle.fromValue(rotation)); //Save the result document pdf.saveToFile("Rotate.pdf"); } }
Rotate All Pages in PDF in Java
The following are the steps to rotate all pages in a PDF document:
- Create an instance of PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Loop through the pages in the document.
- Get the original rotation angle of each page using PdfPageBase.getRotation().getValue() method.
- Increase the original rotation angle by desired degrees.
- Apply the new rotation angle to the page using PdfPageBase.setRotation() method.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.PdfPageRotateAngle; public class RotateAllPdfPages { public static void main(String []args){ //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a PDF document pdf.loadFromFile("Sample.pdf"); //Loop through each page in the document for(PdfPageBase page :(Iterable) pdf.getPages()) { //Get the original rotation angle of the page int rotation = page.getRotation().getValue(); //Rotate the page 180 degrees clockwise based on the original rotation angle rotation += PdfPageRotateAngle.Rotate_Angle_180.getValue(); page.setRotation(PdfPageRotateAngle.fromValue(rotation)); } //Save the result document pdf.saveToFile("RotateAll.pdf"); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.