並得出了兩種解決方案.一種是舊方法,另一種是新方法.
剛試驗成功趕緊分享經驗,避免其它人再走彎路,實在是太浪費時間了.
試驗的材料:
dynamicreports-3.2.1-.zip (官網下載的報表jar及官方例子) 官網:http://www.dynamicreports.org/
iTextAsian.jar 注意:pdf的字體iTextAsian的jar不是itextpdf包 (在方法一中需要使用.)下載地址:http://yunpan.cn/cf6q7vSU5bqjM 提取碼 9ba6 或者csdn下載:http://download.csdn.net/detail/ssxueyi/4659874
試驗準備:
將dynamicreports-3.2.1-.zip解壓,在目錄中examples/eclipse/dynamicreports-examples有eclipse項目官方例子(幾十個).將它加載到eclipse中.
方法一:
將iTextAsian.jar包,放入到eclipse的lib中,在例子中的Templates.java中給樣式中加入字體設置.可以給其中的rootStyle加入字體設置,這樣對全局的字體都是有效的.
rootStyle = stl.style().setPadding(2)
.setFont(stl.font()
.setPdfFontName("STSong-Light")
.setPdfEncoding("UniGB-UCS2-H")
.setPdfEmbedded(BaseFont.NOT_EMBEDDED));
如下圖所示,因爲這些方法都過期了,所以有刪除線:
方法二(推薦方法):
從電腦系統中的C:\Windows\Fonts目錄下,複製一些你需要的字體文件(字體需要支持中文的,否則顯示效果有問題).複製到項目中(我複製了兩個字體),比如我複製的字體SIMHEI.TTF,複製到位置net/sf/dynamicreports/examples/fonts/SIMHEI.TTF
<fontFamilies>
<fontFamily name="FreeUniversal">
<normal>net/sf/dynamicreports/examples/fonts/SIMFANG.TTF</normal>
<bold>net/sf/dynamicreports/examples/fonts/SIMFANG.TTF</bold>
<italic>net/sf/dynamicreports/examples/fonts/SIMFANG.TTF</italic>
<boldItalic>net/sf/dynamicreports/examples/fonts/SIMFANG.TTF</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
</fontFamily>
<fontFamily name="FreeUniversalHei">
<normal>net/sf/dynamicreports/examples/fonts/SIMHEI.TTF</normal>
<bold>net/sf/dynamicreports/examples/fonts/SIMHEI.TTF</bold>
<italic>net/sf/dynamicreports/examples/fonts/SIMHEI.TTF</italic>
<boldItalic>net/sf/dynamicreports/examples/fonts/SIMHEI.TTF</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
</fontFamily>
<fontFamily name="FreeUniversalTest">
<normal>net/sf/dynamicreports/examples/fonts/FreeUniversal-Regular.ttf</normal>
<bold>net/sf/dynamicreports/examples/fonts/FreeUniversal-Regular.ttf</bold>
<italic>net/sf/dynamicreports/examples/fonts/FreeUniversal-Regular.ttf</italic>
<boldItalic>net/sf/dynamicreports/examples/fonts/FreeUniversal-Regular.ttf</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
</fontFamily>
</fontFamilies>
這個配置文件jasperreports_extension.properties不用修改.使用默認的就可以.
修改Templates.java文件中的樣式.FreeUniversalHei這個與配置文件中的customfonts.xml的fontFamily name="FreeUniversalHei"對應
rootStyle = stl.style().setPadding(2).setFontName("FreeUniversalHei");
再運行官方的例子,輸出pdf格式,可以輸出中文.參考資料:
http://www.oschina.net/question/552008_51866
http://www.dynamicreports.org/forum/viewtopic.php?f=1&t=195
https://github.com/BriteNSW/msttcorefonts/blob/master/src/main/resources/com/britensw/msttcorefonts/fonts.xml
http://www.dynamicreports.org/documentation/fonts
[注意]:方法二中選擇的字體一定要支持中文,否則也顯示不出來中文。