gpt4 book ai didi

java - 如何在iText pdf中知道pdf的剩余空间

转载 作者:行者123 更新时间:2023-11-30 02:39:26 25 4
gpt4 key购买 nike

我想知道pdf页面添加内容后还剩下多少个空格。有什么方法可以让我跟踪那里有一些空间,以便我可以在一页中容纳 3 到 4 行,或者我可以创建一个新的文档页面来放置内容。

我想按以下方式进行 -

if( getRemainingSpace() < pageSize)
//Create new page
else
put contents in the same page

最佳答案

当您使用时标签(不是 ),我假设您使用的是 iText 5.5.x。

PdfWriter 类提供了一个返回当前垂直位置的方法:

/**
* Use this method to get the current vertical page position.
* @param ensureNewLine Tells whether a new line shall be enforced. This may cause side effects
* for elements that do not terminate the lines they've started because those lines will get
* terminated.
* @return The current vertical page position.
*/
public float getVerticalPosition(final boolean ensureNewLine)

因此,要检索当前页面上剩余主要内容空间的垂直范围,您只需检索此垂直位置值并减去底部页边距的 y 位置(您可以使用 文档方法bottom())。

您获得的值采用默认用户空间单位(默认为 1/72 英寸)。

适合此空间的行数显然取决于您要在这些行上使用的字体和其他参数(字体大小、行距等)。

关于java - 如何在iText pdf中知道pdf的剩余空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42173874/

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