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

痴心易碎 提交于 2019-12-21 14:52:31

问题


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.




回答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
                }
            }
        }


来源:https://stackoverflow.com/questions/38919551/how-to-find-pdf-is-portrait-or-landscape-using-pdfbox-library-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!