嵌套表格,即在一张表格中的特定单元格中再插入一个或者多个表格,使用嵌套表格的优点在于能够让内容的布局更加合理,同时也方便程序套用。下面的示例中,将介绍如何通过C#编程来演示如何插入嵌套表格到PDF文档。

要点概括:

1. 插入嵌套表格

2. 插入文字到嵌套表格

3. 插入图片到嵌套表格

使用工具

注:

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, , );

步骤 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();
grid.Columns[].Width = 120f;
grid.Columns[].Width = 150f;
grid.Columns[].Width = 120f;

步骤 4 :创建一个嵌套表格

//创建一个一行两列的嵌套表格
PdfGrid embedGrid1 = new PdfGrid();
PdfGridRow newRow = embedGrid1.Rows.Add();
embedGrid1.Columns.Add(); //设置嵌套表格的列宽
embedGrid1.Columns[].Width = 50f;
embedGrid1.Columns[].Width = 60f;

步骤 5 :添加文本、图片到嵌套表格

//初始化SizeF类,设置图片大小
SizeF imageSize = new SizeF(, ); //实例化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[].Value = "Norway";
newRow.Cells[].StringFormat = stringFormat;
newRow.Cells[].Value = contentList; //将图片添加到嵌套表格的第二个单元格
newRow.Cells[].StringFormat = stringFormat;

步骤 6 :添加数据到第一个表格

//设置第一个表格的单元格的值和格式
row1.Cells[].Value = "Rank";
row1.Cells[].StringFormat = stringFormat;
row1.Cells[].Style.Font = font;
row1.Cells[].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[].Value = "Country";
row1.Cells[].StringFormat = stringFormat;
row1.Cells[].Style.Font = font;
row1.Cells[].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[].Value = "Total";
row1.Cells[].StringFormat = stringFormat;
row1.Cells[].Style.Font = font;
row1.Cells[].Style.BackgroundBrush = PdfBrushes.LightSalmon; row2.Cells[].Value = "";
row2.Cells[].StringFormat = stringFormat;
row2.Cells[].Style.Font = font;
row2.Cells[].Value = embedGrid1; //将嵌套表格添加到第一个表格的第二行第二个单元格
row2.Cells[].StringFormat = stringFormat; row2.Cells[].Value = "";
row2.Cells[].StringFormat = stringFormat;
row2.Cells[].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, , ); //创建一个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();
grid.Columns[].Width = 120f;
grid.Columns[].Width = 150f;
grid.Columns[].Width = 120f; //创建一个一行两列的嵌套表格
PdfGrid embedGrid1 = new PdfGrid();
PdfGridRow newRow = embedGrid1.Rows.Add();
embedGrid1.Columns.Add(); //设置嵌套表格的列宽
embedGrid1.Columns[].Width = 50f;
embedGrid1.Columns[].Width = 60f; //初始化SizeF类,设置图片大小
SizeF imageSize = new SizeF(, ); //实例化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[].Value = "Norway";
newRow.Cells[].StringFormat = stringFormat;
newRow.Cells[].Value = contentList; //将图片添加到嵌套表格的第二个单元格
newRow.Cells[].StringFormat = stringFormat; //设置第一个表格的单元格的值和格式
row1.Cells[].Value = "Rank";
row1.Cells[].StringFormat = stringFormat;
row1.Cells[].Style.Font = font;
row1.Cells[].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[].Value = "Country";
row1.Cells[].StringFormat = stringFormat;
row1.Cells[].Style.Font = font;
row1.Cells[].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[].Value = "Total";
row1.Cells[].StringFormat = stringFormat;
row1.Cells[].Style.Font = font;
row1.Cells[].Style.BackgroundBrush = PdfBrushes.LightSalmon; row2.Cells[].Value = "";
row2.Cells[].StringFormat = stringFormat;
row2.Cells[].Style.Font = font;
row2.Cells[].Value = embedGrid1; //将嵌套表格添加到第一个表格的第二行第二个单元格
row2.Cells[].StringFormat = stringFormat; row2.Cells[].Value = "";
row2.Cells[].StringFormat = stringFormat;
row2.Cells[].Style.Font = font; //将表格绘制到页面指定位置
grid.Draw(page, new PointF(30f, 90f)); //保存文档并打开
pdf.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");
}
}
}

以上是本次C#在PDF中绘制嵌套表格的全部内容。

更多关于在PDF中绘制的表格的方法,请参阅以下示例:

(本文完)

最新文章

  1. 弱省互测#0 t1
  2. lecture5-对象识别与卷积神经网络
  3. 解决jetty7.0.pre5启动时报ClassNotFoundException: javax.interceptor.InvocationContext异常的问题
  4. 第二十篇:在SOUI中使用分层窗口
  5. vagrant 启动错误
  6. poj 2507Crossed ladders <计算几何>
  7. 线段树(区间合并) POJ 3667 Hotel
  8. keep alive的相关介绍
  9. ExtJs--15--Ext.is*各种类型推断的方法,简单看源代码就能够明确了
  10. Linux目录树详细说明
  11. HDU1019 Least Common Multiple(多个数的最小公倍数)
  12. python_缩进_格式化代码
  13. 【源码】HashMap源码及线程非安全分析
  14. Docker多步构建更小的Java镜像
  15. SwipeToLoadLayout
  16. Oracle密码过期问题 ORA-28001:the password has expired
  17. error: ‘kEmptyString’ is not a member of ‘google::protobuf::internal’
  18. docker部署安装
  19. 既生list何生tuple
  20. ST3 插件和技巧

热门文章

  1. java的8种基础类型
  2. Electron学习笔记(一)
  3. Flannel工作原理
  4. [译]PEP 342--增强型生成器:协程
  5. SpringCloud学习系列之四-----配置中心(Config)使用详解
  6. 第一次上机,HTML静态网页的开发
  7. Angular(01)-- 架构概览
  8. GIS大数据存储预研
  9. 骁龙735处理器细节曝光:7nm工艺加持,支持5G
  10. 一、redis简单配置