获取PDPage / PDDocument的DPI以准确计算PDF尺寸 [英] Getting DPI of PDPage/PDDocument to calculate PDF Dimensions Accurately

查看:315
本文介绍了获取PDPage / PDDocument的DPI以准确计算PDF尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking to get an accurate size of each page in a PDF as part of a Unit test of PDF's I'll be creating. As I'm dealing with PDFs that have many different page sizes in each document the code returns an ArrayList of dimensions.

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

AFAIK each page can have its own DPI setting too.

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

I've done quite a bit of Googling but I've only come up with this which only gives me part of the answer, as I still need to work out what DPI each page is.

PDFBox-查找页面尺寸

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


推荐答案

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

The page dimensions (media box / crop box) are given in default userspace units which in turn default to 1/72 inch. So simply divide the box width or height by 72 to get the page width or height in inch.

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

This does not correspond to a DPI value of 72, though, because that would be a resolution value and a pdf does not have a resolution, the default userspace units merely are a different measurement unit.

这篇关于获取PDPage / PDDocument的DPI以准确计算PDF尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆