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

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:


   x[count][0], document.bottom(),
   x[count][1], - 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");
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 this link if you want to see how to answer this question in iText 5.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now