最近看了在ASP.NET中如何使用word文档模板制作word文档,学习之余,便收集学习了如何在ASP.NET MVC中制作pdf文档的案例教程,做一个阶段性的总结
说明:使用了iTextSharp 类库,下载位置:http://sourceforge.net/projects/itextsharp/ ,功能说明
- Create:Automate、Convert、Sign、Encrypt。
- Read:Extract。
- Update:Stamp、Fill out、Split/Merge、Convert、Sign、Encrypt。更多请跳转http://sourceforge.net/projects/itextsharp/#resources
开发环境
- 我安装的IDE是Visul Studio2012 ,
- iTextSharp 的版本是5.5.6。
part 1、使用入门
- 添加以下引用,由于会使用到MemoryStream,所以要加上System.IO
1 using System.IO;
2 using iTextSharp.text;
3 using iTextSharp.text.pdf;
- 在这个案例中,我们利用web application在Server Memory产生pdf文档后 ,使用者可以自行下载浏览或存档,采用的是PdfWriter类別,如果是在ASP.NET MVC中,可以将产生的pdf文档存于服务器端,可根据客户的需求进行下载,也可将pdf文档转化成html文件,直接在浏览器中浏览
1 var doc = new Document(PageSize.A4, 50, 50, 80, 50); //设置页面大小及边距
2 MemoryStream Memory = new MemoryStream();
3 PdfWriter pdfWriter = PdfWriter.GetInstance(doc, Memory);
- 如果要在服务器端直接生成文件,可以使用下面的方式
1 string path = System.Web.HttpContext.Current.Server.MapPath(filePath);
2 if (!Directory.Exists(path))
3 {
4 Directory.CreateDirectory(path);
5 }
6 PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(path + "/pdfexample.pdf", FileMode.Create));
- 在PDF文档內容中要显示中文,最重要的是字型设置,如果沒有正确设置中文字型,会造成中文无法显示的问题。首先设置基本字型:kaiu.ttf 是操作系統提供的标楷体字型,IDENTITY_H 是指编码(The Unicode encoding with horizontal writing),及是否要将字型嵌入PDF 档中。再來针对基本字型做变化,例如Font Size、粗体斜体以及颜色等。
1 //字型设定
2 BaseFont bfChinese = BaseFont.CreateFont(@"C:\Windows\Fonts\kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
3 Font ChFont = new Font(bfChinese, 12);
4 Font ChFont_blue = new Font(bfChinese, 40, Font.NORMAL, new BaseColor(51, 0, 153));
5 Font ChFont_msg = new Font(bfChinese, 12, Font.ITALIC, BaseColor.RED);
- 写入内容后记得要记得关闭哟
1 //写入内容
2 doc.Open();
3 doc.AddAuthor("余智平");
4 doc.AddCreationDate();
5 doc.AddTitle("余智平制作");
6 doc.Add(new Paragraph(10f, "Hello 大家好!", ChFont_blue));
7 doc.Close();
- 若要将pdf文档在客户端显示,并让用户可下载,下面的代码必不可少,将档案输出到浏览器端
1 Response.Clear();
2 Response.AddHeader("Content-Disposition", "attachment;filename=pdfExample.pdf");
3 Response.ContentType = "application/octet-stream";
4 Response.OutputStream.Write(Memory.GetBuffer(), 0, Memory.GetBuffer().Length);
5 Response.OutputStream.Flush();
6 Response.OutputStream.Close();
7 Response.Flush();
8 Response.End();
- 完整的代码如下:
1 const string filePath = "/FileFolder/pdfFiles";
2 protected void Page_Load(object sender, EventArgs e)
3 {
4 var doc = new Document(PageSize.A4, 50, 50, 80, 50); //设置页面大小及边距
5 MemoryStream Memory = new MemoryStream();
6 PdfWriter pdfWriter = PdfWriter.GetInstance(doc, Memory);
7 //若想将文档存于文件系统
8 //string path = System.Web.HttpContext.Current.Server.MapPath("pdf");
9 //if (!Directory.Exists(path))
10 //{
11 // Directory.CreateDirectory(path);
12 //}
13 //PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(path + "/pdfexample.pdf", FileMode.Create));
14
15
16 //字型设定
17 BaseFont bfChinese = BaseFont.CreateFont(@"C:\Windows\Fonts\kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
18 Font ChFont = new Font(bfChinese, 12);
19 Font ChFont_blue = new Font(bfChinese, 40, Font.NORMAL, new BaseColor(51, 0, 153));
20 Font ChFont_msg = new Font(bfChinese, 12, Font.ITALIC, BaseColor.RED);
21
22 //写入内容
23 doc.Open();
24 doc.AddAuthor("余智平");
25 doc.AddCreationDate();
26 doc.AddTitle("余智平制作");
27 doc.Add(new Paragraph(10f, "Hello 大家好!", ChFont_blue));
28 doc.Close();
29 Response.Clear();
30 Response.AddHeader("Content-Disposition", "attachment;filename=pdfExample.pdf");
31 Response.ContentType = "application/octet-stream";
32 Response.OutputStream.Write(Memory.GetBuffer(), 0, Memory.GetBuffer().Length);
33 Response.OutputStream.Flush();
34 Response.OutputStream.Close();
35 Response.Flush();
36 Response.End();
37 }
结果展示:
总结:一个简单的案例就完成了,还有很多很多的学习内容,未完,未来几天继续更贴