dqsg

  要创建pdf,首先要用到一个itextsharp的dll控件。这是我找到的该控件itextsharp的使用方法。

  这是同事给我的一个简单的创建pdf的类。我觉得蛮好用的,大部分pdf操作都能实现。

代码
1 using System;
2  using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using iTextSharp.text.pdf;
6 using iTextSharp.text;
7 using System.IO;
8
9 namespace Imdingji.Tools
10 {
11 public class MyPDF
12 {
13 /// <summary>
14 /// 创建中文字体(实现中文)
15 /// </summary>
16 /// <returns></returns>
17 private static BaseFont CreateChineseFont()
18 {
19 BaseFont.AddToResourceSearch("iTextAsian.dll");
20 BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); //"STSong-Light", "UniGB-UCS2-H",
21 BaseFont baseFT = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
22
23 //iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT);
24 return baseFT;
25 }
26
27 private static Font CreateChinese(int style)
28 {
29 BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
30 return new Font(bfChinese, 10, style, Color.BLACK);
31 }
32
33 /// <summary>
34 /// 构造单元格
35 /// </summary>
36 /// <param name="text"></param>
37 /// <param name="colspan"></param>
38 /// <param name="isCenter"></param>
39 /// <returns></returns>
40 //public static Cell InitCell(string text, int colspan, int rowspan, bool isCenter)
41 //{
42 // Font fontChinese = CreateChinese();
43 // Cell cell = new Cell(new Paragraph(text, fontChinese));
44 // if (isCenter)
45 // cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
46 // if (colspan > 1)
47 // cell.Colspan = colspan;
48 // if (rowspan > 1)
49 // cell.Rowspan = rowspan;
50 // cell.UseAscender = true;
51 // cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
52 // return cell;
53 //}
54
55 public static PdfPCell CreateCell(string text,
56 int size,
57 int colspan,
58 int rowspan,
59 bool isCenter,
60 bool isBold)
61 {
62 int bold = isBold ? 1 : 0;//1为加粗,0为正常
63 Font fontChinese = CreateChinese(bold);
64 //Font font = new Font(Font.FontFamily.HELVETICA, size, bold, BaseColor.BLACK);
65 PdfPCell cell = new PdfPCell(new Paragraph(text, fontChinese));
66 if (isCenter)
67 cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
68 if (colspan > 1)
69 cell.Colspan = colspan;
70 if (rowspan > 1)
71 cell.Rowspan = rowspan;
72 cell.UseAscender = true;
73 cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
74 cell.Padding = 5f;
75 return cell;
76 }
77
78 public static PdfPCell CreateCell(string text)
79 {
80 return CreateCell(text, 1, 1);
81 }
82
83 public static PdfPCell CreateCell(string text, int colspan, int rowspan)
84 {
85 return CreateCell(text, 10, colspan, rowspan, false, false);
86 }
87
88 /// <summary>
89 /// 构造一个pdf实例,调用后务必关闭doc
90 /// </summary>
91 /// <param name="filePath"></param>
92 /// <param name="creator"></param>
93 /// <param name="author"></param>
94 /// <param name="title"></param>
95 /// <param name="isRotate">true-页面横向,false-默认</param>
96 /// <returns></returns>
97 public static Document CreatePDFDoc(string filePath,string creator,string author,string title,bool isRotate)
98 {
99 Rectangle rec;
100 if (isRotate)
101 rec = new Rectangle(PageSize.A4.Rotate());
102 else
103 rec = new Rectangle(PageSize.A4);
104 Document doc = new Document(rec);
105 PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));
106 //writer.PageEvent = new HeaderEvent();
107 //设置文档的边距,依次是 left, right, top, bottom
108 //doc.SetMargins(17.2f, 18.8f, 18.8f, 15f);
109 //可以在Open()方法调用前为doc添加摘要信息
110 doc.AddCreationDate();
111 doc.AddCreator(creator);
112 doc.AddAuthor(author);
113 doc.AddTitle(title);
114 return doc;
115 }
116
117 public static void SetHeader(Document doc,string text)
118 {
119 Phrase ph = new Phrase(text,CreateChinese(0));
120 HeaderFooter header = new HeaderFooter(ph,false);
121 Phrase phFoot = new Phrase();
122 phFoot.Font.Size = 9;
123
124 HeaderFooter foot = new HeaderFooter(phFoot, true);
125 foot.Alignment = 2;
126 foot.BorderWidthTop = 0;
127 foot.BorderWidthBottom = 0;
128
129 header.BorderWidthTop = 0;
130 doc.Header = header;
131 doc.Footer = foot;
132 }
133 //public static void SetFoot(Document doc)
134 //{
135 // HeaderFooter foot = new HeaderFooter(new Phrase(doc.PageNumber.ToString()), true);
136 // // foot.
137 // doc.Footer = foot;
138 //}
139 /// <summary>
140 /// 构造一个pdf的表格
141 /// </summary>
142 /// <param name="colCount"></param>
143 /// <returns></returns>
144 public static PdfPTable CreateTable(int colCount)
145 {
146 PdfPTable pdfTable = new PdfPTable(colCount);
147 /*
148 * 要理解 Table 中的单元格怎么显示,先设定列数,然后逐个放 Cell,当前行的 Cell
149 * 数量到达列数时另起新行,可用单元格的 Rowspan,Colspan 设定跨行或跨列的数量
150 **/
151 pdfTable.SpacingBefore = 3;
152 pdfTable.SpacingAfter = 3;
153 pdfTable.WidthPercentage = 98f;
154
155 //可以使单元格内容跨页显示
156 pdfTable.SplitLate = false;
157 pdfTable.SplitRows = true;
158 return pdfTable;
159 }
160
161 public static Paragraph CreatePara(string text)
162 {
163 Font fontChinese = CreateChinese(1);
164 Paragraph p = new Paragraph(text, fontChinese);
165 p.SpacingAfter = 10f;
166 return p;
167 }
168 }
169
170 ////用来在每一页加页眉的页面事件
171 //class HeaderEvent : PdfPageEventHelper,IPdfPageEvent
172 //{
173 // private Phrase header;
174 // public HeaderEvent()
175 // {
176 // header = new Phrase("上海市火炬高科技产业开发中心");
177 // }
178 // public override void OnEndPage(PdfWriter writer, Document document)
179 // {
180 // PdfContentByte cb = writer.DirectContent;
181 // ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT, header,
182 // (document.Right - document.Left) / 2 + document.LeftMargin, document.Top + 6, 0);
183 // }
184 //}
185 }
186
  这样调用即可:

代码
1 //创建PDF
2 iTextSharp.text.Document doc = new iTextSharp.text.Document();
3 //随机文件名(可无视)
4 string savefilename = Imdingji.Tools.MyFile.GetRandomName() + ".pdf";
5 //获得文件在服务器上的物理路径
6 string path = HttpContext.Current.Server.MapPath("~/ExportFiles/");
7 path += savefilename;
8 doc = Imdingji.Tools.MyPDF.CreatePDFDoc(path, Session["username"].ToString(), lit_comName.Text, "上海市科技小巨人(培育)企业中期检查情况调查表", false);
9 //页眉
10 Imdingji.Tools.MyPDF.SetHeader(doc, CompanyModel.CompanyName);
11 doc.Open();
12 //title
13 iTextSharp.text.Paragraph p = Imdingji.Tools.MyPDF.CreatePara("上海市科技小巨人(培育)企业中期检查情况调查表");
14 p.Alignment = 1;
15 p.Font.Size = 14;
16 //title table
17 iTextSharp.text.pdf.PdfPTable Ttb = Imdingji.Tools.MyPDF.CreateTable(2);
18 Ttb.TotalWidth = 500f;
19 float[] Twidths = { 350f, 150f };
20 Ttb.SetWidths(Twidths);
21 iTextSharp.text.pdf.PdfPCell Tcell;
22 Tcell = Imdingji.Tools.MyPDF.CreateCell("企业名称(公章):"); Tcell.Border = 0; Ttb.AddCell(Tcell);
23 Tcell = Imdingji.Tools.MyPDF.CreateCell("填表人:"); Tcell.Border = 0; Ttb.AddCell(Tcell);
24 //段落一
25 iTextSharp.text.Paragraph p1 = Imdingji.Tools.MyPDF.CreatePara("一、企业基本情况");
26 p1.Alignment = 0;
27 p1.Font.Size = 10;
28 //企业基本情况表
29 iTextSharp.text.pdf.PdfPTable Ctb = Imdingji.Tools.MyPDF.CreateTable(4);
30 Ctb.TotalWidth = 500f;
31 float[] Cwidths = { 125f, 125f, 125f, 125f };
32 Ctb.SetWidths(Cwidths);
33 iTextSharp.text.pdf.PdfPCell Ccell;
34 Ccell = Imdingji.Tools.MyPDF.CreateCell("立项编号:"); Ctb.AddCell(Ccell);
35 Ccell = Imdingji.Tools.MyPDF.CreateCell(CompanyModel.ProjectCode); Ccell.Colspan = 3; Ctb.AddCell(Ccell);
36 Ccell = Imdingji.Tools.MyPDF.CreateCell("企业名称:"); Ctb.AddCell(Ccell);
37 Ccell = Imdingji.Tools.MyPDF.CreateCell(CompanyModel.CompanyName); Ccell.Colspan = 3; Ctb.AddCell(Ccell);
38 //段落二
39 iTextSharp.text.Paragraph p2 = Imdingji.Tools.MyPDF.CreatePara("二、企业经济情况");
40 p2.Alignment = 0;
41 p2.Font.Size = 10;
42 //企业经济情况表
43 iTextSharp.text.pdf.PdfPTable tb = Imdingji.Tools.MyPDF.CreateTable(5);
44 tb.SplitLate = false;
45 tb.SplitRows = false;
46 tb.TotalWidth = 500f;
47 float[] widths = { 123.75f, 151.25f, 75f, 75f, 75f };
48 tb.SetWidths(widths);
49 iTextSharp.text.pdf.PdfPCell cell;
50 cell = Imdingji.Tools.MyPDF.CreateCell("年份"); cell.Column.Alignment = 1; cell.Colspan = 2; tb.AddCell(cell);
51 cell = Imdingji.Tools.MyPDF.CreateCell(Yearname1); cell.Column.Alignment = 1; tb.AddCell(cell);
52 cell = Imdingji.Tools.MyPDF.CreateCell(Yearname2); cell.Column.Alignment = 1; tb.AddCell(cell);
53 cell = Imdingji.Tools.MyPDF.CreateCell(Yearname3); cell.Column.Alignment = 1; tb.AddCell(cell);
54 cell = Imdingji.Tools.MyPDF.CreateCell("企业经济情况(万元)"); cell.Rowspan = 13; tb.AddCell(cell);
55 cell = Imdingji.Tools.MyPDF.CreateCell("主营业收入"); tb.AddCell(cell);
56 doc.Add(p);
57 doc.Add(Ttb);
58 doc.Add(p1);
59 doc.Add(Ctb);
60 doc.Add(p2);
61 doc.Add(tb);
62 doc.Add(p3);
63 doc.Add(Gtb);
64 //Imdingji.Tools.MyPDF.SetFoot(doc);
65 doc.Close();
66
67 Imdingji.Tools.MyFile.DownloadFile(path);
68 Imdingji.Tools.MyFile.deleteFile(path);
69

 


分类:

技术点:

相关文章:

  • 2021-12-05
  • 2021-09-10
  • 2022-01-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
  • 2021-12-15
猜你喜欢
  • 2021-11-27
  • 2021-12-24
  • 2021-12-24
  • 2021-09-27
  • 2021-12-28
  • 2021-07-15
  • 2021-06-02
相关资源
相似解决方案