user6709592
user6709592

Reputation:

How to find pdf is portrait or landscape using PDFBOX Library in Java

I am doing project in Java using PDFBOX-1.8.6 library (its compulsory to use). My Question is

  1. How can I check input pdf file have portrait or landscape orientation ?
  2. How to check/scan portrait or landscape orientation in PDF by its dimensions of each page if both are same? For example, both are in standard A4 size. You will be more clear by below picture. my Landscape - Portrait problem I just want to check its content is rotated or not. So How can I cope up with above problem ?

Upvotes: 8

Views: 9275

Answers (2)

Tilman Hausherr
Tilman Hausherr

Reputation: 18851

Assuming that you have a PDPage object:

PDRectangle mediaBox = page.findMediaBox();
boolean isLandscape = mediaBox.getWidth() > mediaBox.getHeight();

however... the page could be rotated:

int rotation = page.findRotation();
if (rotation == 90 || rotation == 270)
    isLandscape = !isLandscape;

This is for 1.8.* only. In the 2.* versions, use getMediaBox() and getRotation(). Don't use the get* methods in the 1.8.* versions because they don't look up the page tree if the info is missing at the page level.

Upvotes: 18

sagar vyas
sagar vyas

Reputation: 131

This will help you

if(document !=null){
            int pageCount = document.getNumberOfPages();
            for(int i = 0; i <pageCount ; i++){

                PDRectangle pageSize=document.getPage(i).getMediaBox();


                int degree=document.getPage(i).getRotation();

                 if(( pageSize.getWidth() > pageSize.getHeight()) ||(degree==90)||(degree==270)){

                       document.close();
                    return true; //document is landscape
                }
            }
        }

Upvotes: 2

Related Questions