.Net iTextSharp 导出pdf, 包含列表和图片
2018-03-20 17:56
435 查看
//引包iTextSharp,下载iTextSharp,项目引入itextsharp.dll using iTextSharp.text; using iTextSharp.text.pdf; public void SimplePdf() //入口 { Document doc = new Document(PageSize.A4); //图纸大小A4纸 byte[] bytes = null ; try { BaseFont baseFont = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //SIMSUN.TTC:宋体和新宋体 Font ChFont_black = new Font(baseFont, 12); //PDF内容放入到流中 MemoryStream m = new MemoryStream(); PdfWriter.GetInstance(doc, m); doc.Open(); //设定每列宽度 float[] w = { 150, 150, 150 }; PdfPTable table = new PdfPTable(w.Length); //初始化列数 table.SetWidthPercentage(w, PageSize.A4); table.NormalizeHeadersFooters(); table.SplitLate = false; PdfPCell cell; #region 一、数据列表 #region 数据列表标题 cell = new PdfPCell(new Phrase("姓名", ChFont_black)); //new Phrase("姓名"); 多个重载方法,可以添加样式等。 cell.Colspan = 1; //默认为1;可以做合并单元格处理,列合并例如:cell.Colspan = 2 合并两列;行合并例如:cell.Rowspan = 2 合并两行 cell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑 4000 色 cell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色 cell.HorizontalAlignment = Element.ALIGN_CENTER; //水平居中 cell.VerticalAlignment = Element.ALIGN_MIDDLE; //垂直居中 table.AddCell(cell); cell = new PdfPCell(new Phrase("性别", ChFont_black)); cell.BackgroundColor = new BaseColor(0, 102, 204); cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.VerticalAlignment = Element.ALIGN_MIDDLE; table.AddCell(cell); cell = new PdfPCell(new Phrase("年龄", ChFont_black)); cell.BackgroundColor = new BaseColor(0, 102, 204); cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.VerticalAlignment = Element.ALIGN_MIDDLE; table.AddCell(cell); #endregion #region 列表数据 List<UserTest> userTestList = InitUserTestData(); //得到数据 foreach (UserTest info in userTestList) { //遍历数据后,第一列 cell = new PdfPCell(new Phrase(info.Name, ChFont_black)); cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.VerticalAlignment = Element.ALIGN_MIDDLE; table.AddCell(cell); //遍历数据后,第二列 cell = new PdfPCell(new Phrase(info.Sex, ChFont_black)); cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.VerticalAlignment = Element.ALIGN_MIDDLE; table.AddCell(cell); //遍历数据后,第三列 cell = new PdfPCell(new Phrase(info.Age, ChFont_black)); cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.VerticalAlignment = Element.ALIGN_MIDDLE; table.AddCell(cell); } #endregion //***cell.Table 可以进行table嵌套,适用于多个复杂table, 举例:外层嵌套框架table, 单元格内嵌套table,这样能够有效处理到处内容的结构。 #endregion #region 二、嵌入图片 iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("E:\\wandapos\\02_Project\\14_Web\\03_WandaPOS\\WandaWebSite\\TempDoc\\209054a4-fa46-4882-8c2a-e9918ded954e.jpg"); //图片存放路径 cell = new PdfPCell(); cell.Colspan = 3; cell.Image = img; //图片 cell.HorizontalAlignment = Element.ALIGN_LEFT; cell.VerticalAlignment = Element.ALIGN_MIDDLE; table.AddCell(cell); #endregion doc.Add(table); //将table元素添加入文档对象 doc.Close(); bytes = m.GetBuffer(); } catch { //异常 } ExportPDFFile("pdf名称", bytes); } public void ExportPDFFile(string fileName, byte[] bytes) //浏览器下载 { Encoding encoding; string outputFileName = null; //兼容MS/IE,FIREFOX浏览器处理 string browser = HttpContext.Current.Request.UserAgent.ToUpper(); if (browser.Contains("MS") == true && browser.Contains("IE") == true) { outputFileName = HttpUtility.UrlEncode(fileName); encoding = System.Text.Encoding.Default; } else if (browser.Contains("FIREFOX") == true) { outputFileName = fileName; encoding = System.Text.Encoding.GetEncoding("GB2312"); } else { outputFileName = HttpUtility.UrlEncode(fileName); encoding = System.Text.Encoding.Default; } //输出到浏览器端。 HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentEncoding = encoding; HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.pdf", string.IsNullOrEmpty(outputFileName) ? DateTime.Now.ToString("yyyyMMdd") : outputFileName)); HttpContext.Current.Response.BinaryWrite(bytes); HttpContext.Current.Response.End(); } public List<UserTest> InitUserTestData() //构造数据 { List<UserTest> userTestList = new List<UserTest>(); UserTest userTestInfo = null; userTestInfo = new UserTest(); userTestInfo.Name = "张三"; userTestInfo.Sex = "女"; userTestInfo.Age = "18"; userTestList.Add(userTestInfo); userTestInfo = new UserTest(); userTestInfo.Name = "李四"; userTestInfo.Sex = "男"; userTestInfo.Age = "19"; userTestList.Add(userTestInfo); userTestInfo = new UserTest(); userTestInfo.Name = "王麻"; userTestInfo.Sex = "男"; userTestInfo.Age = "30"; userTestList.Add(userTestInfo); return userTestList; } public class UserTest { public string Name { get; set; } public string Sex { get; set; } public string Age { get; set; } }
相关文章推荐
- .net中操作PDF-iTextSharp 中图片Image
- itextsharp 导出PDF 包括图片 分页(全面)
- 基于iTextSharp的HTML转PDF,包含图片的转换
- 在Asp.Net中操作PDF – iTextSharp - 操作图片
- .net 导出excel带图片
- Fusion Charts导出图片或Pdf
- 【译】在Asp.Net中操作PDF – iTextSharp-列表
- 问题修改highcharts 导出图片 .net c#(二)问题修改
- .NET的那些事儿(9)——C# 2.0 中用iTextSharp制作PDF(基础篇)
- Java程序员从笨鸟到菜鸟之(一百零六)java操作office和pdf文件(四)页面列表导出cvs,excel、pdf报表.
- 详解开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍之八)
- 使用itextsharp创建PDF文档——图片集合
- 使用第三方组件NPOI和iTextSharp根据DataTable导出Excel、Word、Pdf
- FusionCharts fcexporter.jar图表导出图片或pdf功能
- highcharts 导出图片 .net c#
- 快速导出PDF文件中所有图片(使用Adobe Acrobat 10 )
- java操作office和pdf文件页面列表导出cvs,excel、pdf报表.
- Confluence PDF导出不显示中文,导出Word图片及代码显示
- unity3d 利用 itextSharp导出pdf格式
- 利用itextsharp导出pdf