最近要制作 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); }
示例圖: