本文介绍使用Spire.PDF for .NET来设置PDF表格跨页时,重复显示表头行的方法。
C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;
namespace RepeatTableHeaderRow
{
class Program
{
static void Main(string[] args)
{
//新建一个PDF文档
PdfDocument pdf = new PdfDocument();
//添加一页
PdfPageBase page = pdf.Pages.Add();
//创建PdfGrid类的对象
PdfGrid grid = new PdfGrid();
//设置单元格填充
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
//添加表格列数
grid.Columns.Add(3);
//添加表头行及表格数据
PdfGridRow[] pdfGridRows = grid.Headers.Add(1);
for (int i = 0; i < pdfGridRows.Length; i++)
{
pdfGridRows[i].Style.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Regular), true);//指定字体
pdfGridRows[i].Cells[0].Value = "NAME";
pdfGridRows[i].Cells[1].Value = "SUBJECT";
pdfGridRows[i].Cells[2].Value = "SCORES";
pdfGridRows[i].Style.TextBrush = PdfBrushes.Red;
}
//设置重复表头(表格跨页时)
grid.RepeatHeader = true;
//添加数据到表格
for (int i = 0; i < 60; i++)
{
PdfGridRow row = grid.Rows.Add();
for (int j = 0; j < grid.Columns.Count; j++)
{
row.Cells[j].Value = "(Row " + i + ", column " + j + ")";
}
}
//在PDF页面绘制表格
grid.Draw(page, new PointF(0, 20));
//保存文档
pdf.SaveToFile("Result.pdf");
System.Diagnostics.Process.Start("Result.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Grid
Imports System.Drawing
Namespace RepeatTableHeaderRow
Class Program
Private Shared Sub Main(args As String())
'新建一个PDF文档 Dim pdf As New PdfDocument() '添加一页
Dim page As PdfPageBase = pdf.Pages.Add()
'创建PdfGrid类的对象 Dim grid As New PdfGrid() '设置单元格填充
grid.Style.CellPadding = New PdfPaddings(1, 1, 1, 1)
'添加表格列数 grid.Columns.Add(3) '添加表头行及表格数据
Dim pdfGridRows As PdfGridRow() = grid.Headers.Add(1)
For i As Integer = 0 To pdfGridRows.Length - 1
pdfGridRows(i).Style.Font = New PdfTrueTypeFont(New Font("Arial", 11F, FontStyle.Regular), True)
'指定字体 pdfGridRows(i).Cells(0).Value = "NAME" pdfGridRows(i).Cells(1).Value = "SUBJECT" pdfGridRows(i).Cells(2).Value = "SCORES" pdfGridRows(i).Style.TextBrush = PdfBrushes.Red Next '设置重复表头(表格跨页时)
grid.RepeatHeader = True
'添加数据到表格 For i As Integer = 0 To 59 Dim row As PdfGridRow = grid.Rows.Add() For j As Integer = 0 To grid.Columns.Count - 1 row.Cells(j).Value = "(Row " + i + ", column " + j + ")" Next Next '在PDF页面绘制表格
grid.Draw(page, New PointF(0, 20))
'保存文档 pdf.SaveToFile("Result.pdf") System.Diagnostics.Process.Start("Result.pdf") End Sub End Class End Namespace
文档效果: