现在的位置: 首页 > 综合 > 正文

ireport 4.1.1 导出PDF 引入外部中文字体

2013年03月26日 ⁄ 综合 ⁄ 共 1542字 ⁄ 字号 评论关闭

版本:iReport4.1.1
iReport默认情况下是不支持中文的,即便是自带的STSong-Light,虽能在预览视图里良好的显示正常、粗体、斜体、下划线等,但部署到应用后部分功能无效果,只能以正常字体显示。

如需完整使用以上的功能,需要装中文字体包导入到iReprot软件里去。
步骤如下:
安装字体:在菜单工具(tools)---->选项--->iReport---->fonts--->installfonts--->browse--->选择中文字体文件(simfang.ttf)

中文字体文件可以在系统文件夹 C:\WINDOWS\Fonts 找到你需要的字体。这里以 仿宋_GB2312 为例,如图选择相应的值。


--->下一步--->下一步--->下一步--->完成
这时在Fonts窗口中可以看到刚安装的字体了。


导出字体:选中刚安装的字体----Export as extension将刚导入的字体打包成*.jar文件导出。

根据个人习惯,我把它放在 C:\Program Files\Jaspersoft\iReport-4.1.1\ireport\fonts 文件夹下。
引用字体:切换到Classpath选项卡下,add jar---->找到刚导出的字体包将其导入,并勾上Reloadable。


这样我们就在iReprot里面可以用这个字体了。

使用字体时应注意

以调色板中的staticText为例:向页面上拖放一个staticText组件选中此组件,在属性窗口中找到如下几个属性
1,Font name 选择安装字体时所显示的名称,这里我选择的是仿宋_GB2312

2.PdfFontName, PdfEncoding, PdfEmbedded这三个属性全部清除。只需设置Font name一个即可。具体如下:

点击打开链接

If you have packed your TTFs in a font extension JAR, there is no reason for using the pdfFontName attributes in your JRXML.
Please remove them and make sure text fields have fontName, or have the fontName be inherited from a report default style.
Note that the three attributes pdfFontName, pdfEncoding, and isPdfEmbedded are now deprecated (replaced by settings inside font extensions).

I have tested your report and font extension JAR with iReport and it worked. Are you sure your font extension JAR is in the classpath at the time the PDF export is performed?
Is the PDF export occurring inside the same application that does the report filling?

注意。每一个要显示中文字体的组件都需要这样设定。这样就大功告成。

在应用程序中,同样需将导出的字体jar包放入classpath文件夹下。

实际上,按照以上的步骤完成后,在应用中仍然会报错FontFormatException。此问题困扰我很久很久。

最后发现在ireport 4.1.1版本中,使用Export as extension导出的字体jar包根本无法使用。里面的simfang.ttf文件大小竟然为0字节。不知道到底是谁的错误导致的。

无奈之下,手动将原始大小的simfang.ttf替换掉它。这才万事大吉。

抱歉!评论已关闭.