C# 实现绘制PDF嵌套表格案例详解
作者:返回主页E-iceblue 发布时间:2023-05-25 11:57:13
标签:C#,PDF,嵌套表格
嵌套表格,即在一张表格中的特定单元格中再插入一个或者多个表格,使用嵌套表格的优点在于能够让内容的布局更加合理,同时也方便程序套用。下面的示例中,将介绍如何通过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");
}
}
}
以上就是C# 实现绘制PDF嵌套表格案例详解的详细内容,更多关于C# 的资料请关注脚本之家其它相关文章!
来源:https://www.cnblogs.com/Yesi/p/9804027.html


猜你喜欢
- 可以用如下方法: 修改AudioYusuStreamOut.cpp,添加方法: void AudioYusuStreamOut::swS2M
- 协同过滤简单来说是利用某兴趣相投、拥有共同经验之群体的喜好来推荐用户感兴趣的信息,个人通过合作的机制给予信息相当程度的回应(如评分)并记录下
- 谷歌官方推出了一种侧滑菜单的实现方式(抽屉效果),即 DrawerLayout,这个类是在Support Library里的,需要加上and
- 本文实例为大家分享了C#导出Excel的具体代码,供大家参考,具体内容如下using System;using System.Collect
- 具体代码如下所示:public class MainActivity extends AppCompatActivity { p
- 1. 前言不知道小伙伴对于日期字段,在项目中都是如何处理的,是单独给每个字段都自定义日期格式还是做全局格式设置?这个我之前啊,是
- 本文研究的主要是SpringMVC中使用Thymeleaf模板引擎的相关内容,具体介绍如下。Thymeleaf提供了一组Spring集成,允
- 前言RecyclerView是我们常用的列表控件,一般来说当Item的数据改变的时候我们需要刷新当前的Item 。如何刷新 RV 的列表?基
- 本文实例讲述了Android开发之多媒体文件获取工具类。分享给大家供大家参考,具体如下:package com.android.ocr.ut
- 本文实例讲述了Android编程基于重力传感器实现横竖屏放向切换功能。分享给大家供大家参考,具体如下:最近项目中用到了vr视频播放,因为自己
- 反编译jar包并修改class文件重新打包这两天碰到一个需求:需要修改一个jar包中的逻辑代码,并且重新打包本来是很简单的问题,但是因为这个
- MainActivity.java package com.zhang.showPhoto;import android.app.Actio
- 结构化查询语言(SQL)是一种标准化的语言,它允许你在数据库上执行操作,如创建项目,读取内容,内容更新和删除条目。SQL是所有可能会使用几乎
- 今天做项目的时候,遇到一个问题,如果我调用某个服务的接口,但是这个服务挂了,同时业务要求这个接口的结果是必须的,那我该怎么办呢,答案是通过h
- 在做asp.net和unity进行http通信的时候,当unity客户端发出表单请求的时候,我要将他要请求的数据以json的格式返回给客户端
- 实例如下:package com.bwsk.modules.weixin.util;import java.util.Random;/**
- 在前后端分离的项目中后端返回的格式一定要友好,不然会对前端的开发人员带来很多的工作量。那么SpringBoot如何做到统一的后端返回格式呢?
- 话不多说,请看代码:package com.lxj.demo;import java.io.BufferedReader;import ja
- Springboot2.x的session和cookie有效期session和cookie的相关区别和联系就不介绍了,这里就记录一下笔记。背
- 前言在看一本关于高性能编程的时候发现 Java8 中关于接口的新特性的介绍,这个特性是真的棒,解决了一个接口中有多个方法,但并不想实现该接口