本文介绍使用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
文档效果: