C# 绘制PDF嵌套表格
嵌套表格,即在一张表格中的特定单元格中再插入一个或者多个表格,使用嵌套表格的优点在于能够让内容的布局更加合理,同时也方便程序套用。下面的示例中,将介绍如何通过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中绘制的表格的方法,请参阅以下示例:
(本文完)
C# 绘制PDF嵌套表格的更多相关文章
- C# 绘制PDF图形——基本图形、自定义图形、色彩透明度
引言 在PDF中我们可以通过C#程序代码来添加非常丰富的元素来呈现我们想要表达的内容,如绘制表格.文字,添加图形.图像等等.在本篇文章中,我将介绍如何在PDF中绘制图形,并设置图形属性的操作. 文章中 ...
- Python绘制PDF文件~超简单的小程序
Python绘制PDF文件 项目简介 这次项目很简单,本次项目课,代码不超过40行,主要是使用 urllib和reportlab模块,来生成一个pdf文件. reportlab官方文档 http:// ...
- html嵌套表格示例
常用嵌套表格示例,出自<网页开发手记:HTML+CSS+JavaScript实战详解> <html> <head> <title>嵌套表格布 ...
- 跟我一起玩转FineUI之嵌套表格
最近一直在研究FineUI(http://www.fineui.com/),那么什么是FineUI呢,FineUI是基于 ExtJS 的专业 ASP.NET 控件库.创建 No JavaScript, ...
- Ext3.4-EXT之嵌套表格的实现
其中使用到的"RowExpander.js"为extjs官方示例中自带的. 实现这个嵌套表格要注意两点技巧: 1 提供给外层表格的dataStore的数据源以嵌套数组的形式表示细节 ...
- ExtJS中实现嵌套表格
先看效果: 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行
本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...
- PDF提取表格的网页工具——Excalibur
在之前的文章另类爬虫:从PDF文件中爬取表格数据中,我们知道如何利用Python的camelot模块,通过写Python程序来提取PDF中的表格数据.本文我们将学习如何用更便捷的工具从PDF中提取 ...
- elementUI表单嵌套表格并对每行进行校验
elementUI表单嵌套表格并对每行进行校验 elementUI 表单嵌套表格并进行校验. 目录 效果展示 代码链接 关键代码 完整代码 效果展示 先看看这是不是需要的效果^_^ 如图,Elem ...
随机推荐
- 前端教程(1)http协议的深刻理解
一 HTTP协议简介 作为学习前端开发的开始,我们必须搞明白以下几件事 1.什么是互联网 互联网=物理连接介质+互联网协议 2.互联网建立的目的? 数据传输打破地域限制,否则的话,我 ...
- Python3|ddt|unittest|浅议数据驱动测试
目录 1.DDT简介 2.data装饰器 3.unpack装饰器 4.file_data装饰器 5.总结 1.DDT简介 Data-Driven Tests(DDT)即数据驱动测试.它允许您通过不同的 ...
- 为什么VUE注册组件命名时不能用大写的?
这段时间一直在弄vue,当然也遇到很多问题,这里就来跟大家分享一些注册自定义模板组件的心得 首先"VUE注册组件命名时不能用大写"其实这句话是不对的,但我们很多人开始都觉得是对的, ...
- headfirst设计模式(9)—模板方法模式
前言 这一章的模板方法模式,个人感觉它是一个简单,并且实用的设计模式,先说说它的定义: 模板方法模式定义了一个算法的步骤,并允许子类别为一个或多个步骤提供其实践方式.让子类别在不改变算法架构的情况下, ...
- CSS引入本地字体与在线字体
有些时候为了强调某些文字,需要使用一些比较特别的字体,CSS中现在也可以比较方便的引入字体了,如下: /* 定义字体 */ @font-face{ font-family: Arista2; src: ...
- ArcGIS JS Api 4.x修改三维球背景技巧
通过修改scenceview.js中tileBackground和defaultTileBackground中的png的base64编码就可以达到要求. 4.8中通过修改scenceview. ...
- Redis入门教程(二)
推荐阅读: Redis入门教程(一)https://www.cnblogs.com/jichi/p/10285346.html 5. Redis 的数据结构 5.1 Redis 数据结构介绍 redi ...
- windows 无法启动网络发现
1.先启动三个服务,按顺序启动(有依存关系),都改为自启动 Function Discovery Resource Publication SSDP Discovery UPnP Device Hos ...
- 搭建环境-Monkeyrunner-自动化测试工具
这篇博客帮助挺大,我补充部分,帮助同样的小白哈哈,侵删 https://www.cnblogs.com/lynn-li/p/5885001.html 1.前期准备 需要安装:JDK,SDK,pytho ...
- [题解] 2038: [2009国家集训队]小Z的袜子(hose)
莫队,卡常数 题目地址 思路 设\(\text{Vis[i]}\)为元素\(\text{i}\)在区间\(\text{[L,R]}\)的出现次数 考虑区间\(\text{[L,R]}\)和元素\(\t ...