Rotate PDF via Java

Change PDF page orientation. Use Aspose.PDF for Java to modify PDF documents programmatically

Rotate PDF Pages with Java

Do you need to rotate PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. With Java libraries like Aspose.PDF – are stand-alone solutions that don’t rely on other software and are ready for commercial use. They cover all possible needs of professional Java developers.

This Java library enables developers to rotate PDF documents quickly and efficiently through programming. This library shows you how easily to update or change the page orientation of pages in an existing PDF file programmatically with Java. It is a standalone solution for processing PDF documents in Java that does not require the installation of any third-party software.

In order to rotate PDF file, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful, and easy-to-use document manipulation API for the Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

How to Rotate PDF documents


You need Aspose.PDF for Java to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Move page upper in order to compensate changing page size.
  3. Set old and new page height.
  4. Setting the page rotation angle.
  5. Save the output PDF file.

If you want to rotate PDF files for your tasks, this solution can help. With this powerful Aspose.PDF Java library, you can update or change the page orientation of pages while keeping its quality. This professional tool uses Java to rotate PDF effectively. The following example demonstrates how to rotate PDF document in Java:

Rotate PDF - Java

This sample code shows how to Rotate PDF Pages - Java

Input file:

File not added

Output format:

PDF

Output file:

    
    public static void RotatePages() {
        // Open document
        Document pdfDocument = new Document(_dataDir + "sample2.pdf");

        for (Page page : pdfDocument.getPages())
        {            
            // Rectangle r = page.getMediaBox();
            // double newHeight = r.getWidth();
            // double newWidth = r.getHeight();
            // double newLLX = r.getLLX();
            // //  We must to move page upper in order to compensate changing page size
            // // (lower edge of the page is 0,0 and information is usually placed from the
            // //  Top of the page. That's why we move lover edge upper on difference between
            // //  Old and new height.
            // double newLLY = r.getLLY() + (r.getHeight() - newHeight);
            // page.setMediaBox (new Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight));
            // // Sometimes we also need to set CropBox (if it was set in original file)
            // page.setCropBox(new Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight));

            // Setting Rotation angle of page
            page.setRotate(Rotation.on90);
        }

        _dataDir = _dataDir + "ChangeOrientation_out.pdf";
        // Save output file
        pdfDocument.save(_dataDir);
    }

About Aspose.PDF for Java API

Aspose.PDF for Java API is a library that enables developers to add PDF processing capabilities to their applications. It can be used to build any type of 32-bit and 64-bit applications to generate or read, convert and manipulate PDF files without the use of Adobe Acrobat. Aspose.PDF for Java allows developers to insert tables, graphs, images, hyperlinks, custom fonts - and more - into PDF documents. Moreover, it is also possible to rotate PDF. Aspose.PDF for Java provides excellent security features to develop secure PDF files.

You can find more information about Aspose.PDF for Java API on documentation and examples on how to use API. Some of the key features of Aspose.PDF for Java API include support for various file formats including HTML, XFA, TXT, PCL, XML, XPS and image file formats, support for various PDF versions, and extensive hyperlink functionality.