Java PDFBox - 查找页面尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20904191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDFBox - find page dimensions
提问by John Smith
How can I find(in mm) the width and the height of a pdf page using PDFBox? Currently, I'm using this:
如何使用 PDFBox 查找(以毫米为单位)pdf 页面的宽度和高度?目前,我正在使用这个:
System.out.println(page.getMediaBox().getHeight());
System.out.println(page.getMediaBox().getWidth());
but the result is(not in mm):
但结果是(不是毫米):
842.0
595.22
采纳答案by usr2564301
Measurement units inside a PDF are in points, a traditional graphic industry measurement unit. Adobe uses the following definition:
PDF 中的度量单位以点为单位,这是一种传统的图形行业度量单位。Adobe 使用以下定义:
1 pt = 1/72 inch
and since one inch is defined to be exactly 25.4 mm (really!), you can convert from points to mm using the formula
并且由于一英寸被定义为恰好 25.4 毫米(真的!),您可以使用公式从点转换为毫米
mm = pt*25.4 / 72
Your values, by the way, translate (loosely) to the A4 paper dimensions 210 x 297 mm. ("Loosely", for 2 reasons. First: Axdimensions are derived from 1 square meter, in the metric system. Points are based (according to Adobe's usage) in the imperial system; therefore, all conversions between points and millimeters are approximations. Second: the given value in mm for A4 is rounded as well. Axrelative and absolute sizes are based on an irrational number.)
顺便说一下,您的值(松散地)转换为尺寸为 210 x 297 毫米的 A4 纸。(“松散”,有两个原因。第一:在公制系统中,A x尺寸源自 1 平方米。点基于(根据 Adobe 的用法)在英制系统中;因此,点和毫米之间的所有转换都是近似值. 第二:A4 的给定值(以毫米为单位)也四舍五入。A x相对和绝对大小基于无理数。)
Footnote
脚注
Inside an object stream, units of measurement can be scaled to something else. The above is only true for top level base objects.
在对象流中,测量单位可以缩放到其他东西。以上仅适用于顶级基础对象。
回答by nillas
Coordinates in DTP points are defined as: 1 pt = 1/72 inch = 25.4/72 mm
DTP 点中的坐标定义为:1 pt = 1/72 英寸 = 25.4/72 毫米
You could write a method like this:
你可以写一个这样的方法:
public float pt2mm(float pt) {
return pt * 25.4f / 72;
}
回答by raisercostin
If the document is created using a different DPI than 72 then use the more general formula:
如果文档是使用不同于 72 的 DPI 创建的,则使用更通用的公式:
public float pt2mmForWeb72dpi(float pt) {
return pt2mm(pt,72);
}
public float pt2mmForPrint300dpi(float pt) {
return pt2mm(pt,300);
}
public float pt2mmForPrint600dpi(float pt) {
return pt2mm(pt,600);
}
public float pt2mm(float pt, float dpi) {
return pt * 25.4f / dpi;
}
You can find more at https://forums.indigorose.com/forum/indigo-rose-software/developer-s-den/13282-what-is-the-size-of-a4-in-px
您可以在https://forums.indigorose.com/forum/indigo-rose-software/developer-s-den/13282-what-is-the-size-of-a4-in-px找到更多信息
A4 is a document format, as a screen image that's going to depend on the image resolution, for example an A4 document resized to:
- 72 dpi (web) = 595 X 842 pixels
- 300 dpi (print) = 2480 X 3508 pixels (This is "A4" as I know it, i.e. "210mm X 297mm @ 300 dpi")
- 600 dpi (print) = 4960 X 7016 pixels
And so forth. FWIW document formats like A4 are described by their print dimensions (millimeters), which is a whole different thing than screen images (pixels) so that's why you don't see anyone using pixels to describe A4. :yes
A4 是一种文档格式,作为将取决于图像分辨率的屏幕图像,例如 A4 文档调整为:
- 72 dpi (web) = 595 X 842 像素
- 300 dpi(打印)= 2480 X 3508 像素(这是我所知的“A4”,即“210mm X 297mm @ 300 dpi”)
- 600 dpi(打印)= 4960 X 7016 像素
等等。像 A4 这样的 FWIW 文档格式是通过它们的打印尺寸(毫米)来描述的,这与屏幕图像(像素)完全不同,所以你看不到任何人使用像素来描述 A4。:是的