轉帖|使用教程|編輯:李顯亮|2019-08-07 09:56:53.137|閱讀 1063 次
概述:Spire.PDF的PDF API擁有豐富的功能,如安全設置(包括數字簽名)、PDF文本/附件/圖片提取、PDF文件合并/拆分、元數據更新、章節和段落優化、圖形/圖像描繪和插入、表格創建和處理、數據導入等等。本文將介紹如何創建PDF表格并添加圖片到表格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
更多資源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程
Spire.PDF是一個專業的PDF組件,能夠獨立地創建、編寫、編輯、操作和閱讀PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API擁有豐富的功能,如安全設置(包括數字簽名)、PDF文本/附件/圖片提取、PDF文件合并/拆分、元數據更新、章節和段落優化、圖形/圖像描繪和插入、表格創建和處理、數據導入等等。>>下載Spire.PDF最新試用版
Spire.PDF提供了兩種類PdfTable和PdfGrid用于創建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 類創建表格
static void Main(string[] args)
{
//創建一個PDF文檔
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.Rows.Add(new string[] { "張紅", "22", "女" });
dataTable.Rows.Add(new string[] { "王東", "25", "男" });
//填充數據到PDF表格
table.DataSource = dataTable;
//顯示表頭(默認不顯示)
table.Style.ShowHeader = true;
//在BeginRowLayout事件處理方法中注冊自定義事件
table.BeginRowLayout += Table_BeginRowLayout;
//將表格繪入PDF并指定位置和大小
table.Draw(page, new RectangleF(0, 20, 200, 90));
//保存到文檔
doc.SaveToFile("PDF表格_1.pdf");
}
//在自定義事件中設置行高
private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
args.MinimalHeight = 20f;
}
通過 PdfGrid 創建表格——創建一個簡單的表格
//創建一個PDF文檔
PdfDocument doc = new PdfDocument();
//添加一頁
PdfPageBase page = doc.Pages.Add();
//創建一個PdfGrid對象
PdfGrid grid = new PdfGrid();
//設置單元格邊距
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
//添加2行4列
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
grid.Columns.Add(4);
//設置列寬
foreach (PdfGridColumn col in grid.Columns)
{
col.Width = 60f;
}
//寫入數據
for (int i = 0; i < grid.Columns.Count; i++)
{
row1.Cells[i].Value = String.Format("col{0}", i + 1);
row2.Cells[i].Value = String.Format("{0}", i + 1);
}
//將表格繪入文檔
grid.Draw(page, new PointF(0, 20));
//保存到文檔
doc.SaveToFile("PDF表格_2.pdf");
通過 PdfGrid 創建表格——合并單元格,設置背景色和文字對齊方式
//創建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);
//添加4行4列
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();
PdfGridRow row4 = grid.Rows.Add();
grid.Columns.Add(4);
//設置列寬
foreach (PdfGridColumn col in grid.Columns)
{
col.Width = 60f;
}
//寫入數據
row1.Cells[0].Value = "訂單及支付情況";
row2.Cells[0].Value = "訂單號";
row2.Cells[1].Value = "日期";
row2.Cells[2].Value = "客戶";
row2.Cells[3].Value = "是否付款";
row3.Cells[0].Value = "00223";
row3.Cells[1].Value = "2016/06/02";
row3.Cells[2].Value = "陽光地產";
row3.Cells[3].Value = "是";
row4.Cells[0].Value = "00224";
row4.Cells[1].Value = "2016/06/03";
row4.Cells[3].Value = "否";
//水平和垂直合并單元格
row1.Cells[0].ColumnSpan = 4;
row3.Cells[2].RowSpan = 2;
//設置單元格內文字對齊方式
row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
row3.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Middle);
//設置單元格背景顏色
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Gray;
row3.Cells[3].Style.BackgroundBrush = PdfBrushes.Green;
row4.Cells[3].Style.BackgroundBrush = PdfBrushes.MediumVioletRed;
//設置邊框顏色、粗細
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, 20));
//保存到文檔
doc.SaveToFile("PDF表格_3.pdf");
//創建PDF文檔
PdfDocument pdf = new PdfDocument();
//添加一個頁面
PdfPageBase page = pdf.Pages.Add();
//創建一個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(2);
//設置列寬
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 120f;
//創建另一個需要嵌套的表格
PdfGrid embedGrid = new PdfGrid();
//添加一行
PdfGridRow newRow = embedGrid.Rows.Add();
//添加兩列
embedGrid.Columns.Add(2);
//設置列寬
embedGrid.Columns[0].Width = 50f;
embedGrid.Columns[1].Width = 50f;
SizeF imageSize = new SizeF(50, 50);
//加載圖片
PdfGridCellContentList contentList = new PdfGridCellContentList();
PdfGridCellContent content = new PdfGridCellContent();
content.Image = PdfImage.FromFile(@"Doc.png");
content.ImageSize = imageSize;
contentList.List.Add(content);
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);
//設置嵌套表格的單元格的值和格式
newRow.Cells[0].Value = "Spire.Doc";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = contentList; //將圖片添加到嵌套表格的第二個單元格
newRow.Cells[1].StringFormat = stringFormat;
//設置第一個表格的單元格的值和格式
row1.Cells[0].Value = "客戶姓名";
row1.Cells[0].StringFormat = stringFormat;
row1.Cells[0].Style.Font = font;
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.ForestGreen;
row1.Cells[1].Value = "產品";
row1.Cells[1].StringFormat = stringFormat;
row1.Cells[1].Style.Font = font;
row1.Cells[1].Style.BackgroundBrush = PdfBrushes.ForestGreen;
row2.Cells[0].Value = "肖恩";
row2.Cells[0].StringFormat = stringFormat;
row2.Cells[0].Style.Font = font;
row2.Cells[1].Value = embedGrid; //將嵌套表格添加到第一個表格的第二行第二個單元格
row2.Cells[1].StringFormat = stringFormat;
//將第一個表格畫到頁面上
grid.Draw(page, new PointF(0f, 30f));
//保存文檔
pdf.SaveToFile("嵌套表格和圖片.pdf");
//創建PdfDocument實例
PdfDocument doc = new PdfDocument();
//添加一頁
PdfPageBase page = doc.Pages.Add();
//創建PDF grid
PdfGrid grid = new PdfGrid();
//設置單元格內容和邊框之間的距離
grid.Style.CellPadding = new PdfPaddings(3, 3, 1, 1);
//添加3行和4列
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();
grid.Columns.Add(4);
//設置列寬
foreach (PdfGridColumn column in grid.Columns)
{
column.Width = 60f;
}
//寫入數據到單元格
for (int i = 0; i < grid.Columns.Count; i++)
{
row1.Cells[i].Value = String.Format("column{0}", i + 1);
row2.Cells[i].Value = "a";
row3.Cells[i].Value = "b";
}
//刪除第二行
grid.Rows.RemoveAt(1);
//刪除第二列
grid.Columns.RemoveAt(1);
//在頁面的指定位置繪制grid
grid.Draw(page, new PointF(0, 20));
//保存文件
doc.SaveToFile("Output.pdf");
如果你有任何問題或意見,可在下方評論區留言,點擊資源列表查看更多教程資源~
*想要購買正版授權的朋友可以哦~
掃描關注“慧聚IT”微信公眾號,及時獲取更多產品最新動態及最新資訊

本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn