Rotate PDF Pages programmatically

Change Page Orientation

This article describes how to update or change the page orientation of pages in an existing PDF file.

Aspose.PDF for PHP via Java has feature to change the page orientation from landscape to portrait and vice versa.

  1. Open the document using the specified input file.
  2. Get all the pages of the document.
  3. Iterate through each page and set the rotation to 90 degrees.
  4. Save the modified document to the specified output file.
  5. Close the document.

    // Open document
    $document = new Document($inputFile);                
    $pages = $document->getPages();
    $pagesSize = java_values($pages->size());
       
    // Loop through all the pages
    for ($pageCount = 1; $pageCount <= $pagesSize; $pageCount++) {
        $page = $pages->get_Item($pageCount);
       
        $page->setRotate((new Rotation())->On90);
    }

    // Save output document
    $document->save($outputFile);
    $document->close();