浩海技术官网 - 专注最好用的电脑系统,Win10专业版/Win7旗舰版镜像下载!(本站系统保证安全无毒,运行完美) 浩海技术官网首页
当前位置:主页 > 浩海技术教程 >

win10系统下JFreeChart1.0.1和iText集成时,免费字符出现乱码的处理办法

发布日期:2016-11-16      作者:浩海技术官网      来源:http://www.hhghost.com

win10系统下JFreeChart1.0.1和iText集成时,免费字符出现乱码的处理办法

环境: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(0, 0, width, height);
chart.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp, 0, 0);

记住别忘了添加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:715)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:925)
at JFreeChartExample.convertTopdf(JFreeChartExample.java:910)
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:1104)
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:715)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:925)
at JFreeChartExample.convertTopdf(JFreeChartExample.java:910)
at JFreeChartExample.main(JFreeChartExample.java:63)



本站发布的软件及系统镜像均收集至互联网,仅供学习和研究使用,不得用于任何商业用途并请在下载后24小时内删除,如果满意请联系版权方购买。
如果您发现本站侵害了您的版权,请立即联系我们,本站将在48小时处理相关资源。邮箱:410947419#QQ.com[请将#换成@]
Copyright @ 2022 浩海技术官网