iText - 如何找到最后一行分为下一页 [英] iText - how to find last row is split into next page

查看:188
本文介绍了iText - 如何找到最后一行分为下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组包含动态行的表。在某些情况下,表格在页面之间进行分割。在某些情况下,只有最后一行被拆分为下一页。假设一个表有10行,第1行显示第1行,第2行显示第10行。

I have group of tables with dynamic rows. There are some scenarios where a table is splitted between pages. In some scenarios, only the last row is split into next page. Suppose if a table has 10 rows, the rows 1 thru 9 are displayed in page 1 and row 10 is displayed in page 2.

我正在寻找一个解决方案一个分页符(document.newpage())恰好将整个表移动到此场景中的下一页。我尝试了下面的代码,它适用于某些场景,但不适用于所有场景。我想知道什么时候表格最后一行要拆分,所以我可以添加分页符来将整个表格移动到下一页以避免最后一行孤儿。

I am looking for a solution to have a page break (document.newpage()) to happen to move the entire table to next page in this scenario. I tried below code and it was working for some scenarios but not for all. I would like to find out when a table last row is going to split so I can add page break to move the complete table to next page to avoid last row orphan.

public class SplitItextLastRow {

final static private String BODY_FONT = FontFactory.getFont("Arial").getFamilyname();

public static void main(String[] args) {
    try {
        Document document = new Document();
        document.setPageSize(PageSize.LETTER);
        document.setMargins(16, 14, 14, 14);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/SplitItext.pdf"));
        document.open();
        document.setPageSize(PageSize.LETTER);
        document.setMargins(16, 14, 14, 14);

        int[] maxCountRows = new int[] { 3, 9, 2, 3, 7, 9, 2, 4, 5, 4, 9, 7, 6, 5, 4, 6, 8, 3 };

        for (int k = 1; k <= maxCountRows.length; k++) {

            PdfPTable table = new PdfPTable(1);
            table.setSpacingAfter(0);
            table.setSpacingBefore(0);
            table.setTotalWidth(document.right() - document.left());
            table.setLockedWidth(true);

            table.setHeaderRows(1);
            table.setSkipFirstHeader(true);
            addHeader(table, "Header Row continued " + k, BaseColor.LIGHT_GRAY);
            addHeader(table, "Header Row normal " + k, BaseColor.LIGHT_GRAY);

            for (int i = 1; i < maxCountRows[k - 1]; i++) {
                StringBuilder sb = new StringBuilder();
                for (int p = 0; p < maxCountRows[k - 1] * 6; p++) {
                    sb.append("Text Row ");
                }
                add(table, sb.toString() + i, BaseColor.WHITE);
            }

            float verticalPosition = writer.getVerticalPosition(true);
            System.out.println("------Table " + k);
            if ((verticalPosition - table.getTotalHeight()) <= document.bottom()) {
                System.out.println("........................................Last Row splitted");
            }

            document.add(table);
        }

        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }

}

private static void addHeader(PdfPTable table, String text, BaseColor color) {
    PdfPCell pdfCellHeader = new PdfPCell();
    pdfCellHeader.setBackgroundColor(color);
    pdfCellHeader.addElement(new Paragraph(new Phrase(text, FontFactory.getFont(BODY_FONT, 8f, BaseColor.BLUE))));
    table.addCell(pdfCellHeader);
}

private static void add(PdfPTable table, String text, BaseColor color) {
    PdfPCell pdfCellHeader = new PdfPCell();
    pdfCellHeader.setBackgroundColor(color);
    pdfCellHeader.addElement(new Paragraph(new Phrase(text, FontFactory.getFont(BODY_FONT, 5f, BaseColor.GREEN))));
    table.addCell(pdfCellHeader);
}

}

推荐答案

是的,它的工作原理如下:

Yeah, it works as following:

在循环的开头 for(int k = 1 ; ... 你必须添加:

At the beginning of your loop for (int k = 1; ... you have to add:

PdfPTable outerTable = new PdfPTable(1);
outerTable.setSpacingAfter(0);
outerTable.setSpacingBefore(0);
outerTable.setTotalWidth(document.right() - document.left());
outerTable.setLockedWidth(true);

并且在它结束时,你替换 document.add( table); with:

and at it's end, you replace the document.add(table); with:

outerTable.addCell(new PdfPCell(table));
document.add(outerTable);

这就是...... :-)

然后你应该删除你的调试输出,你可以删除继续标题和它对应的 table.setSkipFirstHeader(true); 。你只需 table.setHeaderRows(1);

And this is it... :-)
You should then remove your debug outputs and you can remove the "continued" header thing and its corresponding table.setSkipFirstHeader(true);. It's enough that you have table.setHeaderRows(1);.

因为你的原创而有点懒散table也只有一列,你可以用 PdfPTable outerTable = new PdfPTable(table); 创建你的 outerTable 你用它的宽度和间距初始化了你的。但是,在将内容添加到之前,必须先创建它。

To be a bit lazier, since your original table has only one column as well, you can create your outerTable with PdfPTable outerTable = new PdfPTable(table); after you initialized your table with its width and spacings. But you have to create it before you add your content to your table.

这篇关于iText - 如何找到最后一行分为下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆