将单元格内容保留在一页上

时间:2015-03-16 16:02:09

标签: java itext

我有一张桌子,有一列。每个单元格包含一个段落。

如何阻止段落分割两页?

PdfPtable table = new PdfPTable(1);

//report must be printed as compat as possible
table.setSplitLate(false);

//I can't set keep together, because table can be larger than page size
//table.setKeepTogether(true);

for (int i = 0; i < 100; i++) {
    //Random text. Can contain ~400 chars.
    String text = "aaaaaaaaaaaaaaa sssssssssssss ddddddddddd ffffffffff";
    Paragraph p = new Paragraph(text);

    //That instruction does not work. I don't know why, may be because paragraph printed in cell.
    p.setKeepTogether(true);

    table.addCell(p);
}

1 个答案:

答案 0 :(得分:1)

更改

table.setSplitLate(false);

table.setSplitLate(true);

这样,除非整个单元格不适合单页,否则不会拆分您的单元格。