spire.pdf支持添加嵌套表格和图片到pdf表格的单元格,本文将对此做详细介绍。
c#
//创建pdf文档
pdfdocument pdf = new pdfdocument();
//添加一个页面
pdfpagebase page = pdf.pages.add();
//创建一个pdf表格
pdfgrid grid = new pdfgrid();
//添加两行
pdfgridrow row1 = grid.rows.add();
pdfgridrow row2 = grid.rows.add();
//设置表格的单元格内容和边框之间的上边距和下边距
grid.style.cellpadding.top = 5f;
grid.style.cellpadding.bottom = 5f;
//添加两列
grid.columns.add(2);
//设置列宽
grid.columns[0].width = 120f;
grid.columns[1].width = 120f;
//创建另一个需要嵌套的表格
pdfgrid embedgrid = new pdfgrid();
//添加一行
pdfgridrow newrow = embedgrid.rows.add();
//添加两列
embedgrid.columns.add(2);
//设置列宽
embedgrid.columns[0].width = 50f;
embedgrid.columns[1].width = 50f;
sizef imagesize = new sizef(50, 50);
//加载图片
pdfgridcellcontentlist contentlist = new pdfgridcellcontentlist();
pdfgridcellcontent content = new pdfgridcellcontent();
content.image = pdfimage.fromfile(@"doc.png");
content.imagesize = imagesize;
contentlist.list.add(content);
pdfstringformat stringformat = new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle);
pdftruetypefont font = new pdftruetypefont(new font("arial unicode ms", 11f), true);
//设置嵌套表格的单元格的值和格式
newrow.cells[0].value = "spire.doc";
newrow.cells[0].stringformat = stringformat;
newrow.cells[1].value = contentlist; //将图片添加到嵌套表格的第二个单元格
newrow.cells[1].stringformat = stringformat;
//设置第一个表格的单元格的值和格式
row1.cells[0].value = "客户姓名";
row1.cells[0].stringformat = stringformat;
row1.cells[0].style.font = font;
row1.cells[0].style.backgroundbrush = pdfbrushes.forestgreen;
row1.cells[1].value = "产品";
row1.cells[1].stringformat = stringformat;
row1.cells[1].style.font = font;
row1.cells[1].style.backgroundbrush = pdfbrushes.forestgreen;
row2.cells[0].value = "肖恩";
row2.cells[0].stringformat = stringformat;
row2.cells[0].style.font = font;
row2.cells[1].value = embedgrid; //将嵌套表格添加到第一个表格的第二行第二个单元格
row2.cells[1].stringformat = stringformat;
//将第一个表格画到页面上
grid.draw(page, new pointf(0f, 30f));
//保存文档
pdf.savetofile("嵌套表格和图片.pdf");
vb.net
'创建pdf文档
dim pdf as new pdfdocument()
'添加一个页面
dim page as pdfpagebase = pdf.pages.add()
'创建一个pdf表格
dim grid as new pdfgrid()
'添加两行
dim row1 as pdfgridrow = grid.rows.add()
dim row2 as pdfgridrow = grid.rows.add()
'设置表格的单元格内容和边框之间的上边距和下边距
grid.style.cellpadding.top = 5f
grid.style.cellpadding.bottom = 5f
'添加两列
grid.columns.add(2)
'设置列宽
grid.columns(0).width = 120f
grid.columns(1).width = 120f
'创建另一个需要嵌套的表格
dim embedgrid as new pdfgrid()
'添加一行
dim newrow as pdfgridrow = embedgrid.rows.add()
'添加两列
embedgrid.columns.add(2)
'设置列宽
embedgrid.columns(0).width = 50f
embedgrid.columns(1).width = 50f
dim imagesize as new sizef(50, 50)
'加载图片
dim contentlist as new pdfgridcellcontentlist()
dim content as new pdfgridcellcontent()
content.image = pdfimage.fromfile("doc.png")
content.imagesize = imagesize
contentlist.list.add(content)
dim stringformat as new pdfstringformat(pdftextalignment.center, pdfverticalalignment.middle)
dim font as new pdftruetypefont(new font("arial unicode ms", 11f), true)
'设置嵌套表格的单元格的值和格式
newrow.cells(0).value = "spire.doc"
newrow.cells(0).stringformat = stringformat
newrow.cells(1).value = contentlist
'将图片添加到嵌套表格的第二个单元格
newrow.cells(1).stringformat = stringformat
'设置第一个表格的单元格的值和格式
row1.cells(0).value = "客户姓名"
row1.cells(0).stringformat = stringformat
row1.cells(0).style.font = font
row1.cells(0).style.backgroundbrush = pdfbrushes.forestgreen
row1.cells(1).value = "产品"
row1.cells(1).stringformat = stringformat
row1.cells(1).style.font = font
row1.cells(1).style.backgroundbrush = pdfbrushes.forestgreen
row2.cells(0).value = "肖恩"
row2.cells(0).stringformat = stringformat
row2.cells(0).style.font = font
row2.cells(1).value = embedgrid
'将嵌套表格添加到第一个表格的第二行第二个单元格
row2.cells(1).stringformat = stringformat
'将第一个表格画到页面上
grid.draw(page, new pointf(0f, 30f))
'保存文档
pdf.savetofile("嵌套表格和图片.pdf")