iText - how to find last row is split into next pa

2019-08-30 17:58发布

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.

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

}

3条回答
萌系小妹纸
2楼-- · 2019-08-30 18:27

I tried Johannes' solution, but it did not work, I don't know why. Anyways, I found an own solution. I tested it with some random scenarios and it worked. The solution I found is to put the code below before adding the table to the document with document.add(table).

int headerRowsCount = table.getHeaderRows();
float headerRowsHeight = 0f;
for (int j = 0; j < headerRowsCount; j++) {
    headerRowsHeight += table.getRowHeight(j);
}
float totalTableHeight = table.getTotalHeight() - headerRowsHeight;
float lastRowHeight = table.getRowHeight(table.getRows().size() - 1);
float tableHeightExceptLastRow = totalTableHeight - lastRowHeight + document.bottom();
float tableGap = verticalPosition - tableHeightExceptLastRow;
boolean lastRowSplit = (tableGap > -35 && (tableGap < lastRowHeight) && tableHeightExceptLastRow > 0);
if (lastRowSplit) {
    document.newPage();
}
查看更多
看我几分像从前
3楼-- · 2019-08-30 18:31

Here's the complete working example as explained here... I used iText 2.1.7.

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class SplitItextLastRow
{
    final static private String BODY_FONT = FontFactory.getFont("Arial").getFamilyname();

    public static void main(final String[] args)
    {
        try
        {
            Document document = new Document();
            document.setPageSize(PageSize.LETTER);
            document.setMargins(16, 14, 14, 14);
            PdfWriter writer =
                    PdfWriter.getInstance(document, new FileOutputStream("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);

                PdfPTable outerTable = new PdfPTable(table);

                table.setHeaderRows(1);
                table.setSkipFirstHeader(true);
                addHeader(table, "Header Row continued " + k, Color.LIGHT_GRAY);
                addHeader(table, "Header Row normal " + k, Color.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, Color.WHITE);
                }

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

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

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

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

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

And the output it produces is here. Isn't that what you expected to achieve?

查看更多
再贱就再见
4楼-- · 2019-08-30 18:46

Yeah, it works as following:

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

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

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

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

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.

查看更多
登录 后发表回答