C#怎样绘制PDF嵌套表格?绘制PDF嵌套表格的步骤【C#.Net教程】,C#,PDF,嵌套表格,绘制表格
作者:搜教程发布时间:2019-11-27分类:.Net教程浏览:46评论:0
导读:怎样绘制PDF嵌套表格?本篇文章就给人人细致引见绘制PDF嵌套表格的步骤。有肯定的参考价值,有须要的朋侪可以参考一下,愿望对你们有所助。嵌套表格,即在一张表格中的特定单元格...
怎样绘制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#视频教程
C#开辟图文教程
bootstrap视频教程
(本文完)
以上就是C#怎样绘制PDF嵌套表格?绘制PDF嵌套表格的步骤的细致内容,更多请关注ki4网别的相干文章!
相关推荐
- Icecream PDF Editor如何提取PDF文件页面?PDF页面提取 完美教程文章资讯
- C#对XML读写的代码实例【XML教程】,C#,XML读写
- C#中经由过程xpath查找xml的指定元素的代码实例【XML教程】,C#,xpath,xml
- C#怎样盘算2个字符串类似度的示例代码分享【C#.Net教程】,C#,字符串,相似度
- 详解C#罕见运用函数的实例总结【C#.Net教程】,C#,应用函数
- C#剖析XML文件的代码实例分享【C#.Net教程】,C#,XML
- C# 怎样设置体系的默许打印机的简朴代码示例【C#.Net教程】,C#,打印机
- 详解C#程序员开辟WinForm必需晓得的Window音讯大全的示例代码【C#.Net教程】,C#,WinForm,Window
- C# HttpHandler 异步监听要求的代码详解【C#.Net教程】,C# ,HttpHandler ,异步监听
- C# Json 序列化与反序列化二【C#.Net教程】,C#,序列化,反序列化
你 发表评论:
欢迎- .Net教程排行
-
- 1案例分享c++ map的运用和 查找机能测试【C#.Net教程】,性能,map,c++
- 2细致引见C# string花样的日期时候字符串转为DateTime范例的要领【C#.Net教程】,C#,string,DateTime
- 3c#怎样运用?c#的基础语法【C#.Net教程】,c#,关键字
- 4详解ASP.NET中衔接数据库设置要领【C#.Net教程】,ASP.NET,数据库,配置
- 5C# DataSet机能最好实践【C#.Net教程】,C#,DataSet
- 6.net和c#有什么区别【C#.Net教程】,.net,c#
- 7C#_挪用封装的一个类完成导出Excel表格的功用【C#.Net教程】,C# Excel表格
- 8asp .net 面试题及答案分享【C#.Net教程】,.net,自己,整理,问题,面试
- 9让WebAPI 返回JSON花样的数据实例教程【C#.Net教程】,javascript,WebAPI,JSON,api,web,搭建,返回
- 最新文章
- 广而告之