为了账号安全,请及时绑定邮箱和手机立即绑定

C# 在PDF中绘制表格、嵌套表格

标签:
C# .NET

表格能够直观的传达数据信息,使信息显得条理化,便于阅读同时也利于数据管理。本篇文章中将介绍如何在PDF文档中添加表格到。包括:

  1. 添加常规的表格:涉及表格格式化设置,如单元格合并、单元格背景色填充、表格边框设置,单元格对齐方式

  2. 添加嵌套表格:在单元格中插入图片、添加文字

 

使用工具:Spire.PDF for .NET

在进行代码编辑前,请先添加Spire.Pdf. dll到项目程序集中,同时添加相应的命名空间。

 

注: 该组件提供了两个类PdfTable和PdfGrid用于创建表格。下面是两种方法来添加表格的全部代码,供参考。

两种类用于创建表格的异同:


 

PdfTable

PdfGrid

无API支持,可通过事件设置

可直接通过API设置

可直接通过API设置(StringFormat)

可直接通过API设置(StringFormat)

单元格

无API支持,可通过事件设置

可直接通过API设置

单元格纵向合并

不支持

可直接通过API设置

单元格横向合并

无API支持,可通过事件设置

可直接通过API设置

嵌套表格

无API支持,可通过事件设置

可直接通过API设置

事件

BeginCellLayout,   BeginPageLayout, BeginRowLayout, EndCellLayout, EndPageLayout, EndRowLayout

BeginPageLayout,   EndPageLayout


【示例1】通过PdfTable类来创建表格

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Tables;
using Spire.Pdf.Graphics;
using System.Data; 

namespace DrawTable1_PDF
{    
  class Program    
    {        
       static void Main(string[] args)        
         {            
            //创建一个PdfDocument类对象并向文档新添加一页            
            PdfDocument doc = new PdfDocument();            
            PdfPageBase page = doc.Pages.Add(); 
                        
            //创建一个PdfTable对象            
            PdfTable table = new PdfTable();            
            //设置字体            
            table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);            
            table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);             
            //创建一个DataTable并写入数据            
            DataTable dataTable = new DataTable();            
            dataTable.Columns.Add("产品类型");            
            dataTable.Columns.Add("产品编号");            
            dataTable.Columns.Add("采购数额(件)");            
            dataTable.Columns.Add("所属月份");             
            dataTable.Rows.Add(new string[] { "A", "00101", "35", "7月"});            
            dataTable.Rows.Add(new string[] { "B", "00102", "56", "8月"});            
            dataTable.Rows.Add(new string[] { "C", "00103", "25", "9月"});             
            
            //填充数据到PDF表格            
            table.DataSource = dataTable;            
            //显示表头(默认不显示)            
            table.Style.ShowHeader = true;            
            
            //在BeginRowLayout事件处理方法中注册自定义事件            
            table.BeginRowLayout += Table_BeginRowLayout;             
            
            //将表格绘入PDF并指定位置和大小            
            table.Draw(page, new RectangleF(0, 60, 200, 200));             
            
            //保存到文档并预览            
            doc.SaveToFile("PDF表格_1.pdf");            
            System.Diagnostics.Process.Start("PDF表格_1.pdf");        
          }        
        
        //在自定义事件中设置行高        
        private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)        
          {            
            args.MinimalHeight = 10f;                   
          }   
      }
 }

运行程序生成文档,如下图

https://img1.sycdn.imooc.com//5ce3a75a0001926606760312.jpg

【示例2】通过PdfGrid类来添加表格

using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Grid;
using Spire.Pdf.Graphics;
using Spire.Pdf.Tables; 
namespace DrawTable_PDF
{    
  class Program    
    {        
       static void Main(string[] args)        
         {            
         //创建一个PdfDocument类对象,并新添加一页到PDF文档            
         PdfDocument doc = new PdfDocument();            
         PdfPageBase page = doc.Pages.Add();             
         
         //创建一个PdfGrid对象            
         PdfGrid grid = new PdfGrid();            
         
         //设置单元格边距和表格默认字体            
         grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);            
         grid.Style.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);             
         
         //添加一个5行6列表格到新建的PDF文档            
         PdfGridRow row1 = grid.Rows.Add();            
         PdfGridRow row2 = grid.Rows.Add();            
         PdfGridRow row3 = grid.Rows.Add();            
         PdfGridRow row4 = grid.Rows.Add();            
         PdfGridRow row5 = grid.Rows.Add();            
         grid.Columns.Add(6);             
         //设置列宽            
         foreach (PdfGridColumn col in grid.Columns)            
          {                
             col.Width = 55f;            
           }             
           
           //写入数据            
           row1.Cells[0].Value = "新入职员工基本信息";            
           row2.Cells[0].Value = "入职时间";            
           row2.Cells[1].Value = "姓名";            
           row2.Cells[2].Value = "部门";            
           row2.Cells[3].Value = "学历";            
           row2.Cells[4].Value = "联系电话";            
           row2.Cells[5].Value = "正式员工";             
           row3.Cells[0].Value = "3月";            
           row3.Cells[1].Value = "马超";            
           row3.Cells[2].Value = "研发部";            
           row3.Cells[3].Value = "硕士";            
           row3.Cells[4].Value = "153****6543";            
           row3.Cells[5].Value = "是";             
           row4.Cells[0].Value = "4月";            
           row4.Cells[1].Value = "刘陵";            
           row4.Cells[2].Value = "研发部";            
           row4.Cells[3].Value = "本科";            
           row4.Cells[4].Value = "176****5464";            
           row4.Cells[5].Value = "是";             
           row5.Cells[0].Value = "4月";            
           row5.Cells[1].Value = "张丽";            
           row5.Cells[2].Value = "研发部";            
           row5.Cells[3].Value = "本科";            
           row5.Cells[4].Value = "158****4103";            
           row5.Cells[5].Value = "是";             
           //水平和垂直方向合并单元格            
           row1.Cells[0].ColumnSpan = 6;            
           row4.Cells[0].RowSpan = 2;            
           row3.Cells[2].RowSpan = 3;            
           row4.Cells[3].RowSpan = 2;             
           //设置单元格内文字对齐方式            
           PdfTable table = new PdfTable();            
           row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);            
           row4.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Middle);            
           row3.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Middle);            
           row4.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Middle);                      
           //设置单元格背景颜色            
           row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightGreen;             
           //设置表格边框颜色、粗细            
           PdfBorders borders = new PdfBorders();            
           borders.All = new PdfPen(Color.Black, 0.1f);            
           foreach (PdfGridRow pgr in grid.Rows)            
           {                
            foreach (PdfGridCell pgc in pgr.Cells)              
             {                    
                pgc.Style.Borders = borders;             
              }            
           }             
           //在指定位置绘入表格            
           grid.Draw(page, new PointF(0, 40));             
           
           //保存到文档            
           doc.SaveToFile("PDF表格.pdf");            
           System.Diagnostics.Process.Start("PDF表格.pdf");     
        } 
    }
}

表格生成效果:

https://img1.sycdn.imooc.com//5ce3a74b00019b7009100362.jpg

【示例3】添加嵌套表格

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");             
     }  
   }
}

嵌套表格添加效果:

https://img1.sycdn.imooc.com//5ce3a70100016e8d07500298.jpg

以上是本篇文章对C# 在PDF 中创建表格的全部内容。更多关于PDF中绘制表格的内容,可点击查看视频教程!

(本文完)


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
9
获赞与收藏
48

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消