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("宋体", 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("宋体", 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")