用中文标签写J免费图表到pdf

时间:2016-05-11 06:19:15

标签: 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();
    }

预期OutPut:带有中文标签的图表

1 个答案:

答案 0 :(得分:1)

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