0

I have an example below that generates a table with ten rows, the last row taking up two lines. The document height is two points smaller than it needs to be to correctly fit the entire table. As expected the last row on the first page contains the word "Two" and the "Lines" portion continues on another page. The generated PDF, however, extends the last row to fit the entire document, excluding page margin padding, instead of using the height of the text in the last row to determine how high it should be. Note that I use table.setExtendLastRow(false, false) to guarantee that the table shouldn't be extended, and yet the last row is getting extended anyway. Any ideas how to stop the last row from being extended?

FileOutputStream out = new FileOutputStream(new File("table.pdf"));
Document document = new Document(new Rectangle(612, 242));
PdfWriter.getInstance(document, out);

document.open();

PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
table.setSplitLate(false);
table.setSplitRows(true);
table.setExtendLastRow(false, false);

for (int i = 0; i < 10; i++) {
    PdfPCell cell;
    if (i == 9) {
        cell = new PdfPCell(new Phrase("Two\nLines"));
    }
    else {
        cell = new PdfPCell(new Phrase(Integer.toString(i)));
    }
    table.addCell(cell);
}

document.add(table);
document.close();

I've uploaded a picture of what I'm seeing. Notice the spacing under the word "Two" is different than the spacing under all the other numbers in the rows. It appears to extend to the bottom of the page margin.

Image of the last row on the first page not being drawn at the base of the font

Michael Hogenson
  • 1,292
  • 1
  • 15
  • 31
  • Does this occur with the latest version of iText? It looks like a problem that was solved a while ago. – Bruno Lowagie Jul 08 '14 at 16:17
  • Yes, as far as I can tell. I'm using version 5.5.1. – Michael Hogenson Jul 08 '14 at 16:26
  • OK, then I'll have to make a standalone example to test what is going on. – Bruno Lowagie Jul 08 '14 at 16:45
  • The example I provided is standalone and as simple as I could think to reproduce the behavior I'm seeing. – Michael Hogenson Jul 08 '14 at 16:49
  • I've calculated the height of your table (the correct way, not your way) and your page height is 2 user units short to accommodate for the table. – Bruno Lowagie Jul 08 '14 at 17:03
  • Sorry, a more correct way to explain the situation would be to say that I want the table on the first page to end at the base line of the font used to render the word "Two". Instead the table takes up the remainder of the space provided to the table on the first page. All other rows use the baseline to determine the height of the cell. It's slight but the last row on the first page "extends" to the bottom margin as you explained. – Michael Hogenson Jul 08 '14 at 17:07
  • I've added a picture of what I'm seeing in case we're seeing different things. Notice the spacing under two isn't determined by the base line but rather by the bottom margin of the page. – Michael Hogenson Jul 08 '14 at 17:13
  • Aha, now I understand. That's normal behavior. This doesn't really qualify as 'extending'. It's what so far 99.999% of the users expect. You being the first one that apparently has a use case where you don't agree with this behavior. – Bruno Lowagie Jul 08 '14 at 17:17
  • I understand it's normal behavior. Unfortunately, I'm part of the 0.001% of users that would like to avoid that behavior. Is there a way I can use PdfPCellEvent to figure out the height that I desire? – Michael Hogenson Jul 08 '14 at 17:20
  • No, because when `PdfPCellEvent` is triggered the `Rectangle` has already been defined and there's no way to measure the lines that have been drawn. Your requirement involves core development and being one of the authors of the table functionality, I know that providing that functionality is not something that is easy. Do you have a support contract with us? – Bruno Lowagie Jul 08 '14 at 17:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56974/discussion-between-michael-hogenson-and-bruno-lowagie). – Michael Hogenson Jul 08 '14 at 17:32

1 Answers1

0

This is expected behavior and iText doesn't currently have support for this edge case.

Michael Hogenson
  • 1,292
  • 1
  • 15
  • 31