Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在iText pdf中知道pdf中的剩余空间_Java_Itext_Itext7 - Fatal编程技术网

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

Java 如何在iText pdf中知道pdf中的剩余空间,java,itext,itext7,Java,Itext,Itext7,我想知道在pdf页面中添加内容后还剩下多少空间。有没有办法让我可以跟踪一些空间,以便我可以在一页中容纳3到4行,或者我可以创建一个新的文档页来放置内容 我想按以下方式做- if( getRemainingSpace() < pageSize) //Create new page else put contents in the same page if(getRemainingSpace()

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

我想按以下方式做-

if( getRemainingSpace() < pageSize)
//Create new page
else
put contents in the same page
if(getRemainingSpace()
当您使用标记(非)时,我假设您使用的是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位置(您可以使用
文档
方法
底部()
)检索该位置)

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


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

如何在
itext7
中实现相同的功能?找到当前垂直位置的等效方法是什么?@Vikas请自行提问。但是,请注意,iText 7有一个更加动态的渲染模型,不一定有全局当前位置,但可能只有多个本地渲染上下文的多个当前位置。如何使用html cde在velocity模板中实现它?这是一个完全不同的问题。因此,请以问题的形式发布。