zoukankan      html  css  js  c++  java
  • C# 如何在PDF文档中创建表格

    表格能够直观的传达数据信息,使信息显得条理化,便于阅读同时也利于管理。那在PDF类型的文档中如何来添加表格并且对表格进行格式化操作呢?使用常规方法直接在PDF中添加表格行不通,那我们可以在借助第三方组件的情况下来实现。本篇文章中将介绍如何正确使用组件Free Spire.PDF for .NET添加表格到PDF。该组件提供了两个类PdfTable和PdfGrid用于创建表格,在进行代码编辑前,需先安装,添加Spire.PDF. dll到项目程序集中,同时添加到命名空间。下面是两种方法来添加表格的全部代码,供参考。

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

     

    PdfTable

    PdfGrid

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

    可直接通过API设置

    可直接通过API设置(StringFormat)

    可直接通过API设置(StringFormat)

    单元格

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

    可直接通过API设置

    单元格纵向合并

    不支持

    可直接通过API设置

    单元格横向合并

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

    可直接通过API设置

    嵌套表格

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

    可直接通过API设置

    事件

    BeginCellLayout, BeginPageLayout, BeginRowLayout, EndCellLayout, EndPageLayout, EndRowLayout

    BeginPageLayout, EndPageLayout

    一、通过PdfTable类来创建表格

     1 using System.Drawing;
     2 using Spire.Pdf;
     3 using Spire.Pdf.Tables;
     4 using Spire.Pdf.Graphics;
     5 using System.Data;
     6 
     7 namespace DrawTable1_PDF
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //创建一个PdfDocument类对象并向文档新添加一页
    14             PdfDocument doc = new PdfDocument();
    15             PdfPageBase page = doc.Pages.Add();
    16 
    17             //创建一个PdfTable对象
    18             PdfTable table = new PdfTable();
    19             //设置字体
    20             table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
    21             table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
    22 
    23             //创建一个DataTable并写入数据
    24             DataTable dataTable = new DataTable();
    25             dataTable.Columns.Add("产品类型");
    26             dataTable.Columns.Add("产品编号");
    27             dataTable.Columns.Add("采购数额(件)");
    28             dataTable.Columns.Add("所属月份");
    29 
    30             dataTable.Rows.Add(new string[] { "A", "00101", "35", "7月"});
    31             dataTable.Rows.Add(new string[] { "B", "00102", "56", "8月"});
    32             dataTable.Rows.Add(new string[] { "C", "00103", "25", "9月"});
    33 
    34             //填充数据到PDF表格
    35             table.DataSource = dataTable;
    36             //显示表头(默认不显示)
    37             table.Style.ShowHeader = true;
    38             //在BeginRowLayout事件处理方法中注册自定义事件
    39             table.BeginRowLayout += Table_BeginRowLayout;
    40 
    41             //将表格绘入PDF并指定位置和大小
    42             table.Draw(page, new RectangleF(0, 60, 200, 200));
    43 
    44             //保存到文档并预览
    45             doc.SaveToFile("PDF表格_1.pdf");
    46             System.Diagnostics.Process.Start("PDF表格_1.pdf");
    47         }
    48 
    49         //在自定义事件中设置行高
    50         private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
    51         {
    52             args.MinimalHeight = 10f;
    53         }
    54     }
    55 }

    运行程序生成文件(可在该项目文件下bin>Debug查看)

    效果展示:

    二、通过PdfGrid类来添加表格

      1 using Spire.Pdf;
      2 using System.Drawing;
      3 using Spire.Pdf.Grid;
      4 using Spire.Pdf.Graphics;
      5 using Spire.Pdf.Tables;
      6 
      7 namespace DrawTable_PDF
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             //创建一个PdfDocument类对象,并新添加一页到PDF文档
     14             PdfDocument doc = new PdfDocument();
     15             PdfPageBase page = doc.Pages.Add();
     16 
     17             //创建一个PdfGrid对象
     18             PdfGrid grid = new PdfGrid();
     19             //设置单元格边距和表格默认字体
     20             grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
     21             grid.Style.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
     22 
     23             //添加一个5行6列表格到新建的PDF文档
     24             PdfGridRow row1 = grid.Rows.Add();
     25             PdfGridRow row2 = grid.Rows.Add();
     26             PdfGridRow row3 = grid.Rows.Add();
     27             PdfGridRow row4 = grid.Rows.Add();
     28             PdfGridRow row5 = grid.Rows.Add();
     29             grid.Columns.Add(6);
     30 
     31             //设置列宽
     32             foreach (PdfGridColumn col in grid.Columns)
     33             {
     34                 col.Width = 55f;
     35             }
     36 
     37             //写入数据
     38             row1.Cells[0].Value = "新入职员工基本信息";
     39             row2.Cells[0].Value = "入职时间";
     40             row2.Cells[1].Value = "姓名";
     41             row2.Cells[2].Value = "部门";
     42             row2.Cells[3].Value = "学历";
     43             row2.Cells[4].Value = "联系电话";
     44             row2.Cells[5].Value = "正式员工";
     45 
     46             row3.Cells[0].Value = "3月";
     47             row3.Cells[1].Value = "马超";
     48             row3.Cells[2].Value = "研发部";
     49             row3.Cells[3].Value = "硕士";
     50             row3.Cells[4].Value = "153****6543";
     51             row3.Cells[5].Value = "";
     52 
     53             row4.Cells[0].Value = "4月";
     54             row4.Cells[1].Value = "刘陵";
     55             row4.Cells[2].Value = "研发部";
     56             row4.Cells[3].Value = "本科";
     57             row4.Cells[4].Value = "176****5464";
     58             row4.Cells[5].Value = "";
     59 
     60             row5.Cells[0].Value = "4月";
     61             row5.Cells[1].Value = "张丽";
     62             row5.Cells[2].Value = "研发部";
     63             row5.Cells[3].Value = "本科";
     64             row5.Cells[4].Value = "158****4103";
     65             row5.Cells[5].Value = "";
     66 
     67             //水平和垂直方向合并单元格
     68             row1.Cells[0].ColumnSpan = 6;
     69             row4.Cells[0].RowSpan = 2;
     70             row3.Cells[2].RowSpan = 3;
     71             row4.Cells[3].RowSpan = 2;
     72 
     73             //设置单元格内文字对齐方式
     74             PdfTable table = new PdfTable();
     75             row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
     76             row4.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Middle);
     77             row3.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Middle);
     78             row4.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Middle); 
     79            
     80             //设置单元格背景颜色
     81             row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightGreen;
     82 
     83             //设置表格边框颜色、粗细
     84             PdfBorders borders = new PdfBorders();
     85             borders.All = new PdfPen(Color.Black, 0.1f);
     86             foreach (PdfGridRow pgr in grid.Rows)
     87             {
     88                 foreach (PdfGridCell pgc in pgr.Cells)
     89                 {
     90                     pgc.Style.Borders = borders;
     91                 }
     92             }
     93 
     94             //在指定位置绘入表格
     95             grid.Draw(page, new PointF(0, 40));
     96 
     97             //保存到文档
     98             doc.SaveToFile("PDF表格.pdf");
     99             System.Diagnostics.Process.Start("PDF表格.pdf");
    100         }
    101     }
    102 }

    效果展示:

    以上是关于组件Free Spire.PDF for .NET用于在PDF 中创建表格的方法介绍,如对您有所帮助,欢迎转载(转载请注明出处)

  • 相关阅读:
    宿主机( win 7 系统) ping 虚拟机VMware( cent os 6.6 ) 出现“请求超时”或者“无法访问目标主机”的解决方法
    Java实现 LeetCode 23 合并K个排序链表
    Java实现 LeetCode 23 合并K个排序链表
    Java实现 LeetCode 23 合并K个排序链表
    Java实现 LeetCode 22 括号生成
    Java实现 LeetCode 22 括号生成
    Java实现 LeetCode 22 括号生成
    Java实现 LeetCode 21 合并两个有序链表
    Java实现 LeetCode 21 合并两个有序链表
    Java实现 LeetCode 21 合并两个有序链表
  • 原文地址:https://www.cnblogs.com/Yesi/p/8127003.html
Copyright © 2011-2022 走看看