java - 写J自由图表与中文标签到pdf

标签 java itext jfreechart itextpdf jfreereport

我正在尝试编写带有中文标签的JFree图表到PDF,我可以在没有标签的pdf中看到图表。但我需要带有标签的图表。

下面是代码片段。有人可以帮忙解决这个问题吗?

public static void main(String[] args) throws Exception {


              writeChartToPDF(generatePieChart(), 500, 400, "2.pdf");


  } 

public JFreeChart generatePieChart() throws SQLException {
         java.awt.Font font = new java.awt.Font("c:\\fonts\\SimHei Regular.TTF", java.awt.Font.HANGING_BASELINE,8);
        JFreeChart localJFreeChart = ChartFactory.createRingChart("", dataSet, true, false, false);
        RingPlot localRingPlot = (RingPlot)localJFreeChart.getPlot();
        localRingPlot.setLabelFont(font);
        localRingPlot.setNoDataMessage("No data available");
        localRingPlot.setNoDataMessageFont(font);
        localRingPlot.setNoDataMessagePaint(Color.darkGray);
        localRingPlot.setSectionDepth(0.75D);
        localRingPlot.setCircular(true);
        localRingPlot.setLabelGap(0.02D);
        localRingPlot.setSeparatorsVisible(true);
        localRingPlot.setSeparatorPaint(Color.white);
        localRingPlot.setShadowPaint(Color.white);
        localRingPlot.setOutlineVisible(false);
        localRingPlot.setBackgroundPaint(Color.white);
        localRingPlot.setLabelLinkStyle(PieLabelLinkStyle.STANDARD);

        localRingPlot.setLabelBackgroundPaint(Color.WHITE);
        localRingPlot.setLabelOutlinePaint(Color.WHITE);
        localRingPlot.setLabelShadowPaint(null);

        colorRingPlot(localRingPlot, dataSet);
        localJFreeChart.removeLegend();

        localRingPlot.setBaseSectionOutlinePaint(Color.white);
        localJFreeChart.setBackgroundPaint(Color.white);
        localJFreeChart.setBorderVisible(false);
        return localJFreeChart;
    }

private static void colorRingPlot(RingPlot plot, DefaultPieDataset dataset) {

        Double dataVal ;

        for (int i = 0; i < dataset.getItemCount(); i++) {  
            dataVal= (Double) dataset.getValue(i);
            if(dataVal >= 20){
                plot.setSectionPaint(dataset.getKey(i), colorRange[0]);
            }else if(dataVal >= 10 && dataVal < 20){  
                plot.setSectionPaint(dataset.getKey(i), colorRange[1]);
            }else if((dataVal > 0) && (dataVal < 10)){   
                plot.setSectionPaint(dataset.getKey(i), colorRange[2]);
            }        
        }   
    }  


private static final Color[]    colorRange = { new Color(111,191,103), new Color(169,217,164), new Color(210,235,205) };



private  PieDataset createDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("短期资 (68.20%)", 0);
        result.setValue("股 (1.87%)", 2);
        result.setValue("同基金s (3.95%)", 3);
        result.setValue("另投资 (8.19%))", 5);
        result.setValue("债 (7.96%)", 1);
        result.setValue("其投资 (2.81%))", 6);
        result.setValue("组合资 (7.02%)", 4);
        return result;
       }




public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
        PdfWriter writer = null;
        BaseFont bfA = null ;

        Document document = new Document();

        try {
            bfA = BaseFont.createFont("c:\\S24_Statements\\HK\\rundir\\fonts\\SimHei Regular.TTF", BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED);
            writer = PdfWriter.getInstance(document, new FileOutputStream(
                    fileName));
            document.open();
            PdfContentByte contentByte = writer.getDirectContent();
            FontFactory.getFont("GulimChe", BaseFont.IDENTITY_H, true, Font.BOLD,
                    14);

             DefaultFontMapper dfm=new DefaultFontMapper();
             dfm.pdfToAwt(bfA, 8);
            PdfTemplate template = contentByte.createTemplate(width, height);
            Graphics2D graphics2d = template.createGraphics(width, height,
                    dfm);
            Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
                    height);

            chart.draw(graphics2d, rectangle2d);

            graphics2d.dispose();
            contentByte.addTemplate(template, 0, 0);

        } catch (Exception e) {
            e.printStackTrace();
        }
        document.close();
    }

预期输出:带中文标签的图表

最佳答案

FontMapper arialuni = new FontMapper() {
            public BaseFont awtToPdf(Font font) {
                try {
                    return BaseFont.createFont(
                            "c:/windows/fonts/arialuni.ttf",
                            BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            public Font pdfToAwt(BaseFont font, int size) {
                return null;
            }

        };

此代码片段帮助了我,

引用:http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-14#524-text2topdf1.java

关于java - 写J自由图表与中文标签到pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37154482/

相关文章:

java - 如何将JFreeChart库添加到JDK?错误: package org. jfree.chart不存在

java - 用于 XYPlot 的 JFreeChart setDataset

java - 在 Jersey 资源组件类中获取 mule 消息属性

java - 为什么 setSkew 会增加文本大小?

java - iText PDF 中的 Unicode 字符

java - 从 PDF 文件中提取页码

jfreechart - 如何删除 jfreechart 图表面板的标题空间?

java - 通过使用文件重定向为其提供输入来运行 Java 程序

java - 为什么私有(private)修饰符使得程序通过子类对象调用却执行父类(super class)中的私有(private)方法?

java - Freemarker 中的可空日期