嵌套表格,即在一张表格中的特定单元格中再插入一个或者多个表格,使用嵌套表格的优点在于能够让内容的布局更加合理,同时也方便程序套用。下面的示例中,将介绍如何通过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嵌套表格的更多相关文章

  1. C# 绘制PDF图形——基本图形、自定义图形、色彩透明度

    引言 在PDF中我们可以通过C#程序代码来添加非常丰富的元素来呈现我们想要表达的内容,如绘制表格.文字,添加图形.图像等等.在本篇文章中,我将介绍如何在PDF中绘制图形,并设置图形属性的操作. 文章中 ...

  2. Python绘制PDF文件~超简单的小程序

    Python绘制PDF文件 项目简介 这次项目很简单,本次项目课,代码不超过40行,主要是使用 urllib和reportlab模块,来生成一个pdf文件. reportlab官方文档 http:// ...

  3. html嵌套表格示例

    常用嵌套表格示例,出自<网页开发手记:HTML+CSS+JavaScript实战详解>   <html>   <head>   <title>嵌套表格布 ...

  4. 跟我一起玩转FineUI之嵌套表格

    最近一直在研究FineUI(http://www.fineui.com/),那么什么是FineUI呢,FineUI是基于 ExtJS 的专业 ASP.NET 控件库.创建 No JavaScript, ...

  5. Ext3.4-EXT之嵌套表格的实现

    其中使用到的"RowExpander.js"为extjs官方示例中自带的. 实现这个嵌套表格要注意两点技巧: 1 提供给外层表格的dataStore的数据源以嵌套数组的形式表示细节 ...

  6. ExtJS中实现嵌套表格

    先看效果: 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  7. Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行

    本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...

  8. PDF提取表格的网页工具——Excalibur

      在之前的文章另类爬虫:从PDF文件中爬取表格数据中,我们知道如何利用Python的camelot模块,通过写Python程序来提取PDF中的表格数据.本文我们将学习如何用更便捷的工具从PDF中提取 ...

  9. elementUI表单嵌套表格并对每行进行校验

    elementUI表单嵌套表格并对每行进行校验 elementUI 表单嵌套表格并进行校验. 目录 效果展示 代码链接 关键代码 完整代码 效果展示 先看看这是不是需要的效果^_^ ​ 如图,Elem ...

随机推荐

  1. 判断decimal 是否为整数

    用了半个小时搞懂了这个问题,基础愁死我了! private static boolean isIntegerValue(BigDecimal decimalVal) { return decimalV ...

  2. Django web框架开发基础-django实现留言板功能

    1.创建项目 cmd  django-admin startpoject cloudms 2.创建APP cmd django-admin startapp msgapp 3.修改settings,T ...

  3. 记录遭遇挖矿程序kthrotlds的失败处理经历

    1 发现问题 在腾讯云上购买了一个centos7的服务器,平时用来练手,偶尔也安装一些程序进行测试,上面安装了mysql和redis,前段时间数据库经常掉线,连不上,到腾讯云后台进行查看,通过服务器实 ...

  4. JS 图片放大镜

    今天练习一个小demo, 从本地读取图片, 然后实现类似淘宝放大镜的效果, 再加两个需求 1 .可以调节缩放比例,默认放大两倍 2 . 图片宽高自适应, 不固定宽高 话不多说先看效果: 原理:1, 右 ...

  5. ASP.NET Core在CentOS上的最小化部署实践

    引言        本文从Linux小白的视角, 在CentOS 7.x服务器上搭建一个Nginx-Powered AspNet Core Web准生产应用. 在开始之前,我们还是重温一下部署原理,正 ...

  6. EIGRP 基础实验

    一.环境准备 1. 软件:GNS3 2. 路由:c7200 二.实验操作 实验要求: 1.掌握EIGRP 的基本配置 2.掌握EIGRP 的通配符掩配置方法 3.掌握EIGRP 的自动汇总特性,理解E ...

  7. 在阿里云服务器windows server2012r iis上部署.net网站

    先说一堆废话:之前在阿里云上租了一个服务器,也配置了相关的环境,然后准备把自己手上的一个小网站挂上去,就按照我的上篇博客记载的方法把发布好的网站发布到服务器的iis上,结果发布之后死活访问不了,始终显 ...

  8. mybatis小结

    mybatis是Apache的一个开源项目ibatis,后由Google管理,目前在github上.MyBatis 是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架. 一.mybatis解决 ...

  9. Java_基础篇(数组的反转)

    数组反转也是Java的基础. 数组反转要求掌握的是: 1).创建一个数组,在内存中申请一块空间. 2).实例化数组. 3).对数组的了解.如:数组的长度,数组的下标,数组的表示方法. 4).数组的交换 ...

  10. cesium 之地图贴地量算工具效果篇(附源码下载)

    前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...