How to find pdf is portrait or landscape using PDF

2019-04-13 05:00发布

问题:

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 ?

回答1:

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.



标签: java pdf pdfbox