首页 > 代码库 > 使用iTextSharp导出PDF
使用iTextSharp导出PDF
/// <summary> /// 导出至PDF /// </summary> /// <param name="dt">数据源</param> /// <param name="fileName">文件名</param> /// <param name="dicTableHeader">字段表头名对照</param> public static void Export(DataTable dt, string fileName, Dictionary<string, string> dicTableHeader) { Document document = new Document(); using (MemoryStream pdfStream = new MemoryStream()) { PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfStream); document.Open(); BaseFont bfChinese = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new BaseColor(0, 0, 0)); Font fontHeader = new Font(bfChinese, 12, Font.BOLD, new BaseColor(0, 0, 0)); int columCount = dicTableHeader.Count; PdfPTable table = new PdfPTable(columCount); foreach (var item in dicTableHeader) { PdfPCell cell = new PdfPCell(new Phrase(item.Value, fontHeader)); cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(cell); } for (int i = 0; i < dt.Rows.Count; i++) { foreach (var item in dicTableHeader) { table.AddCell(new Phrase(dt.Rows[i][item.Key].ToString(), fontChinese)); } } document.Add(table); document.Close(); pdfWriter.Close(); HttpContext curContext = HttpContext.Current; curContext.Response.Clear(); curContext.Response.ClearContent(); curContext.Response.ClearHeaders(); curContext.Response.ContentType = "application/octet-stream"; curContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8) + ".pdf"); curContext.Response.BinaryWrite(pdfStream.ToArray()); curContext.Response.Flush(); curContext.Response.End(); } }
使用iTextSharp导出PDF
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。