有两种方便的方法,名为getPageSize()
and 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 行)是一个 A4 页面,就像第 1 页一样,但它是横向的。这/MediaBox
条目与第一页使用的条目相同[0 0 595 842]
,这就是为什么getPageSize()
返回相同的结果。
该页面是横向的,因为\Rotate
页字典中的条目设置为90
。该条目的可能值为0
(如果条目丢失,则这是默认值),90
, 180
and 270
.
The getPageSizeWithRotation()
方法考虑该值。它交换宽度和高度,以便您意识到差异。它还为您提供了价值/Rotate
entry.
第 4 页也具有横向方向,但在这种情况下,通过调整/MediaBox
入口。在这种情况下,值/MediaBox
is [0 0 842 595]
如果有一个/Rotate
条目,其值为0
.
这解释了为什么输出getPageSizeWithRotation()
方法与输出相同getPageSize()
method.
当我读到你的问题时,我发现你正在寻找轮换。这可以通过getRotation()
method.
Remark:这段文字是从我的书中复制的《PDF 基础知识》(本书正在编写中;您可以免费下载前几章)。代码示例可以找到here.