Portrait & Landscape Orientation

IronPDF for Java can modify the page orientation of new and existing PDF documents.

New PDF documents rendered anew with IronPDF use portrait orientation by default. Developers can override this behavior when converting content (HTML, RTFs, URLs, etc.) into PDFs with a ChromePdfRenderOptions instance. The setPaperOrientation method accepts a PaperOrientation value and allows developers to alter the paper orientation of the resulting PDF as desired. Lines 21 - 23 of the featured code example create a PDF document set in landscape orientation. The call to setPaperOrientation with PaperOrientation.LANDSCAPE on line 21 triggers the orientation behavior. Substituting the enum value in favor of PaperOrientation.PORTRAIT would make the subsequent call to PdfDocument.renderUrlAsPdf create the IronPDF homepage in portrait orientation.

ChromePdfRenderOptions objects cannot be used to change the page orientation for existing PDFs (these can be PdfDocuments produced from previous calls to any of the PDF rendering methods or that have been loaded into IronPDF using the PdfDocument.fromFile method). For these PDF documents, page orientation can be adjusted with rotation-based transformations. Towards this end, IronPDF makes the rotateAllPages method available for use.

rotateAllPages accepts a PageRotation enum type, which specifies a set of accepted rotation values. Line 40 of the featured code example rotates every page in the working PDF document clockwise by 270 degrees. To rotate only one page (or a subset of pages) in a PDF, opt for the rotatePage method in lieu of rotateAllPages.

existingPdf.rotatePage(PageRotation.CLOCKWISE_270, PageSelection.firstPage());  
existingPdf.rotatePage(PageRotation.CLOCKWISE_180, PageSelection.lastPage());  
existingPdf.rotatePage(PageRotation.CLOCKWISE_90, PageSelection.singlePage(8)); // Rotate page 9  
existingPdf.rotatePage(PageRotation.CLOCKWISE_270, PageSelection.pageRange(9, 14)); // Rotate pages 10 - 15
JAVA