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 ...
随机推荐
- Text-CNN-文本分类-keras
Text CNN 1. 简介 TextCNN 是利用卷积神经网络对文本进行分类的算法,由 Yoon Kim 在 "Convolutional Neural Networks for Sent ...
- [Active Learning] Multi-Criteria-based Active Learning
目录 1 Informativeness 2 Representativeness 3 Diversity 3.1 Global consideration 3.2 Local considerati ...
- SSRS报表服务随笔(rdl报表服务)-报表数据:使用第三方控件生成条形码
因为工作需要,需要将订单号显示成条形码,比如数据库存储的20190106A,我需要把这个转换为Code128来显示出来 在国内我没有找到这方面的教程,最后还是一个人自己摸索出来的 在这里我是使用的是B ...
- python enumerate() 函数的使用方法
列表是最常用的Python数据类型,前段时间看书的时候,发现了enumerate() 函数非常实用,因为才知道下标可以这么容易的使用,总结一下. class enumerate(object): &q ...
- SpringBoot之旅第二篇-配置
一.引言 虽然springboot帮我们进行了自动配置,但配置还是不可避免的,比如最简单的端口号,数据库连接.但springboot的配置一般不用xml进行配置,而是yml和properties,选择 ...
- Promise, Generator, async/await的渐进理解
作为前端开发者的伙伴们,肯定对Promise,Generator,async/await非常熟悉不过了.Promise绝对是烂记于心,而async/await却让使大伙们感觉到爽(原来异步可以这么简单 ...
- asp.net core系列 56 IS4使用OpenID Connect添加用户认证
一.概述 在前二篇中讲到了客户端授权的二种方式: GrantTypes.ClientCredentials凭据授权和GrantTypes.ResourceOwnerPassword密码授权,都是OAu ...
- C++删除文件末尾字符
C++中使用fstream来进行文件读写,如果要覆盖文件末尾的部分字符,应该怎么操作呢? #include <iostream> #include <fstream> std: ...
- C#-Xamarin的Android项目开发(三)——发布、部署、打包
前言 部署,通常的情况下,它其实也是项目开发的一个难点. 为什么这么说呢?因为,它不是代码开发,所以很多开发者本能的拒绝学习它. 并且一个项目配置好一次以后,部署的步骤和部署的人通常很固定,所以大部分 ...
- 第5章 令牌自省端点(Token Introspection Endpoint) - IdentityModel 中文文档(v1.0.0)
OAuth 2.0令牌自省的客户端库是作为HttpClient扩展方法提供的. 以下代码将引用令牌发送到内省端点: var client = new HttpClient(); var respons ...