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



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

我想通过以下方式做——

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

当你使用itext标签(不是itext7(时,我假设你使用的是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 位置(可以使用 Document 方法检索 bottom() (。

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

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

最新更新