iTextSharp 生成 PDF 问题集合
最近要制作 asp.net 生成 PDF 项目,在网上找一些相关资料发现 iTextSharp 这个 dll 生成PDF 还是不错的,
在开发中遇到很多问题,在网上也找了不少的资料,在这汇总了一个知识点,在关键的地方做了一些注释希望能给园里的朋友带来一些帮助。
using System; using System.Web; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; public class Pdf : IHttpHandler { public void ProcessRequest(HttpContext context) { Rectangle rect = new Rectangle(8.5f * 72, 11 * 72); //自己设置 纸张大小 Document document = new Document(rect, 20, 20, 80, 20); //Document document = new Document(PageSize.A4); //可以按照指定纸张大小 string pdfFile = context.Server.MapPath("sample.pdf"); PdfWriter.GetInstance(document, new FileStream(pdfFile, FileMode.Create)); document.Open(); //设置 document 背景图 string imgPath = context.Server.MapPath("../images/2012/bg2.png"); Image image = Image.GetInstance(imgPath); image.SetAbsolutePosition(0, 0); image.Alignment = Image.ALIGN_UNDEFINED; image.ScaleAbsolute(image.Width, image.Height); document.Add(image); //设置PDF 文档属性 document.AddAuthor("junny"); document.AddCreationDate(); document.AddCreator("SBS.inc"); document.AddHeader("Email", "junny_sa@163.com"); document.AddKeywords(" PDF "); document.AddProducer(); document.AddSubject(" This is Subject"); document.AddTitle(" This is Title "); //设置中文字体 BaseFont baseFont = CreateChineseFont(); //设置字体的类型 iTextSharp.text.Font titleFont = new iTextSharp.text.Font(baseFont, 18, Font.BOLD); iTextSharp.text.Font normalFont = new iTextSharp.text.Font(baseFont, 12), normalBoldFont = new iTextSharp.text.Font(baseFont, 10, Font.BOLD); iTextSharp.text.Font normalRedFont = new iTextSharp.text.Font(baseFont, 10, Font.NORMAL | Font.BOLD, BaseColor.RED); //设置一个空白换行 float normalLineHeight = 25f; Paragraph pBlank = new Paragraph(" ", normalFont); pBlank.Leading = normalLineHeight; document.Add(pBlank); float[] widths = new float[] { 12f, 140f, 35f }; float padding = 3f; BaseColor bgColor = new BaseColor(153, 204, 255); //设置背景色 PdfPTable table = new PdfPTable(3); //设置为三列表格 table.SetWidths(widths);
table.AddCell(CreateCellHeader(" Create PDF ", titleFont, 3, padding, padding, bgColor)); string[] ID_Arr = { "1", "2", "3" }; string[] name_Arr = { "junny", "kendy", "sally" }; string[] phone_Arr = { "178-894-7893", "378-822-7289", "221-842-9874" }; table.AddCell(CreateCell("ID", normalFont, false, 1, 0, padding, padding)); table.AddCell(CreateCell("Name", normalFont, false, 0, 0, padding, padding)); table.AddCell(CreateCell("Phone", normalFont,false, 0, 0, padding, padding)); for (int i = 0; i < 3; i++) { table.AddCell(CreateCell(ID_Arr[i],normalFont,true,0,0,padding,padding)); table.AddCell(CreateCell(name_Arr[i], normalFont,false, 0, 0, padding, padding)); table.AddCell(CreateCell(phone_Arr[i], normalFont,false, 0, 0, padding, padding)); } document.Add(table); document.Close(); } }
CreateCell 方法
/// <summary> /// 创建Table行 /// </summary> /// <param name="txt">文本</param> /// <param name="txtFont">字体</param> /// <param name="image">是否添加图片</param> /// <param name="align">对齐方式</param> /// <param name="colSpan">跨行数</param> /// <param name="padTop">顶部padding</param> /// <param name="padBottom">底部padding</param> /// <returns>Table行</returns> public static PdfPCell CreateCell(string txt, Font txtFont,bool image, int align, int colSpan, float padTop, float padBottom) { PdfPCell cell = new PdfPCell(); Phrase ph = new Phrase(txt, txtFont); //这里是添加图片的例子,顺带说一下在表格里加图片会换行问题,按照以下方法,图片会跟在文字后面 if (image) { Image IMG = Image.GetInstance(HttpContext.Current.Server.MapPath("../images/view_add.png")); Chunk ck = new Chunk(IMG, 4, -4); //图片可设置 偏移 ph.Add(ck); } cell.AddElement(ph); if (padTop > 0) { cell.PaddingTop = padTop; } if (padBottom > 0) { cell.PaddingBottom = padBottom; } if (colSpan > 0) { cell.Colspan = colSpan; } //cell.Border = 0; 设置表格线 cell.HorizontalAlignment = align; return cell; }
CreateCellHeader 方法
/// <summary> /// 创建Table行 /// </summary> /// <param name="txt">文本</param> /// <param name="txtFont">字体</param> /// <param name="colSpan">跨行数</param> /// <param name="padTop">顶部padding</param> /// <param name="padBottom">底部padding</param> /// <param name="bgColor">背景色</param> /// <returns>Table行</returns> public static PdfPCell CreateCellHeader(string txt, Font txtfont, int colSpan, float padTop, float padBottom, BaseColor bgColor) { PdfPCell cell = new PdfPCell(new Phrase(txt, txtfont)); if (padTop > 0) { cell.PaddingTop = padTop; } if (padBottom > 0) { cell.PaddingBottom = padBottom; } if (colSpan > 0) { cell.Colspan = colSpan; } //cell.Border = 0; cell.HorizontalAlignment = Element.ALIGN_CENTER; //0=Left, 1=Centre, 2=Right cell.VerticalAlignment = Element.ALIGN_MIDDLE; cell.BackgroundColor = bgColor; return cell; }
//设置中文字体
public static BaseFont CreateChineseFont() { */ //这里设置 黑体 字类型, return BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); }
示例图: