2

I'm getting unexpected results when I try to keep rows in an iText table together. Below is some standalone code that creates a PDF with the results I'm seeing.

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);

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);
}
table.keepRowsTogether(new int[] { 8, 9 });

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

The example creates a table that is two pixels too small to fit on the first page, forcing the last row onto the next page. I would expect, however, since I added the array using keepRowsTogether, to see the first eight rows to stay on one page and the last two to stay together on the next page but that isn't the case as shown by the example images below. Instead the seventh row (counting from zero) is also carried over to the next page.

enter image description here enter image description here According to the API documentation found here, keepRowsTogether "Defines which rows should not allow a page break (if possible)." That indicates to me that row seven, which isn't included in the array, should allow a page break.

Does anyone have any ideas how to keep the seventh row from getting carried over to the next page when it definitely fits on the first?

Solution: After talking to Bruno, I realized I misunderstood how keepRowsTogether works. All I need to do for the example above is change table.keepRowsTogether(new int[] { 8, 9 }); to table.keepRowsTogether(9).

Michael Hogenson
  • 1,292
  • 1
  • 15
  • 31
  • `to see the first eight rows to stay on one page ` you can use `keepRowsTogether(1,8)` for that and in your case may be you specified `keepRowsTogether(new int[] { 8, 9 });` it is doing that only it kept the 8th and 9th row together since the pixel dint allow the first page to keep the 8th and 9th with the first 7 so it shiftd to the next page – SparkOn Jul 10 '14 at 06:19

1 Answers1

1

I think there's a misunderstanding about what keepRowsTogether() means. If you use:

table.keepRowsTogether(new int[] { 8, 9 });

You indicate that the table won't split at row 8 or 9. This is consistent with what I see.

Maybe you want something like this: split_at_row.pdf

This PDF is created using SplitRowAtSpecificRow. Instead of keepRowsTogether(), it uses:

table.setBreakPoints(8);

This means that iText will give preference to splitting the table after row 8 (start counting at 0). You can introduce more than one breakpoint. For instance:

table.setBreakPoints(4, 8, 12);
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I saw the `setBreakPoints` method when I Was looking through the API. In my situation row 8 is a subtitle and shouldn't be orphaned at the bottom of a page so I thought by telling the table not to split on row 8 or 9 that it would keep the rows married, which it did. I do however want row 7 to stay on the first page, thought I don't think `setBreakPoints` is what I want since I'm not sure when I'm generating my tables whether the subtitle is at the end of the page or in the middle. – Michael Hogenson Jul 10 '14 at 14:49
  • To keep row 9 married to 8, would the correct way be `table.keepRowsTogether(new int[] { 9 });`? – Michael Hogenson Jul 10 '14 at 14:49
  • I would use `table.keepRowsTogether(9);` but I've just tested an putting the `9` into an array also seems to work... – Bruno Lowagie Jul 10 '14 at 14:58