MVC 生成PDf表格并插入图片
生活随笔
收集整理的這篇文章主要介紹了
MVC 生成PDf表格并插入图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近做的項目中有一個功能,將最終的個人信息生成PDF表格,并插入圖片。對于沒接觸過的程序員來說回一片茫然,網上有多種生成PDf的方法,我給大家介紹一下我認為比較簡單,好操作的一種。
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);
?不一步一步贅述了,將詳細代碼奉上。
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);#endregiondocument.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);elseCreateCell(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
?
?
?附上下載方法
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.dll
轉載于:https://www.cnblogs.com/zlzly/p/3732542.html
總結
以上是生活随笔為你收集整理的MVC 生成PDf表格并插入图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员常用的代码编辑器
- 下一篇: s3c2440移植MQTT