嵌套表格,即在一張表格中的特定單元格中再插入一個或者多個表格,使用嵌套表格的優點在于能夠讓内容的布局更加合理,同時也友善程式套用。下面的示例中,将介紹如何通過C#程式設計來示範如何插入嵌套表格到PDF文檔。
要點概括:
1. 插入嵌套表格
2. 插入文字到嵌套表格
3. 插入圖檔到嵌套表格
使用工具
- Spire.PDF 4.9.7
注:
1.這裡使用的版本為4.9.7,經測試,對于代碼中涉及的PdfGridCellContentList類和PdfGridCellContent類僅在使用該版本或者以上版本可用。使用時,請注意版本資訊。
2.下載下傳安裝後,在編輯代碼時,請注意添加引用Spire.Pdf.dll(dll檔案可在安裝路徑下的Bin檔案夾下擷取)
示例代碼(供參考)
步驟 1 :建立文檔
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
步驟 2 :添加字型、畫筆,寫入文本到PDF文檔
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("行楷", 11f), true);
PdfPen pen = new PdfPen(Color.Gray);
string text = "2018 Pyeongchang Olympic Winter Games Medal Ranking";
page.Canvas.DrawString(text, font, pen, 100, 50);
步驟 3 :建立第一個表格
//建立一個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(3);
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 150f;
grid.Columns[2].Width = 120f;
步驟 4 :建立一個嵌套表格
//建立一個一行兩列的嵌套表格
PdfGrid embedGrid1 = new PdfGrid();
PdfGridRow newRow = embedGrid1.Rows.Add();
embedGrid1.Columns.Add(2);
//設定嵌套表格的列寬
embedGrid1.Columns[0].Width = 50f;
embedGrid1.Columns[1].Width = 60f;
步驟 5 :添加文本、圖檔到嵌套表格
//初始化SizeF類,設定圖檔大小
SizeF imageSize = new SizeF(45, 35);
//執行個體化PdfGridCellContentList、PdfGridCellContent類,加載需要添加到嵌套表格的圖檔
PdfGridCellContentList contentList = new PdfGridCellContentList();
PdfGridCellContent content = new PdfGridCellContent();
content.Image = PdfImage.FromFile("1.png");
content.ImageSize = imageSize;
contentList.List.Add(content);
//執行個體化PdfStringFormat、PdfTrueTypeFont類,設定單元格文字對齊方式
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
//添加文本内容及圖檔到嵌套表格
newRow.Cells[0].Value = "Norway";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = contentList; //将圖檔添加到嵌套表格的第二個單元格
newRow.Cells[1].StringFormat = stringFormat;
步驟 6 :添加資料到第一個表格
//設定第一個表格的單元格的值和格式
row1.Cells[0].Value = "Rank";
row1.Cells[0].StringFormat = stringFormat;
row1.Cells[0].Style.Font = font;
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[1].Value = "Country";
row1.Cells[1].StringFormat = stringFormat;
row1.Cells[1].Style.Font = font;
row1.Cells[1].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[2].Value = "Total";
row1.Cells[2].StringFormat = stringFormat;
row1.Cells[2].Style.Font = font;
row1.Cells[2].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row2.Cells[0].Value = "1";
row2.Cells[0].StringFormat = stringFormat;
row2.Cells[0].Style.Font = font;
row2.Cells[1].Value = embedGrid1; //将嵌套表格添加到第一個表格的第二行第二個單元格
row2.Cells[1].StringFormat = stringFormat;
row2.Cells[2].Value = "39";
row2.Cells[2].StringFormat = stringFormat;
row2.Cells[2].Style.Font = font;
步驟 7:将表格繪制到頁面指定位置
grid.Draw(page, new PointF(30f, 90f));
步驟 8 :儲存文檔
pdf.SaveToFile("result.pdf");
完成代碼後,調試程式,生成文檔。繪制的表格如下:
全部代碼:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;
using System.Windows.Forms;
using System;
namespace NestedTable_PDF
{
class Program
{
static void Main(string[] args)
{
//執行個體化PdfDocument類,并添加頁面到建立的文檔
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
//添加字型、畫筆,寫入文本到PDF文檔
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("行楷", 11f), true);
PdfPen pen = new PdfPen(Color.Gray);
string text = "2018 Pyeongchang Olympic Winter Games Medal Ranking";
page.Canvas.DrawString(text, font, pen, 100, 50);
//建立一個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(3);
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 150f;
grid.Columns[2].Width = 120f;
//建立一個一行兩列的嵌套表格
PdfGrid embedGrid1 = new PdfGrid();
PdfGridRow newRow = embedGrid1.Rows.Add();
embedGrid1.Columns.Add(2);
//設定嵌套表格的列寬
embedGrid1.Columns[0].Width = 50f;
embedGrid1.Columns[1].Width = 60f;
//初始化SizeF類,設定圖檔大小
SizeF imageSize = new SizeF(45, 35);
//執行個體化PdfGridCellContentList、PdfGridCellContent類,加載需要添加到嵌套表格的圖檔
PdfGridCellContentList contentList = new PdfGridCellContentList();
PdfGridCellContent content = new PdfGridCellContent();
content.Image = PdfImage.FromFile("1.png");
content.ImageSize = imageSize;
contentList.List.Add(content);
//執行個體化PdfStringFormat、PdfTrueTypeFont類,設定單元格文字對齊方式
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
//添加文本内容及圖檔到嵌套表格
newRow.Cells[0].Value = "Norway";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = contentList; //将圖檔添加到嵌套表格的第二個單元格
newRow.Cells[1].StringFormat = stringFormat;
//設定第一個表格的單元格的值和格式
row1.Cells[0].Value = "Rank";
row1.Cells[0].StringFormat = stringFormat;
row1.Cells[0].Style.Font = font;
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[1].Value = "Country";
row1.Cells[1].StringFormat = stringFormat;
row1.Cells[1].Style.Font = font;
row1.Cells[1].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[2].Value = "Total";
row1.Cells[2].StringFormat = stringFormat;
row1.Cells[2].Style.Font = font;
row1.Cells[2].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row2.Cells[0].Value = "1";
row2.Cells[0].StringFormat = stringFormat;
row2.Cells[0].Style.Font = font;
row2.Cells[1].Value = embedGrid1; //将嵌套表格添加到第一個表格的第二行第二個單元格
row2.Cells[1].StringFormat = stringFormat;
row2.Cells[2].Value = "39";
row2.Cells[2].StringFormat = stringFormat;
row2.Cells[2].Style.Font = font;
//将表格繪制到頁面指定位置
grid.Draw(page, new PointF(30f, 90f));
//儲存文檔并打開
pdf.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");
}
}
}
View Code
以上是本次C#在PDF中繪制嵌套表格的全部内容。
更多關于在PDF中繪制的表格的方法,請參閱以下示例:
- C# 繪制PDF表格
- C# 插入圖檔到PDF表格
(本文完)