2017-02-11 214 views
0

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

我想通过以下方式做的事情 - 当您使用

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

回答

1

标签(未),我假设你正在使用的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英寸)。

的数量显然取决于您想要在这些行上使用的字体和其他参数(字体大小,前导,...)。