使用ITextSharper小结
用到了生成PDF版的合同,上网研究了一圈,发现不需要服务器端安装插件的,比较好用的就是这个ITextSharper了,于是便开始了研究。
1.解决汉字不显示的问题,指定一下字体,默认的字体好像不支持中文,C:\Windows\Fonts\simsun.ttc,这是字体的路径。
BaseFont songTiFont = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//宋体 BaseFont heiTiFont = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//黑体 var titlePara = new iTextSharp.text.Paragraph("借 款 合 同", new Font(heiTiFont, 22, iTextSharp.text.Font.BOLD));
2.PDF中基本的概念:
从大到小:Document文档>Paragraph段落>Phrase短句>Chunk块
这个概念跟HTML差不多,Document=<Html>,Paragraph=<P> ,Phrases=<div>,Chunk=<Span>,
Phrase这个我可能理解的不太到位,大家参考就好了,音译过来是短句的意思,是一系列以特定间距(两行之间的距离)作为参数的块,一个短句有一个主字体,但短句中的一些块具有不同于主字体的字体,你有更多的选择去创建短句(网上抄的)。
这个博文讲的比较好,值得参考:http://www.cnblogs.com/LifelongLearning/archive/2011/03/30/2000072.html
3.设置
我用下划线后发现两行之间距离有点近了,研究了半天,发现在创建Paragraph的时候去指定一下就好了。
var loanerPara = new Paragraph(25);
需要左对齐或者右对齐的时候,使用下面的这句
titlePara.Alignment = Element.ALIGN_CENTER;
4.基本写法
懒得去分解了,贴了一部分代码上来。基本思路是,创建Document->指定大小(如A4)->创建实例->打开文档->创建段落(中文一定要指定字体)->创建块->把块加到段落中->把段落加到文档中 。
private void button1_Click(object sender, EventArgs e) { string fileName = string.Empty; SaveFileDialog dlg =new SaveFileDialog(); dlg.FileName = "借款合同"; dlg.DefaultExt = ".pdf"; dlg.Filter = "Text documents (.pdf)|*.pdf"; var result = dlg.ShowDialog(); if (result ==DialogResult.OK) { fileName = dlg.FileName; Document document = new Document(PageSize.A4);//从大到小:Document文档>Paragraph段落>Chunk块 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create)); document.Open(); // BaseFont songTiFont = BaseFont.CreateFont(@"H:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//宋体 //BaseFont heiTiFont = BaseFont.CreateFont(@"H:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//黑体 #region 生成标题 var titlePara = new iTextSharp.text.Paragraph("借 款 合 同", new Font(heiTiFont, 22, iTextSharp.text.Font.BOLD)); titlePara.Alignment = Element.ALIGN_CENTER; document.Add(titlePara); #endregion #region 借款号 var loanNumberPara = new Paragraph(string.Format("{0}借字第{1}号",DateTime.Now.Year,00001),new Font(songTiFont,12)); loanNumberPara.Alignment = Element.ALIGN_RIGHT;//设置右对齐 document.Add(loanNumberPara); #endregion #region 出借人 var investPara=new Paragraph(25); investPara.Add(CreateSongtiBoldChunk("出借人")); investPara.Add(CreateSongtiNomalChunk("(以下简称甲方):")); investPara.Add(CreateSongtiUnderLineChunk(" 网络名(昵称) ")); document.Add(investPara); #endregion #region 借款人 var loanerPara = new Paragraph(25); loanerPara.Add(CreateSongtiBoldChunk("借款人")); loanerPara.Add(CreateSongtiNomalChunk("(以下简称乙方):")); loanerPara.Add(CreateSongtiUnderLineChunk(" ")); loanerPara.Add(CreateSongtiNomalChunk("身份证号:")); loanerPara.Add(CreateSongtiSpeaceUnderLineChunk(20)); document.Add(loanerPara); #endregion #region 通讯地址 var addressPara = new Paragraph(25); addressPara.Add(CreateSongtiNomalChunk("通讯地址:")); addressPara.Add(CreateSongtiSpeaceUnderLineChunk(40)); addressPara.Add(CreateSongtiNomalChunk("邮编:")); addressPara.Add(CreateSongtiSpeaceUnderLineChunk(10)); document.Add(addressPara); #endregion #region 电话邮箱 document.Close(); } } BaseFont songTiFont = BaseFont.CreateFont(@"H:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//宋体 BaseFont heiTiFont = BaseFont.CreateFont(@"H:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//黑体 public Chunk CreateSongtiNomalChunk(string text) { Chunk chunk = new Chunk(text, new Font(songTiFont, 15, iTextSharp.text.Font.NORMAL)); return chunk; } public Chunk CreateSongtiBoldChunk(string text) { Chunk chunk = new Chunk(text, new Font(songTiFont, 15, iTextSharp.text.Font.BOLD)); return chunk; } public Chunk CreateSongtiUnderLineChunk(string text) { Chunk chunk = new Chunk(text, new Font(songTiFont, 15, iTextSharp.text.Font.UNDERLINE)); return chunk; } public Chunk CreateSongtiSpeaceUnderLineChunk(int length) { StringBuilder sb=new StringBuilder(); for (int i = 0; i < length; i++) { sb.Append(" "); } return CreateSongtiUnderLineChunk(sb.ToString()); }
西安.NET俱乐部群