文章主要介紹使用ITextSharp導出PDF表格和圖片的簡單操作說明,以下為ITextSharp.dll下載鏈接
分享鏈接:http://pan.baidu.com/s/1nuc6glj 密碼:3gxw
一、流程
二、簡單實例:
1)創建表格實例
程序:
/// <summary> /// 創建PDF文件 /// </summary> private void CreatPDFTable() { //打印PDF表格 string pdfname = string.Empty; SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = "PDF表格"; dlg.DefaultExt = ".pdf"; dlg.Filter = "Text documents (.pdf)|*.pdf"; if (dlg.ShowDialog() == DialogResult.OK) { pdfname = dlg.FileName; FileStream fs = new FileStream(pdfname, FileMode.Create); //創建文件流 Document document = new Document(PageSize.A7.Rotate()); //創建文件 PageSize.A7.Rotate()表示A7紙橫向輸出 PdfWriter pdfWriter = PdfWriter.GetInstance(document, fs); //實例化 document.Open(); //打開文件 document.Add(new Paragraph("1")); document.Add(PDFTable1()); //添加表格 document.SetPageSize(PageSize.A6); //A6紙縱向輸出 document.NewPage(); //新起一頁 document.Add(new Paragraph("2")); document.Add(PDFTable2()); document.Add(new Paragraph("3")); document.Add(PDFTable3()); document.Close(); //關閉文件 fs.Close(); } } /// <summary> /// 創建表格1 /// </summary> /// <returns></returns> private PdfPTable PDFTable1() { var table1 = new PdfPTable(4); //創建表格實例4列 int[] a = { 1, 2, 3, 4 }; //設置列寬比例 table1.SetWidths(a); for (int i = 0; i < 16; i++) { table1.AddCell((i + 1).ToString()); //添加單元格 } return table1; } /// <summary> /// 創建表格2 /// </summary> /// <returns></returns> private PdfPTable PDFTable2() { //字體定義 var bfchinese = BaseFont.CreateFont(@"c:\windows\fonts\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//simkai.ttf var ChFont_12 = new iTextSharp.text.Font(bfchinese, 12); var ChFont_10 = new iTextSharp.text.Font(bfchinese, 10); var ChFont_8 = new iTextSharp.text.Font(bfchinese, 8); var ChFont_12_red = new iTextSharp.text.Font(bfchinese, 12, iTextSharp.text.Font.ITALIC, BaseColor.RED); var table2 = new PdfPTable(4); //創建表格實例 PdfPCell cell; cell = new PdfPCell(new Phrase(Convert.ToString(1), ChFont_10)); cell.HorizontalAlignment = 1; //居中輸入 默認 0:居左 1:居中 cell.Colspan = 2; //橫向合並單元格 table2.AddCell(cell); cell = new PdfPCell(new Phrase(Convert.ToString(2), ChFont_8)); cell.HorizontalAlignment = 1; cell.Rowspan = 2; //縱向合並單元格 table2.AddCell(cell); cell = new PdfPCell(new Phrase(Convert.ToString(3), ChFont_10)); cell.BackgroundColor = BaseColor.GRAY; //設置背景顏色 table2.AddCell(cell); cell = new PdfPCell(new Phrase(Convert.ToString(4), ChFont_12_red)); //設置字體顏色 table2.AddCell(cell); for (int i = 0; i < 16; i++) { table2.AddCell((i + 1).ToString()); //添加單元格 } return table2; } /// <summary> /// 單元格中填加表 /// </summary> /// <returns></returns> private PdfPTable PDFTable3() { var table3 = new PdfPTable(4); int[] a = { 1, 1, 4, 1 }; //設置列寬比例 table3.SetWidths(a); for (int i = 0; i < 16; i++) { if (i == 10) { PdfPCell cell = new PdfPCell(PDFTable2()); //單元格中添加表2 cell.Padding = 0; //表與單元格間距為0; table3.AddCell(cell); continue; } table3.AddCell("3"); //添加單元格 } return table3; }
樣式:
2)創建圖片
程序:
private void btnPDFImage_Click(object sender, EventArgs e) { string pdfname = string.Empty; SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = "PDF圖片"; dlg.DefaultExt = ".pdf"; dlg.Filter = "Text documents (.pdf)|*.pdf"; if (dlg.ShowDialog() == DialogResult.OK) { pdfname = dlg.FileName; FileStream fs = new FileStream(pdfname, FileMode.Create); //創建文件流 Document document = new Document(PageSize.A5.Rotate()); //創建文件 PageSize.A5.Rotate()表示A5紙橫向輸出 PdfWriter pdfWriter = PdfWriter.GetInstance(document, fs); //實例化 document.Open(); //打開文件 document.Add(addImage(document)); //添加圖片 document.Add(addImage2(document)); //添加圖片 document.Close(); //關閉文件 fs.Close(); } } /// <summary> /// PDF添加圖片 /// </summary> /// <returns></returns> private iTextSharp.text.Image addImage(Document document) { iTextSharp.text.Image hgLogo = iTextSharp.text.Image.GetInstance("yijing.jpg"); hgLogo.ScalePercent(4f); //圖片比例 hgLogo.SetAbsolutePosition(40f, document.PageSize.Height - 100f); //iamge 位置 return hgLogo; } /// <summary> /// PDF添加圖片2 /// </summary> /// <param name="document"></param> /// <returns></returns> private iTextSharp.text.Image addImage2(Document document) { iTextSharp.text.Image hgLogo = iTextSharp.text.Image.GetInstance("yijing.jpg"); hgLogo.ScalePercent(12f); //圖片比例 hgLogo.SetAbsolutePosition(200f, document.PageSize.Height - 400f); //iamge 位置 return hgLogo; }
樣式: