Saturday, 12 October 2019 09:41

Detect PDF Page Orientation in Java

Written by  support iceblue
Rate this item
(0 votes)

Spire.PDF for Java supports detecting the orientation of a PDF page by comparing the value of page width and page height. This article shows how to use Spire.PDF for Java to accomplish this function.

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();
        pdf.loadFromFile("Fields.pdf");

        //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");
        }
        else{
            System.out.println("The page orientation is Portrait");
        }
    }
}

Output

Detect PDF Page Orientation in Java

Additional Info

  • tutorial_title:
More in this category: « Rotate a PDF page in Java