Skip to main content
Skip table of contents

Repeating rows

Examples that explain how to meet the very specific requirement explained in the following question: How to repeat the last 5 rows of a table on the next page?


headerrowrepeated

JAVA

JAVA
/**
 * This example is written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/32582927/itext-pdfwriter-writing-table-header-if-the-few-table-rows-go-to-new-page
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

/**
 * @author iText
 */
@WrapToTest
public class HeaderRowRepeated {
    public static final String DEST = "results/tables/repeat_header_row.pdf";
    
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new HeaderRowRepeated().createPdf(DEST);
    }    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        // table with 2 columns:
        PdfPTable table = new PdfPTable(2);
        // header row:
        table.addCell("Key");
        table.addCell("Value");
        table.setHeaderRows(1);
        table.setSkipFirstHeader(true);
        // many data rows:
        for (int i = 1; i < 51; i++) {
            table.addCell("key: " + i);
            table.addCell("value: " + i);
        }
        document.add(table);
        document.close();
    }
}


repeatlastrows

JAVA

JAVA
/**
 * This example is written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import sandbox.WrapToTest;

@WrapToTest
public class RepeatLastRows {
    public static final String DEST = "results/tables/repeat_last_rows.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RepeatLastRows().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        // we create a table that spans the width of the page and that has 99 rows
        PdfPTable table = new PdfPTable(1);
        table.setTotalWidth(523);
        for (int i = 1; i < 100; i++)
            table.addCell("row " + i);
        // we add the table at an absolute position (starting at the top of the page)
        PdfContentByte canvas = writer.getDirectContent();
        int currentRowStart = 0;
        int currentRow = 0;
        int totalRows = table.getRows().size();
        while (true) {
            // available height of the page
            float available_height = 770;
            // how many rows fit the height?
            while (available_height > 0 && currentRow < totalRows) {
                available_height -= table.getRowHeight(currentRow++);
            }
            // we stop as soon as all the rows are counted
            if (currentRow == totalRows)
                break;
            // we draw part the rows that fit the page and start a new page
            table.writeSelectedRows(currentRowStart, --currentRow, 36, 806, canvas);
            document.newPage();
            currentRowStart = currentRow;
        }
        // if there are less than 5 rows left, we adjust the row start value
        if (currentRow - currentRowStart < 5)
            currentRowStart = currentRow - 5;
        // we write the remaining rows
        table.writeSelectedRows(currentRowStart, currentRow, 36, 806, canvas);
        document.close();
    }

}


repeatlastrows2

JAVA

JAVA
/**
 * This example is written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import sandbox.WrapToTest;

@WrapToTest
public class RepeatLastRows2 {
    public static final String DEST = "results/tables/repeat_last_rows2.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RepeatLastRows2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        // we create a table that spans the width of the page and that has 99 rows
        PdfPTable table = new PdfPTable(1);
        table.setTotalWidth(523);
        for (int i = 1; i < 100; i++)
            table.addCell("row " + i);
        // we add the table at an absolute position (starting at the top of the page)
        PdfContentByte canvas = writer.getDirectContent();
        int currentRowStart = 0;
        int currentRow = 0;
        int totalRows = table.getRows().size();
        while (true) {
            // available height of the page
            float available_height = 770;
            // how many rows fit the height?
            while (available_height > 0 && currentRow < totalRows) {
                available_height -= table.getRowHeight(currentRow++);
            }
            // we stop as soon as all the rows are counted
            if (currentRow == totalRows) {
                break;
            }
            // we draw part the rows that fit the page and start a new page
            table.writeSelectedRows(currentRowStart, --currentRow, 36, 806, canvas);
            document.newPage();
            currentRowStart = currentRow - 5;
            currentRow -= 5;
            if (currentRow < 1) {
                currentRow = 1;
                currentRowStart = 1;
            }
        }
        // we draw the remaining rows
        table.writeSelectedRows(currentRowStart, -1, 36, 806, canvas);
        document.close();
    }

}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.