This article demonstrates how to repeat table header rows across pages in PDF using Spire.PDF for Java.
import com.spire.pdf.*; import com.spire.pdf.graphics.*; import com.spire.pdf.grid.PdfGrid; import com.spire.pdf.grid.PdfGridRow; import java.awt.*; public class RepeatTableHeaderRow { public static void main(String[] args) { //Create a new PDF document PdfDocument pdf = new PdfDocument(); //Add a page PdfPageBase page = pdf.getPages().add(); //Instantiate a PdfGrid class object PdfGrid grid = new PdfGrid(); //Set cell padding grid.getStyle().setCellPadding(new PdfPaddings(1,1,1,1)); //Add columns grid.getColumns().add(3); //Add header rows and table data PdfGridRow[] pdfGridRows = grid.getHeaders().add(1); for (int i = 0; i < pdfGridRows.length; i++) { pdfGridRows[i].getStyle().setFont(new PdfTrueTypeFont(new Font("Arial", Font.PLAIN,12), true));//Designate a font pdfGridRows[i].getCells().get(0).setValue("NAME"); pdfGridRows[i].getCells().get(1).setValue("SUBJECT"); pdfGridRows[i].getCells().get(2).setValue("SCORES"); pdfGridRows[i].getStyle().setTextBrush(PdfBrushes.getRed()); } //Repeat header rows (when across pages) grid.setRepeatHeader(true); //Add values to the table for (int i = 0; i < 60; i++) { PdfGridRow row = grid.getRows().add(); for (int j = 0; j < grid.getColumns().getCount();j++) { row.getCells().get(j).setValue("(Row " + (i+1) + ", column " + (j+1) + ")"); } } // Draw a table in PDF grid.draw(page,0,40); //Save the document pdf.saveToFile("Result.pdf"); pdf.dispose(); } }
Output