标签:
文章主要介绍使用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; }
样式:
标签:
原文地址:http://www.cnblogs.com/yangy1989/p/5300304.html