Skip to main content
Skip table of contents

How to find out the current cursor position on a page?

I'd like to execute a command to tell me how much height has been consumed thus far in the document (for the current page) so I can set height appropriately.

I add a bunch of stuff to a page, using a sequence of document.add() statements. Then I add a table that fills two columns at absolute positoins using the ColumnText object:

 

column.setSimpleColumn(
   x[count][0], document.bottom(),
   x[count][1], document.top() - height - 10);
In this snippet height is set to 0. This places the table in a column that starts at the top of the document. Of course, what I'd like to do is to add the column directly below the last thing written to the document in the final document.add() statement. If I knew the height consumed thus far on the page, I could manually enter the correct height value and things work fine.

The problem is, what gets written in the document depends on a lot of conditions, so I'd like to execute a command to tell me how much height has been consumed thus far in the document (for the current page) so I can set height appropriately. Is there a way to do this in iText so that I don't need to keep track of the heights of each individual element added to the page?

Posted on StackOverflow on Oct 30, 2015 by user46688

In iText 7 we add elements to the page using doc.add(). After creating an element we can receive the occupied area and X and Y coordinates of it, by next way:

Paragraph p = new Paragraph("Hello World");
doc.add(p);
IRenderer pRenderer = p.createRendererSubTree().setParent(doc.getRenderer());
LayoutResult pLayoutResult = pRenderer.layout(new LayoutContext(new LayoutArea(0, new Rectangle(595-72, 842-72))));

float y = pLayoutResult.getOccupiedArea().getBBox().getY();
float x = pLayoutResult.getOccupiedArea().getBBox().getX();

Click How to find out the current cursor position on a page? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.