表单元格中的大表调用分页符

问题描述 投票:0回答:1

我具有单个列的单个PdfPTable。一页可容纳50行。如果我向表中添加一些文本数据(例如,300行),则报告工作正常。当我将PdfPTable添加到单元格(例如20个字符串单元格,PdfPTable(其中包含20行或更少的行数)和270个字符串单元格)时,所有这些也都可以正常工作:

/--------
20 string rows
inner table (20 rows)
10 string rows
/-------
...additional rows

但是,当我的内部表有更多行(mainTable [20个字符串行,innerTable [90个字符串行],270个字符串行],报告中断第一页,并在第二页上开始innerTable输出。

/---------
20 string rows
whitespace for 30 rows
/---------
inner table (50 rows from 90)
/---------
inner table (40 rows from 90)
..additional data

我需要这个:

/---------
20 string rows
inner table (30 rows from 90)
/---------
inner table (50 rows from 90)
/---------
inner table (10 rows from 80)
..additional data

有人知道解决方案吗?

ps itext版本-2.1.7

java itext
1个回答
1
投票

[请看NestedTables2示例。在此示例中,我们告诉iText,只要一行不适合页面,就可以拆分单元格:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9hOEE1US5wbmcifQ==” alt =“在此处输入图像描述”>

这不是默认值:默认情况下,iText将尝试通过将其转发到下一页来使行保持完整。仅当该行不适合页面时,该行才会被拆分。

更改默认值涉及到在代码中添加一行。该行称为:table.setSplitLate(false);

这是创建屏幕截图中显示的PDF的完整方法:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.setSplitLate(false);
    table.setWidths(new int[]{1, 15});
    for (int i = 1; i <= 20; i++) {
        table.addCell(String.valueOf(i));
        table.addCell("It is not smart to use iText 2.1.7!");
    }
    PdfPTable innertable = new PdfPTable(2);
    innertable.setWidths(new int[]{1, 15});
    for (int i = 0; i < 90; i++) {
        innertable.addCell(String.valueOf(i + 1));
        innertable.addCell("Upgrade if you're a professional developer!");
    }
    table.addCell("21");
    table.addCell(innertable);
    for (int i = 22; i <= 40; i++) {
        table.addCell(String.valueOf(i));
        table.addCell("It is not smart to use iText 2.1.7!");
    }
    document.add(table);
    document.close();
}

请注意,您不应使用iText 2.1.7。如果这样做,您的雇主,客户,投资者可能会遇到问题。在免费电子书The Best iText Questions on StackOverflow的法律部分中阅读有关此内容的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.