How to Rotate PDF

Learn how easily rotate PDF documents with high quality using Python PDF library

How to rotate PDF with Python

When dealing with PDF files programmatically, there might be a need to rotate the pages within a PDF. This task allows for altering the page orientation, converting from portrait to landscape or vice versa. This article presents the simplest approach to rotating PDF files using Python. A comprehensive step-by-step guide and code sample will be provided to demonstrate how to effortlessly switch the orientation of pages in a PDF file. With this solution, manipulating the page layout in a PDF becomes seamless and efficient.

Rotate PDF using Aspose.PDF for Python via .NET

For PDF file rotation, we’ll leverage the capabilities of Aspose.PDF for Python via .NET — a robust library designed for seamless PDF creation, processing, and conversion. To install this library from PyPI, you can use the following pip command.

pip install aspose-pdf

Rotate PDF files

Rotate PDF

In Python, rotating the pages in a PDF to switch their orientation between landscape and portrait is a straightforward process. By swapping the values of the page’s height and width and setting the rotation angle, you can achieve the desired orientation change automatically.

Below are the steps to rotate a PDF file in Python:

  • Import the Aspose.PDF library as ‘ap’.
  • Load the input PDF file named “input.pdf” using the ‘Document’ class and store it in the ‘doc’ variable.
  • Iterate through each page in the document using a ‘for’ loop.
  • Calculate the new height and width of the page to transform it into the desired orientation. For landscape to portrait, swap the width and height values, and vice versa.
  • Adjust the lower-left corner (LLX and LLY) coordinates of the page to ensure the content remains correctly positioned after rotation.
  • Set the rotation angle for the page’s content using the ‘rotate’ property with ‘ap.Rotation.ON90’ to rotate by 90 degrees.
  • Save the rotated PDF file with the ‘save’ method, specifying the output file name as “rotated.pdf”.

Use following code snippet for this:

    import aspose.pdf as ap 
    #Load the PDF 
    doc = ap.Document("input.pdf") 
    for page in doc.pages: 
      #Transform each page's orientation from landscape to portrait or vice versa 
      r = page.media_box 
      newHeight = r.width 
      newWidth = r.height 
      newLLX = r.llx 
      newLLY = r.lly + (r.height - newHeight) 
      page.media_box = ap.Rectangle(newLLX, newLLY, newLLX + newWidth,newLLY + newHeight, True) 
      page.crop_box = ap.Rectangle(newLLX, newLLY, newLLX + newWidth,newLLY + newHeight, True) 
      #Set rotation angle for content 
      page.rotate = ap.Rotation.ON90 
    #Save rotated PDF file 
    doc.save("rotated.pdf")

The provided code sample rotates all pages in the PDF file, but it can be adapted to rotate a specific page or a selection of pages. Moreover, the rotation angle can be customized using different values available in the ‘aspose.pdf.Rotation’ enum, such as ON180, ON270, and NONE, in addition to the ON90 used in the code snippet.

Try to rotate PDF files online

Access our free online tool – Rotate PDF, – developed with the Aspose.PDF library, to effortlessly rotate PDF files. With this user-friendly tool, you can swiftly adjust the orientation of your PDF without any hassle. Whether you need to switch between portrait and landscape modes or make other rotation adjustments, this tool simplifies the process. Explore the convenience of rotating PDF files online and ensure your documents are displayed in the desired layout. Experience the efficiency and versatility of our tool today for seamless PDF rotation without any complexities.

Documentation Aspose.PDF for Python Library

Dive into the rich features of the Python PDF library with our comprehensive Documentation. If you have any questions or need assistance, don’t hesitate to join the Aspose forum and post your inquiries there.

Conclusion

Throughout this article, you have acquired the know-how to rotate PDF files in Python. The comprehensive step-by-step guide and code sample have effectively showcased the process of changing the orientation of PDF pages between portrait and landscape modes. Installing the required library and implementing the PDF rotation feature in your application is a straightforward process, enabling you to effortlessly enhance your PDF documents. With this newfound knowledge, you can seamlessly rotate PDF and tailor their orientation to meet your specific requirements with ease. If you want more details about Aspose.PDF libraries - visit our landings pages