欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 在PDF文檔中創(chuàng)建表格的實(shí)現(xiàn)方法

 更新時間:2017年12月28日 10:19:21   作者:E-iceblue  
表格能夠一目了然的讓用戶看到數(shù)據(jù)信息,使信息顯得有條理化,那么在pdf類型的文檔中如何來添加表格并對表格進(jìn)行格式化操作呢?下面小編給大家?guī)砹薈# 在PDF文檔中創(chuàng)建表格的實(shí)現(xiàn)方法,需要的朋友參考下吧

表格能夠直觀的傳達(dá)數(shù)據(jù)信息,使信息顯得條理化,便于閱讀同時也利于管理。那在PDF類型的文檔中如何來添加表格并且對表格進(jìn)行格式化操作呢?使用常規(guī)方法直接在PDF中添加表格行不通,那我們可以在借助第三方組件的情況下來實(shí)現(xiàn)。本篇文章中將介紹如何正確使用組件Free Spire.PDF for .NET添加表格到PDF。該組件提供了兩個類PdfTable和PdfGrid用于創(chuàng)建表格,在進(jìn)行代碼編輯前,需先安裝,添加Spire.PDF. dll到項(xiàng)目程序集中,同時添加到命名空間。下面是兩種方法來添加表格的全部代碼,供參考。

兩種類用于創(chuàng)建表格的異同:

 

PdfTable

PdfGrid

無API支持,可通過事件設(shè)置

可直接通過API設(shè)置

可直接通過API設(shè)置(StringFormat)

可直接通過API設(shè)置(StringFormat)

單元格

無API支持,可通過事件設(shè)置

可直接通過API設(shè)置

單元格縱向合并

不支持

可直接通過API設(shè)置

單元格橫向合并

無API支持,可通過事件設(shè)置

可直接通過API設(shè)置

嵌套表格

無API支持,可通過事件設(shè)置

可直接通過API設(shè)置

事件

BeginCellLayout, BeginPageLayout, BeginRowLayout, EndCellLayout, EndPageLayout, EndRowLayout

BeginPageLayout, EndPageLayout

一、通過PdfTable類來創(chuàng)建表格

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)
  {
   //創(chuàng)建一個PdfDocument類對象并向文檔新添加一頁
   PdfDocument doc = new PdfDocument();
   PdfPageBase page = doc.Pages.Add();
   //創(chuàng)建一個PdfTable對象
   PdfTable table = new PdfTable();
   //設(shè)置字體
   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);
   //創(chuàng)建一個DataTable并寫入數(shù)據(jù)
   DataTable dataTable = new DataTable();
   dataTable.Columns.Add("產(chǎn)品類型");
   dataTable.Columns.Add("產(chǎn)品編號");
   dataTable.Columns.Add("采購數(shù)額(件)");
   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月"});
   //填充數(shù)據(jù)到PDF表格
   table.DataSource = dataTable;
   //顯示表頭(默認(rèn)不顯示)
   table.Style.ShowHeader = true;
   //在BeginRowLayout事件處理方法中注冊自定義事件
   table.BeginRowLayout += Table_BeginRowLayout;
   //將表格繪入PDF并指定位置和大小
   table.Draw(page, new RectangleF(0, 60, 200, 200));
   //保存到文檔并預(yù)覽
   doc.SaveToFile("PDF表格_1.pdf");
   System.Diagnostics.Process.Start("PDF表格_1.pdf");
  }
  //在自定義事件中設(shè)置行高
  private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
  {
   args.MinimalHeight = 10f;
  }
 }
}

運(yùn)行程序生成文件(可在該項(xiàng)目文件下bin>Debug查看)

效果展示:

二、通過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)
  {
   //創(chuàng)建一個PdfDocument類對象,并新添加一頁到PDF文檔
   PdfDocument doc = new PdfDocument();
   PdfPageBase page = doc.Pages.Add();
   //創(chuàng)建一個PdfGrid對象
   PdfGrid grid = new PdfGrid();
   //設(shè)置單元格邊距和表格默認(rèn)字體
   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);
   //設(shè)置列寬
   foreach (PdfGridColumn col in grid.Columns)
   {
    col.Width = 55f;
   }
   //寫入數(shù)據(jù)
   row1.Cells[0].Value = "新入職員工基本信息";
   row2.Cells[0].Value = "入職時間";
   row2.Cells[1].Value = "姓名";
   row2.Cells[2].Value = "部門";
   row2.Cells[3].Value = "學(xué)歷";
   row2.Cells[4].Value = "聯(lián)系電話";
   row2.Cells[5].Value = "正式員工";
   row3.Cells[0].Value = "3月";
   row3.Cells[1].Value = "馬超";
   row3.Cells[2].Value = "研發(fā)部";
   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 = "研發(fā)部";
   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 = "研發(fā)部";
   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;
   //設(shè)置單元格內(nèi)文字對齊方式
   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); 
   //設(shè)置單元格背景顏色
   row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightGreen;
   //設(shè)置表格邊框顏色、粗細(xì)
   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");
  }
 }
}

效果展示:

總結(jié)

以上所述是小編給大家介紹的C# 在PDF文檔中創(chuàng)建表格的實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

您可能感興趣的文章:

相關(guān)文章

最新評論