2017-02-11 213 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英寸)。

的數量顯然取決於您想要在這些行上使用的字體和其他參數(字體大小,前導,...)。