Getting DPI of PDPage/PDDocument to calculate PDF Dimensions Accurately

╄→гoц情女王★ 提交于 2019-12-13 04:28:24

问题


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 each page can have its own DPI setting too.

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

回答1:


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.

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.



来源:https://stackoverflow.com/questions/55596092/getting-dpi-of-pdpage-pddocument-to-calculate-pdf-dimensions-accurately

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