8

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 ?
3
  • Where is the code you've already tried? Adding code you've already tried will help. SO Is not a "I need code: give me it" service... See stackoverflow.com/help/how-to-ask for tips on asking a good question. Back to Google you go! Aug 12, 2016 at 15:39
  • @GeoffJames Actually I am newbie for PDFBox, I don't know what are the inbuilt methods, Params or anything regarding it. So I need some reference to cope up with my problem. I refered some documentation of PDFBox, but didn't understood as per my requirement. One more thing, I never said that "I need code : give me it." - I just need some reference methods to achieve my Task, Ok.
    – user6709592
    Aug 16, 2016 at 5:17
  • @GeoffJames are you from old Linux boards? Your comment reminds me of the old RTFM days... The guy is new and the question is pretty straight-forward, we don't need code to give him the proper answer, I think.
    – DGoiko
    Feb 9 at 12:04

2 Answers 2

18

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.

2
  • 1
    Thank you for saving a life especially with findRotation.
    – Gentleman
    Apr 26, 2019 at 14:17
  • 1
    THANK YOU! Your if statement is very important when calculating the positions of Glyphs in the document: int rotation = page.findRotation(); boolean bReversePageWidthAndHeight = (rotation == 90 || rotation == 270);
    – mike
    May 5, 2019 at 23:45
2

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
                }
            }
        }
1
  • 2
    Your code has false positives - if width>height and (degree=90 or degree=270), it's not landscape.
    – mkl
    Nov 7, 2019 at 11:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.