最近做的项目中有一个功能,将最终的个人信息生成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列,而后续部分显示四列,所以创建两个tablePdfPTable 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表格并插入图片相关推荐

  1. Springboot通过模板生成pdf文档带图片

    Springboot通过模板生成pdf文档带图片 环境:springboot2.3.10.RELEASE + itextpdf5.5.13.2 依赖 <dependency><gro ...

  2. 打开另外一个页面_如何在PDF页面中插入图片?

    如何给PDF添加图片?有些时候为了丰富PDF的文档内容,需要添加一些图片,相比Word或PPT文档可以直接插入图片,而PDF的操作很多人可能并不熟悉,下面一起来看看如何在PDF文档中插入图片. 关于P ...

  3. Java—将数据生成pdf表格

    由于时间问题,所以粗略的封装了一个生成pdf表格的工具包(不喜欢讲废话,直接上代码!!!) package com.sgcc.dlsc.jibei.commons.utils;import com.i ...

  4. 如何快速在PDF文件中插入图片

    在 PDF文件中插入图片我优先想到了 Adobe Acrobat DC,胜任此项工作完全 OK.但是有个问题,Acrobat 会自动识别 PDF 中的文字.如果有手写字迹经过 Acrobat 识别再保 ...

  5. word文档表格中插入图片设置

    word文档的表格中插入图片有时会出现半截现象. 1.设置段落,足够的高度,比如原先25磅,可以改为50磅: 2.插入图片后,用回车调整图片位置.

  6. wps表格批量插入图片

    wps表格批量插入图片 本文作者LockieDeng,后端工程师,致力于倒腾各种前沿技术,欢迎交流: dengmuuming@163.com 0.前言 在开始阅读本文档之前,电脑中必须安装好 WPS ...

  7. 向pdf文件中插入图片及文字 java实现

    向pdf文件中插入图片及文字 引入itextpdf相关依赖 <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> ...

  8. Word文档表格中插入图片无法完整显示解决办法

    如上图所示,Word文档表格中插入图片却只显示很窄一条边 解决办法:选中图片,菜单栏上点击段落右方小箭头扩展项 选择单倍行距,即可完整显示图片

  9. 记录 java 往 word 表格里边插入图片 的坑

    坑了两次,还是记录下 免得下次又忘记.java 在往word 表格里边插入图片的时候,如果循环插入多个表格,第一个表格里边的图片正常显示,但是第二个表格里边的图片总是展示为上一个表格的最后一张图片,经 ...

  10. QTableView 表格中插入图片、勾选框、下拉框

    QTableView 表格中插入图片.勾选框.下拉框 效果图 QT的Delegate,可以实现TableView中每一个单元格的显示形式.本例利用Delegate的paint函数绘制出图片.勾选框.下 ...

最新文章

  1. 《请不要回应外星人2019》
  2. Python与C语言基础对比(Python快速入门)
  3. 【sparkStreaming】将DStream保存在MySQL
  4. 撑起整个互联网的7大开源技术
  5. Extjs--FormPanel(2)
  6. 【多线程】阻塞队列的C++多线程 实现 BlockingQueue
  7. 东莞.NET俱乐部线下技术沙龙-活动报名
  8. 外链式样式表_引入CSS样式表(书写位置)
  9. ae中心点重置工具_(精品)AE从小白到大神之路(七)-AE动画—动效常见的设计方法...
  10. MySql的事务操作与演示样例
  11. jquery广告轮播插件
  12. 阶段1 语言基础+高级_1-3-Java语言高级_09-基础加强_第1节 基础加强_2_Junit_测试概述...
  13. FastStone Capture滚动截屏
  14. [深度学习] ImageAI库使用笔记
  15. ZJYYC LSH去年买了个表(也是LZY去年买了个表) DFS
  16. 致远oa系统unix 服务器,致远oa手机客户端服务器
  17. 创造单词量测试项目(带有Ajax的爬虫)
  18. Mac 2020版M1 安装虚拟机各种踩坑详解(关注文末小惊喜哟)
  19. 镭速传输:TB级大文件传输,一招教你搞定
  20. python将三位数分离(format格式)_Python格式函数,python,之,format

热门文章

  1. openssl版本升级
  2. Kaggle账号的注册
  3. matlab 光斑质心算法,关于激光探测器光斑质心算法硬件的设计
  4. lollipop中文_lollipop的中文解释_lollipop的读音_lollipop的双语例句 - 英语词典
  5. 我国东北虎种群增长迅速 但近交风险不容忽视
  6. java碳纤维山地车车架咋样_自行车碳纤维车架值得买吗?它有哪些优缺点?老骑手来给你答案!...
  7. 莫名其妙CSDN不让我发帖子了。看来10年的老水手我该离开CSDN了。
  8. 网吧服务器磁盘性能,网吧游戏服务端显示磁盘已满怎么解决
  9. 常用 25/26 Flash 系列器件型号、ID、容量对照表
  10. 国外问卷调查好做吗?