简体   繁体   English

如何使用 Java 中的 PDFBOX 库查找 pdf 是纵向还是横向

[英]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).我正在使用PDFBOX-1.8.6库(必须使用)在Java 中进行项目。 My Question is我的问题是

  1. How can I check input pdf file have portrait or landscape orientation ?如何检查输入的 pdf 文件是纵向还是横向?
  2. How to check/scan portrait or landscape orientation in PDF by its dimensions of each page if both are same?如果两者相同,如何通过每页的尺寸检查/扫描 PDF 中的纵向或横向? For example, both are in standard A4 size.例如,两者都是标准 A4 尺寸。 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 ?那么我该如何应对上述问题呢?

Assuming that you have a PDPage object:假设您有一个 PDPage 对象:

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.这仅适用于 1.8.*。 In the 2.* versions, use getMediaBox() and getRotation() .在 2.* 版本中,使用getMediaBox()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.不要在 1.8.* 版本中使用get*方法,因为如果在页面级别缺少信息,它们不会查找页面树。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM