如何绘制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#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#和.NET通过不断的更新和优化,适应了新兴技术的需求。1)C#9.0和.NET5引入了记录类型和性能优化。2).NETCore增强了云原生和容器化支持。3)ASP.NETCore与现代Web技术集成。4)ML.NET支持机器学习和人工智能。5)异步编程和最佳实践提升了性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的编程过程包括以下步骤:1)编写C#代码,2)编译为中间语言(IL),3)由.NET运行时(CLR)执行。C#在.NET中的优势在于其现代化语法、强大的类型系统和与.NET框架的紧密集成,适用于从桌面应用到Web服务的各种开发场景。

C#是一种现代、面向对象的编程语言,由微软开发并作为.NET框架的一部分。1.C#支持面向对象编程(OOP),包括封装、继承和多态。2.C#中的异步编程通过async和await关键字实现,提高应用的响应性。3.使用LINQ可以简洁地处理数据集合。4.常见错误包括空引用异常和索引超出范围异常,调试技巧包括使用调试器和异常处理。5.性能优化包括使用StringBuilder和避免不必要的装箱和拆箱。

C#.NET应用的测试策略包括单元测试、集成测试和端到端测试。1.单元测试确保代码的最小单元独立工作,使用MSTest、NUnit或xUnit框架。2.集成测试验证多个单元组合的功能,常用模拟数据和外部服务。3.端到端测试模拟用户完整操作流程,通常使用Selenium进行自动化测试。

C#高级开发者面试需要掌握异步编程、LINQ、.NET框架内部工作原理等核心知识。1.异步编程通过async和await简化操作,提升应用响应性。2.LINQ以SQL风格操作数据,需注意性能。3..NET框架的CLR管理内存,垃圾回收需谨慎使用。

C#.NET面试问题和答案包括基础知识、核心概念和高级用法。1)基础知识:C#是微软开发的面向对象语言,主要用于.NET框架。2)核心概念:委托和事件允许动态绑定方法,LINQ提供强大查询功能。3)高级用法:异步编程提高响应性,表达式树用于动态代码构建。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

Atom编辑器mac版下载
最流行的的开源编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中