系統(tǒng)軟件園 - 專注分享最好的電腦系統(tǒng)軟件!免費安全下載 系統(tǒng)軟件園首頁 | Win7激活工具 | 熱門專題
系統(tǒng)軟件園>您的位置:首頁 > > 系統(tǒng)教程 > Win7系統(tǒng)教程 >

win7系統(tǒng)下JFreeChart1.0.1和iText集成時,中文字符出現亂碼的解決方法

更新時間:2017-06-28 14:29:39| 編輯:本站整理 | 信息來源:本站整理|瀏覽數:
環(huán)境:JFreeChart 1.0.1、iText1.3
 
JFreeChart1.0.1中的Plot接口沒有提供SetValueLabelFont的方法,無法直接設置label的字體!
不過還好試了一下午總算弄出來了,具體代碼如下:
 
PdfWriter writer;
writer = PdfWriter.getInstance(document, new FileOutputStream(
filename));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);

// 關鍵是使用AsianFontMapper替代DefaultFontMapper
AsianFontMapper mapper = new AsianFontMapper("STSong-Light","UniGB-UCS2-H");

Graphics2D g2d = tp.createGraphics(width, height,
mapper);
Rectangle2D r2d = new Rectangle2D.Double(00, width, height);
chart.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp, 00);

記住別忘了添加iTextAisan.jar不然會報如下錯誤:下載地址:iTextAsian.jar下載(32&64位)

com.lowagie.text.DocumentException: Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.
at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source)
at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source)
at com.lowagie.text.pdf.AsianFontMapper.awtToPdf(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.getCachedBaseFont(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.setFont(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.(Unknown Source)
at com.lowagie.text.pdf.PdfContentByte.createGraphics(Unknown Source)
at JFreeChartExample.convertToPdf(JFreeChartExample.Java:94)
at JFreeChartExample.main(JFreeChartExample.java:63)
com.lowagie.text.DocumentException: Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.
at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source)
at com.lowagie.text.pdf.BaseFont.createFont(Unknown Source)
at com.lowagie.text.pdf.AsianFontMapper.awtToPdf(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.getCachedBaseFont(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.setFont(Unknown Source)
at org.jfree.chart.title.TextTitle.arrangeRR(TextTitle.java:488)
at org.jfree.chart.title.TextTitle.arrange(TextTitle.java:450)
at org.jfree.chart.JFreeChart.drawTitle(JFreeChart.java:1125)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1015)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:925)
at JFreeChartExample.convertToPdf(JFreeChartExample.java:97)
at JFreeChartExample.main(JFreeChartExample.java:63)
Exception in thread "main" java.lang.NullPointerException
at com.lowagie.text.pdf.PdfWriter.addSimple(Unknown Source)
at com.lowagie.text.pdf.PdfContentByte.setFontAndSize(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.drawString(Unknown Source)
at org.jfree.text.TextUtilities.drawRotatedString(TextUtilities.java:566)
at org.jfree.text.TextUtilities.drawRotatedString(TextUtilities.java:600)
at org.jfree.text.TextFragment.draw(TextFragment.java:212)
at org.jfree.text.TextLine.draw(TextLine.java:174)
at org.jfree.text.TextBlock.draw(TextBlock.java:269)
at org.jfree.text.TextBlock.draw(TextBlock.java:231)
at org.jfree.chart.title.TextTitle.drawHorizontal(TextTitle.java:628)
at org.jfree.chart.title.TextTitle.draw(TextTitle.java:569)
at org.jfree.chart.JFreeChart.drawTitle(JFreeChart.java:1128)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1015)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:925)
at JFreeChartExample.convertToPdf(JFreeChartExample.java:97)
at JFreeChartExample.main(JFreeChartExample.java:63)


返回頂部


系統(tǒng)軟件園發(fā)布的系統(tǒng)鏡像及軟件均來至互聯網,僅供學習和研究使用,不得用于任何商業(yè)用途并請在下載后24小時內刪除,如果滿意請聯系版權方購買。
如果您發(fā)現本站侵害了您的版權,請立即聯系我們,本站將第一時間進行相關處理。聯系方式(見首頁)
版權聲明|下載聲明 Copyright @ 2016 系統(tǒng)軟件園