有两个方便的方法,namedgetPageSize()
和getPageSizeWithRotation()
。
我们来看一个例子:
PdfReader reader =
new PdfReader("src/main/resources/pages.pdf");
show(reader.getPageSize(1));
show(reader.getPageSize(3));
show(reader.getPageSizeWithRotation(3));
show(reader.getPageSize(4));
show(reader.getPageSizeWithRotation(4));
在此示例中,该show()
方法如下所示:
public static void show(Rectangle rect) {
System.out.print("llx: ");
System.out.print(rect.getLeft());
System.out.print(", lly: ");
System.out.print(rect.getBottom());
System.out.print(", urx: ");
System.out.print(rect.getRight());
System.out.print(", lly: ");
System.out.print(rect.getTop());
System.out.print(", rotation: ");
System.out.println(rect.getRotation());
}
这是输出:
llx: 0.0, lly: 0.0, urx: 595.0, lly: 842.0, rotation: 0
llx: 0.0, lly: 0.0, urx: 595.0, lly: 842.0, rotation: 0
llx: 0.0, lly: 0.0, urx: 842.0, lly: 595.0, rotation: 90
llx: 0.0, lly: 0.0, urx: 842.0, lly: 595.0, rotation: 0
llx: 0.0, lly: 0.0, urx: 842.0, lly: 595.0, rotation: 0
第 3 页(参见代码示例 3.8 中的第 4 行)与第 1 页一样是 A4 页面,但它是横向的。该/MediaBox
条目与用于第一页的条目相同[0 0 595 842]
,这就是getPageSize()
返回相同结果的原因。
该页面是横向的,因为\Rotate
页面字典中的条目设置为90
。此条目的可能值为0
(如果缺少条目,则为默认值)90
、180
和270
。
该getPageSizeWithRotation()
方法考虑了这个值。它交换宽度和高度,以便您了解差异。它还为您提供/Rotate
条目的价值。
第 4 页也有横向,但在这种情况下,通过调整/MediaBox
条目来模拟旋转。在这种情况下, 的值/MediaBox
是[0 0 842 595]
,如果有一个/Rotate
条目,它的值是0
。
这就解释了为什么该方法的输出与该getPageSizeWithRotation()
方法的输出相同getPageSize()
。
当我阅读您的问题时,我看到您正在寻找轮换。这可以通过getRotation()
方法来完成。
备注:本文抄自我的《PDF的ABC》一书(本书正在建设中,可以免费下载前几章)。代码示例可以在这里找到。