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

Sam*_*bit 5 java itext

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

我想按以下方式做 -

if( getRemainingSpace() < pageSize)
//Create new page
else
put contents in the same page
Run Code Online (Sandbox Code Playgroud)

mkl*_*mkl 5

当您使用标签(不是)时,我假设您使用的是 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)
Run Code Online (Sandbox Code Playgroud)

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

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

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