MVC 生成PDf表格并插入图片

最近做的项目中有一个功能,将最终的个人信息生成PDF表格,并插入图片。对于没接触过的程序员来说回一片茫然,网上有多种生成PDf的方法,我给大家介绍一下我认为比较简单,好操作的一种。

iTextSharp组件

使用方法。

1,引入itextsharp.dll

2,  引入命名空间

?





1

2

3

using
iTextSharp;

using
iTextSharp.text;

using
iTextSharp.text.pdf;

?





1

2

3

4

5

6

7

8

9

10

11

12

        Document document = new
Document(); <br>           string
filepath = "/Upload/Pdf/";

            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filepath));<br>            PdfWriter.GetInstance(document, new
FileStream(HttpContext.Current.Server.MapPath(filepath + model.ExamNo + ".pdf"), FileMode.Create));<br>       <br>        document.Open();<br>

            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();<br><span style="color: rgb(255, 0, 0);">  //因为显示照片部分显示5列,而后续部分显示四列,所以创建两个table</span>

          <span style="color: rgb(255, 0, 0);">  PdfPTable table = new
PdfPTable(4);

            PdfPTable tablerow = new
PdfPTable(5);

</span>

不一步一步赘述了,将详细代码奉上。

+ ?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

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 个人信息<br><span style="color: rgb(255, 0, 0);">//这种写法感觉很像silverlight,一个萝卜一个坑,习惯了html的感觉这样很不适应</span>

           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();

       }

+ ?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

  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

  

附上下载方法

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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表格并插入图片,布布扣,bubuko.com

时间: 2024-10-11 18:08:11

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

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

package com.test; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.*; import org.junit.Test; import java.io.FileOutputStream; /** *编辑PDF */ public class PDFTest { @Test pu

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

#region 下载说明书PDF protected void lbtnDownPDF_Click(object sender, EventArgs e) { int pid = ConvertHelper.GetInteger(Request["PID"]); pds = p.GetModel(pid); #region 第一种方法 DataTable datatable = new DataTable(dt.ToString()); try { string Url1 = &quo

java使用iText生成pdf表格

转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <dependency>     <groupId>com.lowagie</groupId>     <artifactId>iText</artifactId>  

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

这篇文章我将向大家演示如何以编程的方式在PDF文档中创建一个网格,并将图片插入特定的网格中. 网上有一些类似的解决方法,在这里我选择了一个免费版的PDF组件.安装控件后,创建新项目,添加安装目录下的dll文件作为项目的引用以及命名空间,如下: using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Grid; 接下来是详细步骤及代码片段: 步骤1: 首先创建一个PDF文档,并添加一个新页面. PdfDocument doc = new

ExtJS4.1.1 设置表格背景颜色 修改文本颜色 在表格中插入图片

由于ExtJS版本不断更新,各种渲染方式也随之有所改变,目前大部分书籍还是停留在3版本,对于ExtJS4.1.1版本的表格渲染,设置表格行背景颜色的方式如下: 首先,定义行的样式: 1.yellow-row .x-grid-cell{ 2  background-color:#FFFF00 !important; 3         } 4  .white-row .x-grid-cell{ 5    background-color:#FFFFFF !important; 6         

如何把图片快速生成PDF?这个方法真不错!

在我们办公学习时候,经常会遇见图片转换PDF的情况,那么怎样把图片快速生成PDF呢?有时候呢,图片在电脑上可以直接在电脑进行处理,但是在手机上时候怎么操作呢?今天小编分享一个方法,在手机上将图片转换PDF的办法,需要的可以看看.操作工具:迅捷PDF阅读器操作步骤: 在我们打开手机的应用商店,搜索:迅捷PDF阅读器,下载使用. 打开软件运行,选择[小功能],然后选择[图片生成PDF].3 点击添加手机里的图片,并且给生成的PDF文档命名.点击确定,等待生成PDF文档. 等待PDF文档转换完成,点击

使用ITextSharp生成PDF文件心得

最近公司在做一个项目,需要把数据导出成PDF格式的文件,然后再网上搜了一下发现开源的组件还挺多的,用的比较多的就是itextsharp,itextsharp由java的itext演变而来,并且提供的丰富的功能,能够制作表格.插入图片等,这是官网的下载地址:http://sourceforge.net/projects/itextsharp/,下面就来简单介绍一下使用itextsharp的心得. 首先创建一个winform应用程序,并且添加itextsharp的引用,在按钮的单击事件写上生成pdf

生成PDF文件

@Action("report_exportPdf")    public String exportPdf() throws Exception{        //查询出满足当前条件 结果数据        List<WayBill> wayBills = wayBillService.findWayBills(model);        //下载导出        //设置头信息        ServletActionContext.getResponse().s

【Java】itext根据模板生成pdf(包括图片和表格)

1.导入需要的jar包:itext-asian-5.2.0.jar itextpdf-5.5.11.jar. 2.新建word文档,创建模板,将文件另存为pdf,并用Adobe Acrobat DC打开编辑,点击右侧[准备表单]后点击[开始] 3.在需要插入数据的空白处,右击,点击[文本域],将文本域拖放到你想要的位置,更改域名称为你传入的变量名. 4.保存文件,将文件放到项目中.生成pdf代码如下: 1 public static void creatPdf(Map<String, Objec