最近作的項目中有一個功能,將最終的我的信息生成PDF表格,並插入圖片。對於沒接觸過的程序員來講回一片茫然,網上有多種生成PDf的方法,我給你們介紹一下我認爲比較簡單,好操做的一種。html
iTextSharp組件程序員
使用方法。面試
1,引入itextsharp.dll字體
2, 引入命名空間編碼
using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf;
Document document = new Document();
string filepath = "/Upload/Pdf/"; Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filepath));
PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath(filepath + model.ExamNo + ".pdf"), FileMode.Create));
document.Open();
BaseFont bftitle = BaseFont.CreateFont(@"C:\\Windows\Fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font fonttitle = new Font(bftitle, 20, Font.UNDERLINE); BaseFont bf1 = BaseFont.CreateFont(@"C:\\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font font1 = new Font(bf1, 12); Font fonttitle10 = new Font(bf1, 12); Font fonttitle12 = new Font(bf1, 11); PdfPCell cellmode = new PdfPCell();
//由於顯示照片部分顯示5列,然後續部分顯示四列,因此建立兩個table PdfPTable table = new PdfPTable(4); PdfPTable tablerow = new PdfPTable(5);
不一步一步贅述了,將詳細代碼奉上。url
public void CreatePersonInfo(RBS.Models.UserConfirmModel model) { Document document = new Document(); string filepath = "/Upload/Pdf/"; Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filepath)); PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath(filepath + model.ExamNo + ".pdf"), FileMode.Create)); document.Open(); BaseFont bftitle = BaseFont.CreateFont(@"C:\\Windows\Fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font fonttitle = new Font(bftitle, 20, Font.UNDERLINE); BaseFont bf1 = BaseFont.CreateFont(@"C:\\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font font1 = new Font(bf1, 12); Font fonttitle10 = new Font(bf1, 12); Font fonttitle12 = new Font(bf1, 11); PdfPCell cellmode = new PdfPCell(); PdfPTable table = new PdfPTable(4); PdfPTable tablerow = new PdfPTable(5); float padding = 0f; #region 我的信息
//這種寫法感受很像silverlight,一個蘿蔔一個坑,習慣了html的感受這樣很不適應 CreateCell(5, 5, 1, "我的信息", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "考生面試編號", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.ExamNo, fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "姓名", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.Name, fonttitle10, "left", tablerow); CreateCell(model.UserId.ToString(), model.Ext, 0, 1, padding, padding, tablerow); CreateCell(1, 5, 1, "性別", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.Sex == RBS.Models.Gender.Male ? "男" : "女", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "出生日期", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.BirthDate.ToString("yyyy-MM-dd"), fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "身份證號碼", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.IdentificationNo, fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "戶口所在地", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.Country, fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "出生地", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.State, fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "婚姻情況", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.IsMarrage == RBS.Models.Marriage.Naught ? "未婚" : "已婚", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "聯繫地址", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.Address, fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "郵政編碼", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.Postcode, fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "聯繫電話", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.Telephone, fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "手機", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.Mobile, fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "郵箱", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, model.Email, fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "", fonttitle10, "left", tablerow); CreateCell(1, 5, 1, "", fonttitle10, "left", tablerow); #endregion #region 教育經歷 CreateCell(4, 5, 1, "教育經歷", fonttitle10, "left", table); CreateCell(4, 5, 1, "教育經歷1", fonttitle10, "left", table); CreateCell(1, 5, 1, "入學時間", fonttitle10, "left", table); CreateCell(1, 5, 1, model.EduBegin != null ? Convert.ToDateTime(model.EduBegin).ToString("yyyy-MM-dd") : "", fonttitle10, "left", table); CreateCell(1, 5, 1, "畢業時間", fonttitle10, "left", table); CreateCell(1, 5, 1, model.EduEnd != null ? Convert.ToDateTime(model.EduEnd).ToString("yyyy-MM-dd") : "", fonttitle10, "left", table); CreateCell(1, 5, 1, "學校名稱", fonttitle10, "left", table); CreateCell(1, 5, 1, model.Graduated, fonttitle10, "left", table); CreateCell(1, 5, 1, "專業", fonttitle10, "left", table); CreateCell(1, 5, 1, model.Specialty, fonttitle10, "left", table); CreateCell(1, 5, 1, "學歷", fonttitle10, "left", table); CreateCell(1, 5, 1, model.EduBackground, fonttitle10, "left", table); CreateCell(1, 5, 1, "學位", fonttitle10, "left", table); CreateCell(1, 5, 1, model.Degree, fonttitle10, "left", table); #endregion document.Add(tablerow); document.Add(table); document.Close(); }
private void CreateCell(int Colspan, bool Border, string Content, Font font, string alignment, PdfPTable table) { if (Border) CreateCell(Colspan, 0, 1, Content, font, alignment, table); else CreateCell(Colspan, 0, Content, font, alignment, table); } /// /// 生成單元格 /// /// 合併列數 /// 間距 /// 內容 /// 字體 /// 對齊方式 /// 此單元格填充的表 private void CreateCell(int Colspan, int Padding, string Content, Font font, string alignment, PdfPTable table) { CreateCell(Colspan, Padding, 0, Content, font, alignment, table); } /// /// 生成單元格 /// /// 合併列數 /// 間距 /// 邊框線 /// 內容 /// 字體 /// 對齊方式 /// 此單元格填充的表 private void CreateCell(int Colspan, int Padding, int Border, string Content, Font font, string alignment, PdfPTable table) { CreateCell(Colspan, Padding, Border, 0, 0, Content, font, alignment, table); } private void CreateCell(int Colspan, int Rowspan, int Padding, int Border, string Content, Font font, string alignment, PdfPTable table) { CreateCell(Colspan, Rowspan, Padding, Border, 0, 0, Content, font, alignment, table); } /// /// 生成單元格 /// /// 合併列數 /// 間距 /// 邊框線 /// 水平對齊方式 /// 垂直對齊方式 /// 內容 /// 字體 /// 對齊方式 /// 此單元格填充的表 private void CreateCell(int Colspan, int Padding, int Border, int HorizontalAlignment, int VerticalAlignment, string Content, Font font, string alignment, PdfPTable table) { PdfPCell cell = new PdfPCell(); cell.Colspan = Colspan; cell.Padding = Padding; if (HorizontalAlignment > 0) cell.HorizontalAlignment = HorizontalAlignment; if (VerticalAlignment > 0) cell.VerticalAlignment = VerticalAlignment; if (Border == 0) cell.Border = Border; Paragraph table_t = new Paragraph(Content, font); table_t.SetAlignment(alignment); cell.AddElement(table_t); table.AddCell(cell); } private void CreateCell(int Colspan, int Rowspan, int Padding, int Border, int HorizontalAlignment, int VerticalAlignment, string Content, Font font, string alignment, PdfPTable table) { PdfPCell cell = new PdfPCell(); cell.Colspan = Colspan; cell.Rowspan = Rowspan; cell.Padding = Padding; if (HorizontalAlignment > 0) cell.HorizontalAlignment = HorizontalAlignment; if (VerticalAlignment > 0) cell.VerticalAlignment = VerticalAlignment; if (Border == 0) cell.Border = Border; Paragraph table_t = new Paragraph(Content, font); table_t.SetAlignment(alignment); cell.AddElement(table_t); table.AddCell(cell); } #region 向表格中插入照片,若是向表格中插入照片,這是關鍵的重載 /// <param name="imgName">照片名稱</param> /// <param name="imgExt">照片後綴</param> /// <param name="align"></param> /// <param name="colSpan"></param> /// <param name="padTop"></param> /// <param name="padBottom"></param> /// <param name="table"></param> public void CreateCell(string imgName, string imgExt, int align, int colSpan, float padTop, float padBottom, PdfPTable table) { PdfPCell cell = new PdfPCell(); Paragraph ph = new Paragraph(); string url = HttpContext.Current.Server.MapPath("~/Upload/Images/" + imgName + imgExt + ""); Image jpg = Image.GetInstance(new Uri(url)); //設置照片的寬度和高度 jpg.ScaleAbsolute(80, 130); Chunk ck = new Chunk(jpg, 4, -4); //圖片可設置 偏移 ph.SetAlignment("right"); ph.Add(ck); cell.AddElement(ph); if (padTop > 0) { cell.PaddingTop = padTop; } if (padBottom > 0) { cell.PaddingBottom = padBottom; } if (colSpan > 0) { cell.Colspan = colSpan; } //由於表格插入圖片部分須要跨行,爲了美觀,跨7行,這裏須要注意的是,設置幾行必須跨幾行,不然圖片不會顯示,當我剛跨2行準備看下效果的時候,pdf照片位置顯示空文本框,沒有照片,當跨行跟設置的所對應時照片顯示出來。 cell.Rowspan = 7; //cell.Border = 0; 設置表格線 cell.HorizontalAlignment = align; table.AddCell(cell); } #endregion
附上下載方法spa
public static void DownLoad(string path) { try { HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.HeaderEncoding = Encoding.UTF8; HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(Path.GetFileName(path), Encoding.UTF8)); HttpContext.Current.Response.WriteFile(path); } catch (Exception ex) { HttpContext.Current.Response.Write("下載文件出錯,緣由:" + ex.Message); } finally { HttpContext.Current.Response.End(); } }
下載itextpdf.dllcode