C# 实现绘制PDF嵌套表格案例详解

 更新时间:2021年11月26日 10:26:40   作者:返回主页E-iceblue  
嵌套表格,顾名思义,就是在一张表格中的特定单元格中再插入一个或者多个表格,本文将为大家介绍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# 的资料请关注脚本之家其它相关文章!

相关文章

  • 深入解析C#中的泛型类与泛型接口

    深入解析C#中的泛型类与泛型接口

    这篇文章主要介绍了C#中的泛型类与泛型接口,对泛型的支持是C#语言的重要特性,需要的朋友可以参考下
    2016-02-02
  • C#实现多线程的Web代理服务器实例

    C#实现多线程的Web代理服务器实例

    这篇文章主要介绍了C#实现多线程的Web代理服务器,涉及C#多线程代理服务器的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • c# 连接池的设置与使用

    c# 连接池的设置与使用

    这篇文章主要介绍了c# 连接池的设置与使用,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2021-01-01
  • C# 元组和值元组的具体使用

    C# 元组和值元组的具体使用

    这篇文章主要介绍了C# 元组和值元组的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • C#多线程的相关操作讲解

    C#多线程的相关操作讲解

    本文详细讲解了C#多线程的相关操作,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C#实现的pdf生成图片文字水印类实例

    C#实现的pdf生成图片文字水印类实例

    这篇文章主要介绍了C#实现的pdf生成图片文字水印类,结合完整实例形式分析了C#针对pdf文件的创建、添加文字、水印等相关操作技巧,需要的朋友可以参考下
    2017-09-09
  • C#多线程死锁介绍与案例代码

    C#多线程死锁介绍与案例代码

    这篇文章介绍了C#多线程的死锁,并使用案例代码实现解决方案,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#实现计算年龄的简单方法汇总

    C#实现计算年龄的简单方法汇总

    本文给大家分享的是C#代码实现的简单实用的给出用户的出生日期,计算出用户的年龄的代码,另外附上其他网友的方法,算是对计算年龄的一次小结,希望大家能够喜欢。
    2015-05-05
  • C# 文件下载之断点续传实现代码

    C# 文件下载之断点续传实现代码

    本篇文章主要介绍了C# 文件下载之断点续传实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • WINFORM 窗体间的传值实现解析

    WINFORM 窗体间的传值实现解析

    这篇文章主要介绍了WINFORM 窗体间的传值实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09

最新评论