0

i am using iText 5.1.0 for creating the PDF in java . I have 3 blocks of tables , whose sizes may change dynamically

Table 1

.

.

. 

Table 2 

.

.

.

.

End of page 

Now i am in need of the method that would compute the space below the table 2 . And i am using the pagesize as A4

Intially it printed like this ,

But when the data in the previous tables increased it printed , like the 2 nd one. So i would like to calculate the space below if possible before i insert the table

Any suggestions are highly appreciated.

Thank you

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • Do I understand your drawing correctly that below table 2 the page is empty? – mkl May 13 '14 at 07:22
  • Yes . But the thing is I need to insert table2 only if i could insert its headers with data . consider scenario that i could print the headers alone as it is the last row so that i would look like a empty table . how can i handle this? – Santhosh May 13 '14 at 08:50
  • @mkl And to add your question in the comment . I need to get the number of empty lines below the `table2` . Is it possible ? – Santhosh May 13 '14 at 09:02
  • 1
    How do you insert the tables currently? Are you using `PdfPTable`? Concerning *But the thing is I need to insert table2 only if i could insert its headers with data* - does iText layouting not do this automatically? Concerning *I need to get the number of empty lines below the table2 . Is it possible ?* - when do you attempt this? Right after adding table2 to the document? Perhaps you should post your current code to allow others to constructively answer you. – mkl May 13 '14 at 09:47
  • Yes , i use `PdfPTable` ,And _Right after adding table2 to the document?_ Yes. i need to calculate the space below the table2 after inserting it – Santhosh May 13 '14 at 09:50
  • 1
    Ok, you can retrieve the current y position after adding that table (I still have to look up how exactly) and from that calculate the remaining free space. The number of lines this this corresponds to, though, depends on the contents of those lines. Which font sizes are used? Which line-to-line distance is used? Etc. pp. – mkl May 13 '14 at 10:12
  • I understand what you say . Firstly i need to calculate the number of remaining lines . And i use Times New Roman , sizes and spacing will be default ones . So can you please explain how to calculate the remaining space using current y position – Santhosh May 13 '14 at 10:17
  • Ok, @Bruno meanwhile has posted an answer naming the method to *retrieve the current y position* with. *And i use Times New Roman , sizes and spacing will be default ones* - I don't know those default values, and neither do I know the exact algorithms iText uses while doing layouts. As an estimate I'd simply subtract the bottom y coordinate of the layout region from that current y coordinate and divide this by the font size. The bottom of the layout region should be the bottom of the media box (most often 0) plus the bottom margin. – mkl May 13 '14 at 11:52
  • @mkl Can you please what is the total space available for the `A4 page` inclusive of footer (i.e) initially consider i have a footer of `1F`. – Santhosh May 14 '14 at 07:17
  • *Can you please what is the total space available for the A4 page inclusive of footer* - A4 is defined to be approximately 210mm × 297mm. The starting PDF user space unit (which is the unit iText uses) by default is 1/72 in; in squared user space units, therefore, the space is 595 × 842. – mkl May 14 '14 at 07:35
  • Thank you @mkl you made it clear . Do you have any snippets for adding the headers and footers using itext – Santhosh May 14 '14 at 07:36
  • That has been answered *very* often on stackoverflow. E.g. look at [this answer](http://stackoverflow.com/a/17217346/1729265). – mkl May 14 '14 at 07:39

1 Answers1

2

There's a method in PdfWriter called getVerticalPosition(). It takes a Boolean parameter that tells iText to ensure a new line or not. If you don't ensure a new line (false), you get the current Y, but that may not be the position you need. If you ensure a new line (true), a new line is added and you get the position where the new line starts.

As you are creating A4 pages from start, and as the Y axis points upwards, you need to subtract the bottom margin from this vertical position to find out how much space is left for your new table.

Note that we aren't happy with the getVerticalPosition() method at iText: having a getter that changes the content of the document isn't very elegant. In cases like yours, we prefer working with ColumnText to take charge over the layout process intstead of letting iText decide where to split a table.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Can you post an example from your collection ? – Santhosh May 13 '14 at 10:56
  • The `getVerticalPosition()` method is straight-forward, so I assume your comment isn't about such an example. Using `ColumnText` is a tad more difficult. I Googled for an example and I found one on StackOverflow: http://stackoverflow.com/questions/15387642/itext-strange-column-page-changing-behaviour-with-columntext – Bruno Lowagie May 13 '14 at 11:51
  • And i am Referring your book iText in Action . Its highly helpful :) And thanks for your answer too – Santhosh May 13 '14 at 12:05
  • From your example `cb.lineTo(PageSize.A4.width(), pos);` . I couldnt any method named `width()` ? Any suggestions ? – Santhosh May 13 '14 at 12:11
  • I don't understand your question. Which example are you talking about? `PageSize.A4` is a `Rectangle`. The `Rectangle` class has a `getWidth()` method. If you are using an example where a `width()` method is used, you're referring to very old documentation. – Bruno Lowagie May 13 '14 at 12:19
  • Can you please what is the total space available for the `A4 page` inclusive of footer (i.e) initially consider i have a footer of `1F` – Santhosh May 14 '14 at 07:16
  • Suppose that you have a `document` object, then the Y position of the bottom will be: `document.bottom()`. If you want to take into account a bottom margin, you can use `document.bottom(margin)` where `margin` is the height of the bottom margin in user units. The total space available would then be `writer.getVerticalPosition(true) - document.bottom(margin)`. – Bruno Lowagie May 14 '14 at 07:26