Java – iText – avoid the last line and don’t split the page to the next page

I'm using Java's iText 5 I have some pages with multiple tables with dynamic rows In some cases, the last row of the table will be divided into the next page and the next heading I am using setheaderrows () and setskipfirstheader () to manage the continuation of the next page The last line has enough space to accommodate early pages I want to be on the last line of the same page, not the next page

For example, on page 1, the last line is split into the first line of the next page Instead, I want page 1 to fit that line, so save an extra page with all the white space

I tried to use setextendlastrow (), but it didn't work Who knows how to solve this problem I attached a working example code

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

        for (int m = 1; m < 20; m++) {

            int row = 0;
            PdfPTable table = new PdfPTable(1);
            table.setSpacingAfter(0);
            table.setSpacingBefore(0);
            table.setWidthPercentage(100);

            table.setHeaderRows(1);
            table.setSkipFirstHeader(true);
            add(table,"Header Row continued " + m,BaseColor.LIGHT_GRAY,row++);
            add(table,"Header Row normal " + m,row++);

            add(table,"Text Row 1 ",BaseColor.WHITE,"Text Row 2 ","Text Row 3 ",row++);

            addPadding(table);

            document.add(table);
        }

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

private static void add(PdfPTable table,String text,BaseColor color,int row) {
    PdfPCell pdfCellHeader = new PdfPCell();
    pdfCellHeader.setBackgroundColor(color);
    pdfCellHeader.addElement(new Paragraph(new Phrase(text)));
    table.addCell(pdfCellHeader);
}

private static void addPadding(PdfPTable table) {
    PdfPCell cell = new PdfPCell();
    cell.setFixedHeight(2f);
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setColspan(table.getNumberOfColumns());
    table.addCell(cell);
}
}

Solution

I must implement this example to understand your problem By talking about a title that is not a title ("the line with normal title line is not a title line!), And your reference to setextendlastrow () doesn't help (I won't feel it when I mention this method, which is very confusing)

That is to say, there is no brain to solve your problem I override the main class:

public static void main(String[] args) {
    try {
        Document document = new Document();
        document.setPageSize(PageSize.LETTER);
        document.setMargins(16,new FileOutputStream("SplitLastRow.pdf"));
        document.open();
        document.setPageSize(PageSize.LETTER);
        document.setMargins(16,38);

        for (int m = 1; m < 20; m++) {

            int row = 0;
            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);
            add(table,row++);

            addPadding(table);
            if (writer.getVerticalPosition(true) - table.getRowHeight(0) - table.getRowHeight(1) < document.bottom()) {
                document.newPage();
            }
            document.add(table);
        }

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

Make sure you define the total width, not the width percentage, and lock the width As recorded (and common sense tells you), if the width percentage is defined, the pdfptable object does not know its actual width Needless to say, you cannot calculate the height of a table without knowing its actual width

Then use the getverticalposition () method to get the current position of the cursor and check whether the first two lines fit the page If you don't go to the new page before adding the table If you want to check the suitability of the entire table, use the gettotalheight () method instead of the getrowheight () method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>