停止iText的表从劈裂上新的一页上新、iText

2023-09-07 14:11:00 作者:我心疼所有为爱低头的姑娘

我正在开发Android版生成 PDF的应用程序

I am developing an app for android that generates pdf.

我使用 itextpdf 生成PDF。

我有以下问题:

我有一个具有3行和当该表是靠近页的结束,有时它把一个行上的一个页面和下页面上两行的表。

I have a table that has 3 rows and when this table is near the end of a page sometimes it puts one row on one page and two rows on the next page.

有没有办法来强制此表,开始下一个页面上,所以我可以有充分的下页表格?

Is there a way to force this table to start on the next page so I can have the full table on the next page?

感谢

推荐答案

请看看在分割例如:

Paragraph p = new Paragraph("Test");
PdfPTable table = new PdfPTable(2);
for (int i = 1; i < 6; i++) {
    table.addCell("key " + i);
    table.addCell("value " + i);
}
for (int i = 0; i < 40; i++) {
    document.add(p);
}
document.add(table);

我们有5行的表,并且在这种情况下,我们增加一些段落使得表被添加在页面的末尾。

We have a table with 5 rows, and in this case, we're adding some paragraphs so that the table is added at the end of a page.

在默认情况下,iText的会尽量不拆行,但如果全表不适合,它会转发不适合到下一页的行:

By default, iText will try not to split rows, but if the full table doesn't fit, it will forward the rows that don't fit to the next page:

您想避免这种行为:你不想表分割

You want to avoid this behavior: you don't want the table to split.

因为知道iText的将尽量保持全行不变,则可以解决这个问题,通过嵌套表你不想要分割另一个表内:

Knowing that iText will try to keep full rows intact, you can work around this problem by nesting the table you don' want to split inside another table:

PdfPTable nesting = new PdfPTable(1);
PdfPCell cell = new PdfPCell(table);
cell.setBorder(PdfPCell.NO_BORDER);
nesting.addCell(cell);
document.add(nesting);

现在你会得到这样的结果:

Now you get this result:

目前是previous页上有足够的空间来呈现一对夫妇行,但我们已经裹满表中的行内有一列,iText的将转发完整的表到下一个页面。

There was sufficient space on the previous page to render a couple of rows, but as we've wrapped the full table inside a row with a single column, iText will forward the complete table to the next page.