源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C# 中使用iTextSharp组件创建PDF的简单方法

  • 时间:2020-07-08 20:27 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C# 中使用iTextSharp组件创建PDF的简单方法
[img]http://files.jb51.net/file_images/article/201303/2013317122228354.jpg[/img] 将iTextSharp.dll文件拷贝到项目的bin目录,然后在项目中添加引用: [img]http://files.jb51.net/file_images/article/201303/2013317122405474.jpg[/img] 然后在后台代码添加引用:
[u]复制代码[/u] 代码如下:
using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; using System.Diagnostics; //创建PDF  private void CreatePdf()  {      //定义一个Document,并设置页面大小为A4,竖向      iTextSharp.text.Document doc = new Document(PageSize.A4);      try      {          //写实例          PdfWriter.GetInstance(doc, new FileStream("D:\\Hello.pdf", FileMode.Create));          #region 设置PDF的头信息,一些属性设置,在Document.Open 之前完成          doc.AddAuthor("作者幻想Zerow");          doc.AddCreationDate();          doc.AddCreator("创建人幻想Zerow");          doc.AddSubject("Dot Net 使用 itextsharp 类库创建PDF文件的例子");          doc.AddTitle("此PDF由幻想Zerow创建,嘿嘿");          doc.AddKeywords("ASP.NET,PDF,iTextSharp,幻想Zerow");          //自定义头          doc.AddHeader("Expires", "0");          #endregion //打开document          doc.Open();          //载入字体          BaseFont.AddToResourceSearch("iTextAsian.dll");          BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");          //"UniGB-UCS2-H" "UniGB-UCS2-V"是简体中文,分别表示横向字 和 // 纵向字 //" STSong-Light"是字体名称          BaseFont baseFT = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);          iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT); //写入一个段落, Paragraph          doc.Add(new Paragraph("您好, PDF !", font));          //关闭document          doc.Close();          //打开PDF,看效果          Process.Start("D:\\Hello.pdf");      }      catch (DocumentException de) { Console.WriteLine(de.Message); Console.ReadKey(); }      catch (IOException io) { Console.WriteLine(io.Message); Console.ReadKey(); }  }
[img]http://files.jb51.net/file_images/article/201303/2013317122519661.jpg[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部