最近做的项目中有一个功能,将最终的个人信息生成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); #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

  

附上下载方法

 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

MVC 生成PDf表格并插入图片的更多相关文章

  1. Java编辑PDF写入文字 插入图片

    package com.test; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Font; import com.itex ...

  2. PHP生成PDF并转换成图片爬过的坑

    需求描述:根据订单通过模板合同生成新的PDF合同通过e签宝签约后转为图片给用户下载. 需求整理: 1.如何生成PDF文件:使用TCPDF扩展生成.思考: ⑴为了方便将模板中的固定占位符替换为订单中的内 ...

  3. Spring MVC生成PDF文件

    以下示例演示如何使用Spring Web MVC框架生成PDF格式的文件.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: ...

  4. NX二次开发-OLE/COM向EXCEL表格中插入图片

    今晚有一个兄弟问我怎么往EXCEL里插入图片(加工程序单中需要插入图片),这个我之前也没弄过,回复了他一句不知道,后来刚刚干完游戏吃完鸡,就去VC++的书上翻了翻,还真的被我翻到了.VC++的方法往E ...

  5. Java 生成pdf表格文档

    最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法.废话不多说,直接上代码: THSarabunNew.ttf该文件是泰国字体自行网上 ...

  6. c# iText 生成PDF 有文字,图片,表格,文字样式,对齐方式,页眉页脚,等等等,

    #region 下载说明书PDF protected void lbtnDownPDF_Click(object sender, EventArgs e) { int pid = ConvertHel ...

  7. java使用iText生成pdf表格

    转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ...

  8. C#中如何创建PDF网格并插入图片

    这篇文章我将向大家演示如何以编程的方式在PDF文档中创建一个网格,并将图片插入特定的网格中. 网上有一些类似的解决方法,在这里我选择了一个免费版的PDF组件.安装控件后,创建新项目,添加安装目录下的d ...

  9. .net 生成pdf表格

    只需要建一个类文件就搞定了 public class CreatePDF { public static CreatePDF Current { get { return new CreatePDF( ...

随机推荐

  1. 配置错误 在唯一密钥属性“fileExtension”设置为“.log”时,无法添加类型为“mimeMap”的重复集合项

    错误提示: 配置错误 在唯一密钥属性“fileExtension”设置为“.log”时,无法添加类型为“mimeMap”的重复集合项 配置文件 \\?\D:\www\abc\web.config 出现 ...

  2. Get-ChildItem参数之 -Exclude,Filter,Recurse应用

    $p = "D:\PSScript" gci $p -Exclude "UpdateLog" #排除子目录"UpdateLog",但是后面不 ...

  3. Centos上Apache重启,mysql重启, nginx 重启方法

    1.重启 apache service httpd restrat /etc/init.d/httpd stop /etc/init.d/httpd start 2.重启 mysql service ...

  4. Caching in ASP.NET MVC

    The caching options available in ASP.NET MVC applications don’t come from the ASP.NET MVC Framework, ...

  5. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  6. objective-c对NSArray的学习

    转自:http://gekie.iteye.com/blog/1086256 NSARRAY简单的使用 定义数组,遍历数组: 1 2 3 4 5 6 7 8 NSArray *array; array ...

  7. leetcode -- Largest Rectangle in Histogram TODO O(N)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  8. Ruby on Rails Tutorial 第四章 Rails背后的Ruby 之 其他数据类型(二)

    1.方法 定义如下所示: def string_message(str='') if str.empty? "It's an empty string!" else "T ...

  9. 1050. String Subtraction (20)

    this problem  is from PAT, which website is http://pat.zju.edu.cn/contests/pat-a-practise/1050. firs ...

  10. 图形化管理debian服务

    bootupmanager这个软件 ,用着勉强吧, 功能不多. 安装 sudo apt-get install bum 卸载sudo apt-get remove --purge bum 多了不说 , ...