Detect PDF Page Orientation Using Java

Import JAR Dependency

Method 1: Download the free library and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.


Sample Code

Free Spire.PDF for Java supports detecting the orientation of a PDF page by comparing the value of page width and page height. The complete sample code is shown below.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

public class DetectPageOrientation {
public static void main(String[] args){
//Load PDF file
PdfDocument pdf = new PdfDocument();

//Get the first page
PdfPageBase page = pdf.getPages().get(0);

//Compare the value of page width and height
if (page.getSize().getWidth()> page.getSize().getHeight()){
System.out.println("The page orientation is Landscape");
System.out.println("The page orientation is Portrait");



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store