2

I add a bunch of stuff to a page, such as document.add(p);, then I add a table that fills two columns using the documentation here. In this process, I use:

column.setSimpleColumn(
   x[count][0], document.bottom(),
   x[count][1], document.top() - height - 10);

where 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 (e.g. document.add(p);). 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 simply 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)?

1 Answer 1

4

There are three ways to add content to a PDF:

Using document.add();

In this case, iText decides where the content is added, based on the page size and the margins you defined.

There is a very old method called getVerticalPosition() that takes a boolean as parameter. I'm not proud of that method as it is a getter method that not only will give you the Y position on the page after using a series of document.add() operations, it can also change the current Y position (and that is not what a getter method should do).

If you pass false as a parameter, it will give you the current Y, which could be the position of the baseline of the last Chunk you've added.

If you pass true as a parameter, the method will first add a newline and give you the position of the baseline of the "next" line of text you'll be adding.

Using ColumnText

ColumnText is an object to which you can add Elements. You can then define a column using absolute coordinates and invoke the go() method. The go() method will render the content and you can use the return value to see if the content fitted the rectangle. You can use the getYLine() method to find the Y coordinate of the last piece of content that was added.

Using writeSelectedRows()

This method will only work when you work with PdfPTable objects. The writeSelectedRows() method can add a selection of rows and columns to a page starting at a specific x and y coordinate. It returns a float value that gives you the Y-position of the bottom border of the final row that was added. If you want to know how much space the table needs vertically before adding the table with writeSelectedRows(), you need to calculate the height of the rows you want to add first.

7
  • Thanks @BrunoLowagie, this works great and answers the question. But, interestingly, if I add a second table (of two columns), below the first table (of two columns), it always places this second table at the top of the page (on top of the first table). I think that's because the first table is positioned absolutely (?), and thus there's no way to place subsequent items relatively. Are there any known ways to address this? For example, can I compute height of the first table somehow (the portion on the last page only)? Or other technique?
    – user46688
    Oct 31, 2015 at 0:15
  • 1
    If you use writeSelectedRows, then the tables will be added on top of each other. If you use document.add(table) for both tables, they will follow each other. Note that you can calculate the height of a table if you set the total height. That's useful because once you start adding things at absolute positions, you have to keep doing so. Oct 31, 2015 at 0:23
  • I was able to align the second table below the first by using a column.getYLine() to get the end of the first table and use it to set the placement of the second table.
    – user46688
    Oct 31, 2015 at 0:29
  • I used column.addElement(table); to insert the first table. Not sure if that effects your recommendation in your comment above.
    – user46688
    Oct 31, 2015 at 0:31
  • 2
    Using getYLine() is perfectly OK! There are 3 ways to add a table: document.add(table); lets iText decide; table.writeSelectedRows(...); allows you to render the table at absolute coordinates; column.addElement(table) allows you to fit the table inside a rectangle you define. My answer (about getVerticalPosition()) is only valid if you use document.add(). I'll update it to include something about ColumnText. Oct 31, 2015 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.