使用PDF元件[itextsharp]到C#專案 繪製表格
使用PDF元件[itextsharp]到C#專案 繪製表格
資料來源:http://www.c-sharpcorner.com/blogs/create-table-in-pdf-using-c-sharp-and-itextsharp
http://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm
https://dotblogs.com.tw/aquarius6913/2012/07/27/73670
http://www.cnblogs.com/CareySon/archive/2011/11/05/2237116.html
00.去GOOGLE 搜尋itextsharp 並且下載
01.解壓 itextsharp-dll-core.zip
iTextSharp.xml
itextsharp.dll
02.解壓 itextsharp-dll-pdfa.zip
itextsharp.pdfa.dll
03.解壓 itextsharp-dll-xtra.zip
itextsharp.xtra.dll
04.專案引入三個DLL參考
05.在程式碼中加入引用函式庫語法
//PDF_start
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
//PDF_end
06.建立一個按鈕事件並加入測試程式碼
private void button1_Click(object sender, EventArgs e)//基本使用測試
{
iTextSharp.text.Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);//左,右,上,下 邊界
iTextSharp.text.pdf.PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(“test.pdf”, FileMode.Create));
doc.Open();
Paragraph paragraph = new Paragraph(“this is my first line.”);
doc.Add(paragraph);
doc.Close();
}
07. 建立一個按鈕事件並加入測試程式碼
private void button2_Click(object sender, EventArgs e)//顯示中文+顏色
{
//資料來源:http://renjin.blogspot.tw/2009/01/using-chinese-fonts-in-itextsharp.html
//http://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm
iTextSharp.text.Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);//左,右,上,下 邊界
iTextSharp.text.pdf.PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(“test2.pdf”, FileMode.Create));
iTextSharp.text.pdf.BaseFont bfTW = BaseFont.CreateFont(“01.ttf”, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//01.ttf是微軟正黑體
iTextSharp.text.pdf.BaseFont bfCN = BaseFont.CreateFont(“01.ttf”, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font fontTW = new iTextSharp.text.Font(bfTW, 40, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(51, 0, 153));
iTextSharp.text.Font fontCN = new iTextSharp.text.Font(bfCN, 12, iTextSharp.text.Font.NORMAL, BaseColor.RED);
doc.Open();
Paragraph paragraph = new Paragraph(“this is my first line.”);
doc.Add(paragraph);
doc.Add(new Paragraph(“foolish”, fontTW));
doc.Add(new Paragraph(“難得糊塗”, fontTW));//繁中
doc.Add(new Paragraph(“难得糊涂”, fontCN));//簡中
doc.Close();
}
08. 建立一個按鈕事件並加入測試程式碼
private void button3_Click(object sender, EventArgs e)//顯示中文+表格
{
//資料來源:http://www.c-sharpcorner.com/blogs/create-table-in-pdf-using-c-sharp-and-itextsharp
//http://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm
//https://dotblogs.com.tw/aquarius6913/2012/07/27/73670
//http://www.cnblogs.com/CareySon/archive/2011/11/05/2237116.html
iTextSharp.text.Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);//左,右,上,下 邊界
iTextSharp.text.pdf.PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(“test3.pdf”, FileMode.Create));
iTextSharp.text.pdf.BaseFont bfTW = BaseFont.CreateFont(“01.ttf”, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//01.ttf是微軟正黑體
iTextSharp.text.Font fontTW = new iTextSharp.text.Font(bfTW, 12, iTextSharp.text.Font.NORMAL, BaseColor.RED);
doc.Open();
doc.Add(new Paragraph(“難得糊塗”, fontTW));//繁中
//–
//標準表格
iTextSharp.text.pdf.PdfPTable table1 = new iTextSharp.text.pdf.PdfPTable(3);// 3=每列三欄位
PdfPCell cell_10 = new PdfPCell(new Paragraph(“Row 1, Col 1”, fontTW));
table1.AddCell(cell_10);
table1.AddCell(new Paragraph(“Row 1, Col 2”, fontTW));
table1.AddCell(“Row 1, Col 3”);
table1.AddCell(“Row 2, Col 1”);
table1.AddCell(“Row 2, Col 2”);
table1.AddCell(“Row 2, Col 3”);
table1.AddCell(“Row 3, Col 1”);
table1.AddCell(“Row 3, Col 2”);
table1.AddCell(“Row 3, Col 3”);
doc.Add(table1);
//–
doc.Add(new Paragraph(“\n”));
//–
//表格欄位[同一列]合併
PdfPTable table2 = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase(“Row 1 , Col 1, Col 2 and col 3”));
cell.Colspan = 3;//指定該元件占用的欄位,達到合併欄位的效果
cell.HorizontalAlignment = Element.ALIGN_CENTER;//文字在欄位中要至中顯示
table2.AddCell(cell);
table2.AddCell(“Row 2, Col 1”);
table2.AddCell(“Row 2, Col 1”);
table2.AddCell(“Row 2, Col 1”);
table2.AddCell(“Row 3, Col 1”);
cell = new PdfPCell(new Phrase(“Row 3, Col 2 and Col3”));
cell.Colspan = 2;//指定該元件占用的欄位,達到合併欄位的效果
table2.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 4, Col 1 and Col2”));
cell.Colspan = 2;
table2.AddCell(cell);
table2.AddCell(“Row 4, Col 3”);
doc.Add(table2);
//–
doc.Add(new Paragraph(“\n”));
//–
//表格欄位[同一行]合併
PdfPTable table3 = new PdfPTable(3);
cell = new PdfPCell(new Phrase(“Row 1, Col 1”));
table3.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 1, Col 2”));
table3.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 1, Col 3”));
table3.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 2 , Col 1”));
table3.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 2, Col 2”));
table3.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 2 and Row 3, Col 3”));
cell.Rowspan = 2;//指定該元件占用的欄位,達到合併欄位的效果
table3.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 3, Col 1”));
table3.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 3, Col 2”));
table3.AddCell(cell);
doc.Add(table3);
//–
doc.Add(new Paragraph(“\n”));
//–
//表格欄位[同時合併列和行]合併
PdfPTable table4 = new PdfPTable(3);
cell = new PdfPCell(new Phrase(“Row 1, Col 1”));
table4.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 1, Col 2”));
table4.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 1, Col 3”));
table4.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 2 ,Col 1”));
table4.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 2 and row 3, Col 2 and Col 3”));
cell.Rowspan = 2;
cell.Colspan = 2;
table4.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 3, Col 1”));
table4.AddCell(cell);
doc.Add(table4);
//–
doc.Add(new Paragraph(“\n”));
//–
//表格文字旋轉
PdfPTable table5 = new PdfPTable(3);
cell = new PdfPCell(new Phrase(“Row 1, Col 1”));
table5.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 1, Col 2”));
cell.Rotation = 90;
table5.AddCell(cell);
cell = new PdfPCell(new Phrase(“Row 1, Col 3”));
cell.Rotation = -90;
table5.AddCell(cell);
doc.Add(table5);
//–
doc.Add(new Paragraph(“\n”));
//–
//整合範例
// 產生表格 — START
for (int j = 0; j < 5; j++)
{
PdfPTable pt = new PdfPTable(new float[] { 2, 1, 1, 3 });// 建立4個欄位表格之相對寬度
pt.TotalWidth = 400f;// 表格總寬
pt.LockedWidth = true;
// 塞入資料 — START
// 設定表頭
PdfPCell header = new PdfPCell(new Phrase(“Y2J Header”, fontTW));
header.Colspan = 4;
header.BorderColor = new iTextSharp.text.BaseColor(255, 255, 255);//指定標格邊框顏色
header.HorizontalAlignment = Element.ALIGN_CENTER;// 表頭內文置中
pt.AddCell(header);
pt.AddCell(“R_2/C_1”);
pt.AddCell(“R_2/C_2”);
pt.AddCell(“R_2/C_3”);
pt.AddCell(“R_2/C_4”);
PdfPCell itemname = new PdfPCell(new Phrase(“測試123”, fontTW));
itemname.Colspan = 1;
pt.AddCell(itemname);
PdfPCell content = new PdfPCell(new Phrase(“Y2J_Y2J_Y2J”, fontTW));
content.Colspan = 3;
content.HorizontalAlignment = Element.ALIGN_RIGHT;// 內文靠右
pt.AddCell(content);
PdfPCell rows = new PdfPCell(new Phrase(“ROW_4”, fontTW));
rows.Rowspan = 3;
pt.AddCell(rows);
for (int i = 0; i <= 3; i++)
{
pt.AddCell(“Cell ” + i.ToString() + “1”);
pt.AddCell(“Cell ” + i.ToString() + “2”);
pt.AddCell(“Cell ” + i.ToString() + “3”);
}
//pt.AddCell(“Row 1”);
PdfPCell left = new PdfPCell(new Paragraph(“Y2J:90”));
left.Rotation = 90;
pt.AddCell(left);
PdfPCell row = new PdfPCell(new Phrase(“合併3行3列”, fontTW));
row.Rowspan = 3;
row.Colspan = 3;
pt.AddCell(row);
// Rotation文字翻轉屬性,需為 90 倍數,要不然會出錯
PdfPCell middle_left = new PdfPCell(new Paragraph(“Y2J:180”));
middle_left.Rotation = 180;
pt.AddCell(middle_left);
PdfPCell middle_right = new PdfPCell(new Paragraph(“Y2J:270”));
middle_right.Rotation = 270; // -90跟270是相同
pt.AddCell(middle_right);
PdfPCell right = new PdfPCell(new Paragraph(“Y2J:360”));
right.Rotation = 360; // 360為預設,可不寫
pt.AddCell(right);
doc.Add(pt);
doc.Add(new Paragraph(“\n”));
}
//–
MessageBox.Show(“OK”);
doc.Close();
}