温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#在PDF文档中如何创建表格

发布时间:2021-07-12 10:12:28 来源:亿速云 阅读:107 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关C#在PDF文档中如何创建表格的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

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

 

PdfTable

PdfGrid

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

可直接通过API设置

可直接通过API设置(StringFormat)

可直接通过API设置(StringFormat)

单元格

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

可直接通过API设置

单元格纵向合并

不支持

可直接通过API设置

单元格横向合并

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

可直接通过API设置

嵌套表格

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

可直接通过API设置

事件

BeginCellLayout, BeginPageLayout, BeginRowLayout, EndCellLayout, EndPageLayout, EndRowLayout

BeginPageLayout, EndPageLayout

一、通过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;
  }
 }
}

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

效果展示:

C#在PDF文档中如何创建表格

二、通过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");
  }
 }
}

效果展示:

C#在PDF文档中如何创建表格

感谢各位的阅读!关于“C#在PDF文档中如何创建表格”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI