相信每个业务系统都有报表输出的需要,一般采用第三方报表工具,如水晶报表等等。我们也可以直接采用PDF生成类库类实现,不过代码要写多些。

使用.NET开发的生成PDF文件的类库不少,我之前也介绍过iTextSharp支持中文,但来得不直接,要通过BaseFont.CreateFont()来实现:

利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件   BaseFont baseFont =
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件    BaseFont.CreateFont(
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件    
"C:\\WINDOWS\\FONTS\\SIMHEI.TTF",
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件    BaseFont.IDENTITY_H,
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件    BaseFont.NOT_EMBEDDED);
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件   Font font 
= new Font(baseFont, 9);
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件


这里有一个缺点,就是必须指定字体路径,而Environment.SpecialFolder却不包含字体路径,其它获取方法也是有不少的,如用Win32 API等,也可以来点野蛮的:

利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件 Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\..\Fonts\"


PDF函数:

利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件        
public static bool ConvertDataTableToPDF(DataTable Data, string PDFFile, string FontPath, float FontSize)

PDF调用:
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件ConvertDataTableToPDF(Table, @".\test.pdf""C:\\WINDOWS\\FONTS\\SIMSUN.TTC,1"9); //这里使用宋体,大小9pt。


RTF函数:
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件        
public static bool ConvertDataTableToRtf(DataTable Data, string RtfFile, string FontPath, float FontSize)

RTF调用:
利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件ConvertDataTableToRtf(Table, @".\test.rtf""C:\\WINDOWS\\FONTS\\SIMSUN.TTC,1"9);


生成PDF和RTF的代码是不是很类似?代码很直接,大家根据自己的实际需要改造吧。

生成出来的PDF和RTF在大小上差异很大:6个字段*2000条记录,PDF为200k左右,RTF竟然达到5.5M左右。大家衡量使用什么吧。

btw.iTextSharp刚推出了3.0.7版本:New release iTextSharp 3.0.7 (2005-08-24)

参考:
1、itext官方教程字体部分:http://itextdocs.lowagie.com/tutorial/fonts/index.html
2、iTextSharp全攻略:http://www.zzchina.net/article/17/16571.htm
3、iTextSharp官方教程代码:iTextSharp (iText#) tutorial update version

相关文章:

  • 2022-02-06
  • 2021-11-19
  • 2022-12-23
  • 2021-10-27
  • 2021-08-18
  • 2021-12-17
  • 2022-02-14
  • 2021-11-07
猜你喜欢
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2021-12-04
相关资源
相似解决方案