1

我有一个 PDFReader,其中包含一些横向模式和纵向模式的页面。

我需要区分它们以进行一些处理...但是,如果我调用 getOrientation 或 getPageSize,则该值始终相同(页面大小为 595,方向为 0)。

为什么横向页面的值没有不同?

我试图找到其他方法来检索页面宽度/方向,但没有任何效果。

这是我的代码:

for(int i = 0; i < pdfreader.getNumberOfPages(); i++)
{
    document = PdfStamper.getOverContent(i).getPdfDocument();

    document.getPageSize().getWidth; //this will always be the same
}

谢谢 !

4

2 回答 2

4

有两个方便的方法,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(如果缺少条目,则为默认值)90180270

getPageSizeWithRotation()方法考虑了这个值。它交换宽度和高度,以便您了解差异。它还为您提供/Rotate条目的价值。

第 4 页也有横向,但在这种情况下,通过调整/MediaBox条目来模拟旋转。在这种情况下, 的值/MediaBox[0 0 842 595],如果有一个/Rotate条目,它的值是0

这就解释了为什么该方法的输出与该getPageSizeWithRotation()方法的输出相同getPageSize()

当我阅读您的问题时,我看到您正在寻找轮换。这可以通过getRotation()方法来完成。

备注:本文抄自我的《PDF的ABC》一书(本书正在建设中,可以免费下载前几章)。代码示例可以在这里找到。

于 2014-04-29T06:06:43.500 回答
0

使固定 :

利用

PdfStamper.getImportedPage(pdfReader, pagenumber).getBoundingBox().getWidth()

代替

stamper.getOverContent(i).getPdfDocument().getPageSize().getWidth();
于 2014-04-29T00:16:01.540 回答