Sambit
Sambit

Reputation: 8011

How to know the remaining space in pdf in iText pdf

I want to know that how many spaces are left after adding contents in pdf page. Is there any way so that I can track that some space is there so that I can accommodate 3 to 4 lines in one page or else I can create a new document page to put the contents.

I want to do in the following manner -

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

Upvotes: 5

Views: 3655

Answers (1)

mkl
mkl

Reputation: 95918

As you use the tag (not ), I assume you are using iText 5.5.x.

The PdfWriter class provides a method which returns the current vertical position:

/**
 * 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)

Thus, to retrieve the vertical extent of the remaining main content space on the current page, you merely have to retrieve this vertical position value and subtract the y position of the bottom page margin (which you can retrieve using the Document method bottom()).

The value you get is in default user space units (which in turn defaults to 1/72 inch).

The number of lines fitting into this space obviously depend on the fonts and other parameters (font size, leading, ...) you want to use on those lines.

Upvotes: 5

Related Questions