VS2008 使用itextsharp生成PDF

废话不多说,上图先。

VS2008 使用itextsharp生成PDF

要用本文的方法生成PDF文件,需要两个控件:itextsharp.dll和ICSharpCode.SharpZipLib.dll,由于示例代码实在太多,我将代码全部整理出来.
利用iText五步创建一个PDF文件:helloword。
第一步,创建一个 iTextSharp.text.Document对象的实例:
Document document = new Document();
第二步,为该Document创建一个Writer实例:
PdfWriter.getInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));
第三步,打开当前Document
document.Open();
第四步,为当前Document添加内容:
document.Add(new Paragraph("Hello World"));
第五步,关闭Document
document.Close();

核心代码:

using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; protected void Button1_Click(object sender, EventArgs e) { Document document = new Document(); try { // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter writer = PdfWriter.getInstance(document, new FileStream(Server.MapPath("Chap1003.pdf"), FileMode.Create)); // step 3: we open the document document.Open(); //字体参数设置 BaseFont bfChinese = BaseFont.createFont(@"C:\WINDOWS\Fonts\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //微软雅黑 BaseFont bfChinese1 = BaseFont.createFont(@"C:\WINDOWS\Fonts\MSYHBD.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese1, 8, Font.ITALIC, new Color(0x66, 0x66, 0x66)); // step 4: we grab the ContentByte and do some stuff with it PdfContentByte cb = writer.DirectContent; //图片 iTextSharp.text.Image jpeg = iTextSharp.text.Image.getInstance(Server.MapPath("images/logo.jpg")); //页脚页眉 HeaderFooter footer = new HeaderFooter(new Phrase("Copyright © 2004-2009 DriveTOP, Inc. All Rights Reserved. ",fontChinese), true); footer.Border = Rectangle.NO_BORDER; document.Footer = footer; HeaderFooter header = new HeaderFooter(new Phrase("驱动管理咨询"), false); document.Header = header; // 为第一页增加一个水印 try { Watermark watermark = new Watermark(jpeg, 100, 804); document.Add(watermark); } catch { Response.Write("Are you sure you have the file 'watermark.jpg' in the right path?"); } ; // we create a PdfTemplate PdfTemplate template = cb.createTemplate(500, 200); // we add some text template.beginText(); //BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); template.setFontAndSize(bfChinese, 12); template.setTextMatrix(100, 10); template.showText("驱动管理咨询"); template.endText(); //添加块 Chunk chunk = new Chunk("", FontFactory.getFont( FontFactory.TIMES_BOLD, 30, Font.BOLD, new Color(255, 0, 0))); Phrase ph1 = new Phrase(); ph1.Add(chunk); //添加段落 string str1 = "封面驱动管理咨询"; Font font1 = new Font(bfChinese, 24); Paragraph pa1 = new Paragraph(str1, font1); ////////封面 document.Add(ph1); document.Add(pa1); //添加图片 iTextSharp.text.Image gif = iTextSharp.text.Image.getInstance(Server.MapPath("images/plogo.jpg")); gif.scalePercent(75); document.Add(gif); //添加文字 String stxt = "面试仿真测试反馈报告"; Font sfont = new Font(bfChinese, 44); document.Add(new Paragraph(stxt, sfont)); document.resetPageCount(); // we go to a new page 第一页 document.newPage(); cb.addTemplate(template, 350, 798); //cb.addTemplate(template, 2, 0, 0, 2, -200, 400); //添加文字 stxt = " 面试仿真测试报告"; Font sfontTitle = new Font(bfChinese1, 22, Font.NORMAL, new Color(0, 0, 0)); Font sfont2Title = new Font(bfChinese, 14, Font.NORMAL, new Color(0, 0, 0)); Font sfont3Title = new Font(bfChinese, 14, Font.NORMAL, new Color(255, 255, 255)); Font sfont4Title = new Font(bfChinese, 12, Font.NORMAL, new Color(0, 0, 0)); document.Add(new Paragraph(stxt, sfontTitle)); document.Add(new Paragraph("受评鉴人: 周洪波", sfont2Title)); //添加块 Chunk chunk2 = new Chunk("内容纲要", sfont3Title); //chunk2.setBackground(new Color(0x77, 0x77, 0x77)); Phrase ph2 = new Phrase(); ph2.Add(chunk2); //添加块 Chunk chunk3 = new Chunk("1、报告阅读说明与模型结构", sfont3Title); Phrase ph3 = new Phrase(); ph3.Add(chunk3); //添加块 Chunk chunk4 = new Chunk("2、答题的真实性系数", sfont3Title); Phrase ph4 = new Phrase(); ph4.Add(chunk4); //添加块 Chunk chunk5 = new Chunk("3、各项能力素质指标得分", sfont3Title); Phrase ph5 = new Phrase(); ph5.Add(chunk5); //添加块 Chunk chunk6 = new Chunk("4、了解更多", sfont3Title); Phrase ph6 = new Phrase(); ph6.Add(chunk6); //添加块 Chunk chunk7 = new Chunk(" — 本报告结果由驱动管理咨询提供 —", sfont3Title); Phrase ph7 = new Phrase(); ph7.Add(chunk7); //加表格 atable = new iTextSharp.text.Table(1, 1); atable.WidthPercentage = 100; atable.Spacing = 0; //表格字距;行距 atable.Padding = 4; //表格边距 atable.BorderWidth = 1; atable.addCell(ph3); atable.BackgroundColor = new Color(0x77, 0x77, 0x77); atable.BorderColor = new Color(255, 255, 255); document.Add(atable); //加文字 document.Add(new Paragraph("【关于能力素质】", sfont4Title)); document.Add(new Paragraph(" 能力素质是导致一件事情成功的关键因素,是某种深层动力引起的优秀行为。它与技能不同,必需来自内在动力的驱使。比如:成就导向、影响能力、客观积极...", sfont4Title)); //加表格 atable = new iTextSharp.text.Table(1, 1); atable.WidthPercentage = 100; atable.Spacing = 0; //表格字距;行距 atable.Padding = 4; //表格边距 atable.BorderWidth = 1; atable.addCell(ph4); atable.BackgroundColor = new Color(0x77, 0x77, 0x77); atable.BorderColor = new Color(255, 255, 255); document.Add(atable); //加文字 document.Add(new Paragraph("周洪波 的答题真实性系数为85.00%", sfont4Title)); document.Add(new Paragraph(" 真实性系数是判断个人真实有效答题的程度。真实性系数大于等于80%为高可信度;真实性系数介于60%到80%之间为一般可信度;真实性系数小于60%为较弱可信度。", sfont4Title)); // we go to a new page 第二页 document.newPage(); cb.addTemplate(template, 350, 798); //加表格 atable = new iTextSharp.text.Table(1, 1); atable.WidthPercentage = 100; atable.Spacing = 0; //表格字距;行距 atable.Padding = 4; //表格边距 atable.BorderWidth = 1; atable.addCell(ph5); atable.BackgroundColor = new Color(0x77, 0x77, 0x77); atable.BorderColor = new Color(255, 255, 255); document.Add(atable); //加文字 document.Add(new Paragraph("下方图表显示的是该受评鉴人在各项能力素质指标上的分数。", sfont4Title)); iTextSharp.text.Image png = iTextSharp.text.Image.getInstance(Server.MapPath("test.jpg")); gif.scalePercent(50); document.Add(png); document.Add(new Paragraph("备注:你所看到的得分已经转换为百分数,表示该受评鉴人在目前接受过该评鉴的人群中的各项指标排名相对位置。(假如该受评鉴人在某项指标上的得分为92.12,代表有92.12%的人在该项上的得分要比该人低)。", sfont4Title)); //加表格 atable = new iTextSharp.text.Table(1, 1); atable.WidthPercentage = 100; atable.Spacing = 0; //表格字距;行距 atable.Padding = 4; //表格边距 atable.BorderWidth = 1; atable.addCell(ph6); atable.BackgroundColor = new Color(0x77, 0x77, 0x77); atable.BorderColor = new Color(255, 255, 255); document.Add(atable); //加文字 document.Add(new Paragraph("想了解更多,请关注 http://sss.drivetop.com.cn", sfont4Title)); //加表格 atable = new iTextSharp.text.Table(1, 1); atable.WidthPercentage = 100; atable.Spacing = 0; //表格字距;行距 atable.Padding = 4; //表格边距 atable.BorderWidth = 1; atable.addCell(ph7); atable.BackgroundColor = new Color(0x77, 0x77, 0x77); atable.BorderColor = new Color(255, 255, 255); document.Add(atable); } catch (DocumentException de) { Response.Write(de.Message); } catch (IOException ioe) { Response.Write(ioe.Message); } // step 5: we close the document document.Close(); }

项目源码下载地址:

 http://download.csdn.net/source/1732654  (需注册)

转载于:https://www.cnblogs.com/Renn/archive/2009/10/11/2048400.html