PdfWriter PdfContentByte Graphics2D 导出PDF中文乱码问题解决方案
2016-09-11 11:53
681 查看
JFreeChart1.0.1和iText集成时,中文字符出现乱码的解决方法
环境:JFreeChart 1.0.1、iText1.3
JFreeChart1.0.1中的Plot接口没有提供SetValueLabelFont的方法,无法直接设置label的字体,郁闷啊!
不过还好试了一下午总算弄出来了,具体代码如下:
记住别忘了添加iTextAisan.jar不然会报如下错误:下载地址:http://download.csdn.net/detail/itxiaohei323/9627537
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)
环境: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); |
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)
相关文章推荐
- mxGraph 导出图片节点中文乱码问题解决方案
- ASP.NET 导出到EXCEL LINQ适用 自定义列 自定义数据源 解决中文乱码问题 解决方案
- RDLC(REPORT VIEW)导出PDF报表中文乱码问题
- jsp将table导出成excel+用js将table导出excel,中文乱码问题解决方案+action层导出excel
- jsp将table导出成excel+用js将table导出excel,中文乱码问题解决方案+action层导出excel
- [原创]OpenERP 7.0 打印PDF报表 中文 乱码问题的解决方案。
- jfreechart一&二维报表导出为word&pdf&html-中文乱码和字体模糊问题(一)
- Jpivot mdx查询编辑器和Jpivot导出PDF、Excel中文乱码问题
- 关于RDLC使用导出PDF文件时,中文乱码解决方案
- dokuwiki + dw2pdf 插件,解决导出pdf文件中文乱码(字体问题,显示小方块)
- itop 2.2 导出PDF中文乱码问题解决
- ASPxGridView导出pdf时中文乱码的解决方案
- ASPxGridView导出pdf时中文乱码的解决方案
- 关于java中文乱码问题一些解决方案和经验
- JSP中文乱码问题解决方案
- eXtremeTable导出数据为XLS和PDF时的中文问题解决办法
- 这是我们公司总结的一些关于中文乱码问题的一些解决方案和经验和大家分享!
- 从旧版升级到MySql4.1上的中文乱码问题解决方案
- 一些关于中文乱码问题的一些解决方案和经验
- Tomcat及MySQL中文乱码问题解决方案总览