DEV Community

Alexis
Alexis

Posted on

Java: Rotate PDF Pages

PDF is a widely used format for sharing and printing documents. It’s common that large PDF files contain pages that are rotated at an undesirable angle, especially those converted from formats like JPG, PNG, and scanned PDF. This article will introduce how to rotate PDF pages from the following three parts with the help of Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you need 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 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>9.1.4</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Rotating an existing PDF page in Java:

Rotation is based on 90-degree increments. You can rotate a PDF page by 0/90/180/270 degrees. You can rotate a particular PDF page in the PDF document using PdfPageRotateAngle Enum as shown below:

  • 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.
import com.spire.pdf.*;

public class rotatePDF {
    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("RotatePage.pdf");
    }
}
Enter fullscreen mode Exit fullscreen mode

Rotate PDF page

Rotating an Image in a PDF document

You might also want to add image to a PDF document by rotating it to make the PDF vivid. Here you will learn how to rotate an Image in a PDF document using Java.

  • Create an instance of PdfDocument class.
  • Add a section and page to the PDF.
  • Load the image from disk using PdfImage.fromFile() method and get its width and height.
  • Set the rotation for the image by page.getCanvas().rotateTransform(45) method.
  • Draw the image to the PDF page by using page.getCanvas().drawImage() method.
  • Save the result document using PdfDocument.saveToFile() method.
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;

public class rotateImage {
    public static void main(String[] args) {
        //Create a pdf document
        PdfDocument doc = new PdfDocument();

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Add a section
        PdfSection section = pdf.getSections().add();
        //Add a new page 
        PdfPageBase page = section.getPages().add();

        //Load the image
        PdfImage image = PdfImage.fromFile("Icon.jpg");
        //Get the image's width and height
        double imageWidth = image.getPhysicalDimension().getWidth();
        double imageHeight = image.getPhysicalDimension().getHeight();

        //Set the rotation for the image
        page.getCanvas().rotateTransform(45);

        //Draw image to the page
        page.getCanvas().drawImage(image, 100, 10, imageWidth, imageHeight);
        page.getCanvas().save();

        //Save the result document
        pdf.saveToFile("RotateImage.pdf");
        pdf.close();
    }
Enter fullscreen mode Exit fullscreen mode

Rotate Image on PDF

Rotating Text on PDF Pages in Java

Adding text with a rotation angle can be an important requirement in particular scenarios of text generation in PDF. In order to add text with rotation in PDF document, you can use the section.getPageSettings().setRotate() method to specify the rotation angle of the text when you add text to the PDF.

  • Create an instance of PdfDocument class.
  • Add a section to the PDF.
  • Rotate the section by using section.getPageSettings().setRotate() method.
  • Add a new page to the PDF section using section.getPages().add() method.
  • Draw text to the PDF page using page.getCanvas().drawString() method and set the font and format for the text.
  • Save the result document using PdfDocument.saveToFile() method.
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;

public class rotateText{
    public static void main(String[] args) {

        //Create a pdf document
        PdfDocument doc = new PdfDocument();

        //Create PdfSection
        PdfSection section = doc.getSections().add();

        //Set rotating angle        
        section.getPageSettings().setRotate(PdfPageRotateAngle.Rotate_Angle_90);

        //Add the page
        PdfPageBase page = section.getPages().add();

        //Define a PdfBrush
        PdfBrush brush = PdfBrushes.getBlack();

        //Define a font
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 13), true);

        //Set the position for drawing
        float x = 0;
        float y = 50;

        //Text string
        String text = "Rotate page when creating a PDF document.";

        //Draw text string on page canvas
        page.getCanvas().drawString(text, font, brush, x, y);

        String result = "rotateNewPDF.pdf";

        //Save the document
        doc.saveToFile(result);
        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Rotate Text on PDF

Conclusion:

You have learned how to rotate PDF pages, text, and images programmatically in Java when generating a new PDF file or updating the existing PDF document. You can check the PDF forum for more features to operate the PDF files.

Top comments (0)