spire.pdf提供了两种类pdftable和pdfgrid用于创建pdf表格,二者在对表格进行格式化操作时存在以下差别:
pdftable
|
pdfgrid
|
|
格式设置
|
||
行 | 无api支持,可以通过事件设置 | 可直接通过api设置 |
列 | 可直接通过api设置(stringformat) | 可直接通过api设置(stringformat) |
单元格 | 无api支持,可以通过事件设置 | 可直接通过api设置 |
其他
|
||
单元格纵向合并 | 不支持 | 可直接通过api设置 |
单元格横向合并 | 无api支持,可以通过事件设置 | 可直接通过api设置 |
嵌套表格 | 无api支持,可以通过事件设置 | 可直接通过api设置 |
事件 | begincelllayout, beginpagelayout, beginrowlayout, endcelllayout, endpagelayout, endrowlayout | beginpagelayout, endpagelayout |
通过 pdftable 类创建表格
c#
static void main(string[] args)
{
//创建一个pdf文档
pdfdocument doc = new pdfdocument();
//添加一页
pdfpagebase page = doc.pages.add();
//创建一个pdftable对象
pdftable table = new pdftable();
//设置字体
table.style.defaultstyle.font = new pdftruetypefont(new font("arial unicode ms", 9f), true);
table.style.headerstyle.font = new pdftruetypefont(new font("arial unicode ms", 9f), true);
//创建一个datatable并写入数据
datatable datatable = new datatable();
datatable.columns.add("名字");
datatable.columns.add("年龄");
datatable.columns.add("性别");
datatable.rows.add(new string[] { "张红", "22", "女" });
datatable.rows.add(new string[] { "王东", "25", "男" });
//填充数据到pdf表格
table.datasource = datatable;
//显示表头(默认不显示)
table.style.showheader = true;
//在beginrowlayout事件处理方法中注册自定义事件
table.beginrowlayout = table_beginrowlayout;
//将表格绘入pdf并指定位置和大小
table.draw(page, new rectanglef(0, 20, 200, 90));
//保存到文档
doc.savetofile("pdf表格_1.pdf");
}
//在自定义事件中设置行高
private static void table_beginrowlayout(object sender, beginrowlayouteventargs args)
{
args.minimalheight = 20f;
}
vb.net
private shared sub main(args as string())
'创建一个pdf文档
dim doc as new pdfdocument()
'添加一页
dim page as pdfpagebase = doc.pages.add()
'创建一个pdftable对象
dim table as new pdftable()
'设置字体
table.style.defaultstyle.font = new pdftruetypefont(new font("arial unicode ms", 9f), true)
table.style.headerstyle.font = new pdftruetypefont(new font("arial unicode ms", 9f), true)
'创建一个datatable并写入数据
dim datatable as new datatable()
datatable.columns.add("名字")
datatable.columns.add("年龄")
datatable.columns.add("性别")
datatable.rows.add(new string() {"张红", "22", "女"})
datatable.rows.add(new string() {"王东", "25", "男"})
'填充数据到pdf表格
table.datasource = datatable
'显示表头(默认不显示)
table.style.showheader = true
'在beginrowlayout事件处理方法中注册自定义事件
addhandler table.beginrowlayout, addressof table_beginrowlayout
'将表格绘入pdf并指定位置和大小
table.draw(page, new rectanglef(0, 20, 200, 90))
'保存到文档
doc.savetofile("pdf表格_1.pdf")
end sub
'在自定义事件中设置行高
private shared sub table_beginrowlayout(sender as object, args as beginrowlayouteventargs)
args.minimalheight = 20f
end sub
通过 pdfgrid 创建表格
创建一个简单的表格
c#
//创建一个pdf文档
pdfdocument doc = new pdfdocument();
//添加一页
pdfpagebase page = doc.pages.add();
//创建一个pdfgrid对象
pdfgrid grid = new pdfgrid();
//设置单元格边距
grid.style.cellpadding = new pdfpaddings(1, 1, 1, 1);
//添加2行4列
pdfgridrow row1 = grid.rows.add();
pdfgridrow row2 = grid.rows.add();
grid.columns.add(4);
//设置列宽
foreach (pdfgridcolumn col in grid.columns)
{
col.width = 60f;
}
//写入数据
for (int i = 0; i < grid.columns.count; i )
{
row1.cells[i].value = string.format("col{0}", i 1);
row2.cells[i].value = string.format("{0}", i 1);
}
//将表格绘入文档
grid.draw(page, new pointf(0, 20));
//保存到文档
doc.savetofile("pdf表格_2.pdf");
vb.net
'创建一个pdf文档
dim doc as new pdfdocument()
'添加一页
dim page as pdfpagebase = doc.pages.add()
'创建一个pdfgrid对象
dim grid as new pdfgrid()
'设置单元格边距
grid.style.cellpadding = new pdfpaddings(1, 1, 1, 1)
'添加2行4列
dim row1 as pdfgridrow = grid.rows.add()
dim row2 as pdfgridrow = grid.rows.add()
grid.columns.add(4)
'设置列宽
for each col as pdfgridcolumn in grid.columns
col.width = 60f
next
'写入数据
for i as integer = 0 to grid.columns.count - 1
row1.cells(i).value = [string].format("col{0}", i 1)
row2.cells(i).value = [string].format("{0}", i 1)
next
'将表格绘入文档
grid.draw(page, new pointf(0, 20))
'保存到文档
doc.savetofile("pdf表格_2.pdf")
合并单元格,设置背景色和文字对齐方式
c#
//创建pdf文档
pdfdocument doc = new pdfdocument();
//添加一页
pdfpagebase page = doc.pages.add();
//创建一个pdfgrid对象
pdfgrid grid = new pdfgrid();
//设置单元格边距
grid.style.cellpadding = new pdfpaddings(1, 1, 1, 1);
//设置表格默认字体
grid.style.font= new pdftruetypefont(new font("arial unicode ms", 9f), true);
//添加4行4列
pdfgridrow row1 = grid.rows.add();
pdfgridrow row2 = grid.rows.add();
pdfgridrow row3 = grid.rows.add();
pdfgridrow row4 = grid.rows.add();
grid.columns.add(4);
//设置列宽
foreach (pdfgridcolumn col in grid.columns)
{
col.width = 60f;
}
//写入数据
row1.cells[0].value = "订单及支付情况";
row2.cells[0].value = "订单号";
row2.cells[1].value = "日期";
row2.cells[2].value = "客户";
row2.cells[3].value = "是否付款";
row3.cells[0].value = "00223";
row3.cells[1].value = "2016/06/02";
row3.cells[2].value = "阳光地产";
row3.cells[3].value = "是";
row4.cells[0].value = "00224";
row4.cells[1].value = "2016/06/03";
row4.cells[3].value = "否";
//水平和垂直合并单元格
row1.cells[0].columnspan = 4;
row3.cells[2].rowspan = 2;
//设置单元格内文字对齐方式
row1.cells[0].stringformat = new pdfstringformat(pdftextalignment.center);
row3.cells[2].stringformat = new pdfstringformat(pdftextalignment.justify, pdfverticalalignment.middle);
//设置单元格背景颜色
row1.cells[0].style.backgroundbrush = pdfbrushes.gray;
row3.cells[3].style.backgroundbrush = pdfbrushes.green;
row4.cells[3].style.backgroundbrush = pdfbrushes.mediumvioletred;
//设置边框颜色、粗细
pdfborders borders = new pdfborders();
borders.all = new pdfpen(color.black, 0.1f);
foreach (pdfgridrow pgr in grid.rows)
{
foreach (pdfgridcell pgc in pgr.cells)
{
pgc.style.borders = borders;
}
}
//在指定为绘入表格
grid.draw(page, new pointf(0, 20));
//保存到文档
doc.savetofile("pdf表格_3.pdf");
vb.net
'创建pdf文档
dim doc as new pdfdocument()
'添加一页
dim page as pdfpagebase = doc.pages.add()
'创建一个pdfgrid对象
dim grid as new pdfgrid()
'设置单元格边距
grid.style.cellpadding = new pdfpaddings(1, 1, 1, 1)
'设置表格默认字体
grid.style.font = new pdftruetypefont(new font("arial unicode ms", 9f), true)
'添加4行4列
dim row1 as pdfgridrow = grid.rows.add()
dim row2 as pdfgridrow = grid.rows.add()
dim row3 as pdfgridrow = grid.rows.add()
dim row4 as pdfgridrow = grid.rows.add()
grid.columns.add(4)
'设置列宽
for each col as pdfgridcolumn in grid.columns
col.width = 60f
next
'写入数据
row1.cells(0).value = "订单及支付情况"
row2.cells(0).value = "订单号"
row2.cells(1).value = "日期"
row2.cells(2).value = "客户"
row2.cells(3).value = "是否付款"
row3.cells(0).value = "00223"
row3.cells(1).value = "2016/06/02"
row3.cells(2).value = "阳光地产"
row3.cells(3).value = "是"
row4.cells(0).value = "00224"
row4.cells(1).value = "2016/06/03"
row4.cells(3).value = "否"
'水平和垂直合并单元格
row1.cells(0).columnspan = 4
row3.cells(2).rowspan = 2
'设置单元格内文字对齐方式
row1.cells(0).stringformat = new pdfstringformat(pdftextalignment.center)
row3.cells(2).stringformat = new pdfstringformat(pdftextalignment.justify, pdfverticalalignment.middle)
'设置单元格背景颜色
row1.cells(0).style.backgroundbrush = pdfbrushes.gray
row3.cells(3).style.backgroundbrush = pdfbrushes.green
row4.cells(3).style.backgroundbrush = pdfbrushes.mediumvioletred
'设置边框颜色、粗细
dim borders as new pdfborders()
borders.all = new pdfpen(color.black, 0.1f)
for each pgr as pdfgridrow in grid.rows
for each pgc as pdfgridcell in pgr.cells
pgc.style.borders = borders
next
next
'在指定为绘入表格
grid.draw(page, new pointf(0, 20))
'保存到文档
doc.savetofile("pdf表格_3.pdf")