gpt4 book ai didi

java - 使用 itextpdf,PDF 的页面大小在横向和纵向之间始终相同

转载 作者:行者123 更新时间:2023-11-29 03:21:33 25 4
gpt4 key购买 nike

我有一个 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
}

谢谢!

最佳答案

有两个方便的方法,名为 getPageSize()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() 方法来完成。

备注:这段文字抄 self 的书"The ABC of PDF" (本书正在 build 中;您可以免费下载第一章)。可以找到代码示例 here .

关于java - 使用 itextpdf,PDF 的页面大小在横向和纵向之间始终相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23353146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com