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 中創建表格的方法介紹,如對您有所幫助,歡迎轉載(轉載請注明出處)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM