如何绘制PDF嵌套表格?本篇文章就给大家详细介绍绘制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#如何绘制PDF嵌套表格?绘制PDF嵌套表格的步骤的详细内容。更多信息请关注PHP中文网其他相关文章!

C#.NET生态系统提供了丰富的框架和库,帮助开发者高效构建应用。1.ASP.NETCore用于构建高性能Web应用,2.EntityFrameworkCore用于数据库操作。通过理解这些工具的使用和最佳实践,开发者可以提高应用的质量和性能。

如何将C#.NET应用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。1.在Azure上,使用AzureAppService和AzurePipelines自动化部署。2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda实现部署和无服务器计算。

C#和.NET的结合为开发者提供了强大的编程环境。1)C#支持多态性和异步编程,2).NET提供跨平台能力和并发处理机制,这使得它们在桌面、Web和移动应用开发中广泛应用。

.NETFramework是一个软件框架,C#是一种编程语言。1..NETFramework提供库和服务,支持桌面、Web和移动应用开发。2.C#设计用于.NETFramework,支持现代编程功能。3..NETFramework通过CLR管理代码执行,C#代码编译成IL后由CLR运行。4.使用.NETFramework可快速开发应用,C#提供如LINQ的高级功能。5.常见错误包括类型转换和异步编程死锁,调试需用VisualStudio工具。

C#是一种由微软开发的现代、面向对象的编程语言,.NET是微软提供的开发框架。C#结合了C 的性能和Java的简洁性,适用于构建各种应用程序。.NET框架支持多种语言,提供垃圾回收机制,简化内存管理。

C#和.NET运行时紧密合作,赋予开发者高效、强大且跨平台的开发能力。1)C#是一种类型安全且面向对象的编程语言,旨在与.NET框架无缝集成。2).NET运行时管理C#代码的执行,提供垃圾回收、类型安全等服务,确保高效和跨平台运行。

要开始C#.NET开发,你需要:1.了解C#的基础知识和.NET框架的核心概念;2.掌握变量、数据类型、控制结构、函数和类的基本概念;3.学习C#的高级特性,如LINQ和异步编程;4.熟悉常见错误的调试技巧和性能优化方法。通过这些步骤,你可以逐步深入C#.NET的世界,并编写高效的应用程序。

C#和.NET的关系是密不可分的,但它们不是一回事。C#是一门编程语言,而.NET是一个开发平台。C#用于编写代码,编译成.NET的中间语言(IL),由.NET运行时(CLR)执行。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

记事本++7.3.1
好用且免费的代码编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),