获取PDPage / PDDocument的DPI以准确计算PDF尺寸

问题描述 投票:0回答:1

我希望得到一个PDF格式的每个页面的准确大小,作为我将要创建的PDF单元测试的一部分。当我处理在每个文档中具有许多不同页面大小的PDF时,代码返回维度的ArrayList。

AFAIK每个页面也可以有自己的DPI设置。

我已经做了很多谷歌搜索,但我只想出这个只给我部分答案,因为我仍然需要弄清楚DPI每页是什么。

PDFBox - find page dimensions

public static ArrayList<float[]> getDimentions(PDDocument document) {
    ArrayList<float[]> dimensions = new ArrayList<>();
    float[] dim = new float[2];
    //Loop Round Each Page
    //Get Dimensions of each page and DPI
    for (int i = 0; i < document.getNumberOfPages(); i++) {
        PDPage currentPage = document.getPage(i);
        PDRectangle mediaBox = currentPage.getMediaBox();
        float height = mediaBox.getHeight();
        float width = mediaBox.getWidth();
        // How do I get the DPI now????
    }
    //Calculate Size of Page in mm  (
    //Get Dimensions of each page and DPI ( https://stackoverflow.com/questions/20904191/pdfbox-find-page-dimensions/20905166  )
    //Add size of page to list of page sizes
    //Return list of page sizes.
    return dimensions;
}
java pdfbox dpi
1个回答
2
投票

页面尺寸(媒体框/裁剪框)以默认用户空间单位给出,而默认用户空间单位默认为1/72英寸。因此,只需将框宽度或高度除以72即可获得以英寸为单位的页面宽度或高度。

但是,这与DPI值72不对应,因为这将是分辨率值而pdf没有分辨率,默认用户空间单位仅是不同的度量单位。

© www.soinside.com 2019 - 2024. All rights reserved.