Java set PDF spread sheet table to repeatedly display header row

Time:2021-10-25

When creating a table, if the table contents are displayed across pages, the table header will not be displayed on the next page by default, which is not very good in reading experience. Next, we share a method to display the table header content in the table cross page. Here, we only need to simply use the method grid. Setrepeatheader (true); Just. Refer to the following method steps for details.

  1. Import jar package

The jar package of free spire.pdf for Java is imported here. After downloading, unzip the file, and import the spire.pdf.jar in the Lib file under the unzipped path into the Java program.
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();

     //Create an object of the pdfGrid class
     PdfGrid grid = new PdfGrid();

     //Set cell fill
     grid.getStyle().setCellPadding(www.diuxie.comnew PdfPaddings(1,1,1,1));

     //Add table columns
     grid.getColumns().add(3);

     //Add header row 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));// Specify 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());
         /*pdfGridRows[i].getStyle().setFont(new PdfCjkStandardFont(PdfCjkFontFamily.Hanyang_Systems_Gothic_Medium, 14f));// Method of drawing Chinese, Japanese and Korean fonts
         pdfGridRows[i].getCells().get(0).setValue("이 름");
         Pdfgridrows [i]. Getcells(). Get (1). SetValue ("account");
         pdfGridRows[i].getCells().get(2).setValue("ほしとり");
         pdfGridRows[i].getStyle().setTextBrush(PdfBrushes.getBlue());*/
     }

set upMobile GamesDuplicate header for (table spread)

grid.setRepeatHeader(true);

     //Add data to table
     for (int i = 0; i < 80; 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) + ")");
         }
     }

     //Drawing tables on PDF pages
     grid.draw(page,0,0);

     //Save document
     pdf.saveToFile("Result.pdf");
     pdf.dispose();
 }

}