如何使用iTextPdf将两个JFreeCharts绘制到同一文档中

时间:2017-08-01 09:12:13

标签: itext

我想使用iTextPdf在同一文档中打印两个Jfree图表,当我尝试使用此代码时,以下JFreeChart会覆盖前一个。结果我在pdf的两页上都得到了第二张图表。

    public void ExportChart(OutputStream out, int width, int height) throws IOException, DocumentException {
    Rectangle rect = new com.itextpdf.text.Rectangle((float) width, (float) height+130);
    Document document = new com.itextpdf.text.Document(rect);
    PdfWriter writer = null;

    writer=PdfWriter.getInstance(document, out);
    document.open();
    document.add(addHeaderInfo(width));

    DefaultFontMapper mapper = new DefaultFontMapper();
    FontFactory.registerDirectories();
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(width, height);
    Graphics2D g2d = tp.createGraphics(width, height, mapper);
    tp.setWidth(width);
    tp.setHeight(height);
    //barStat is a JFreeChart Objecct
    barStat.draw(g2d, new java.awt.Rectangle(width, height));

    cb.addTemplate(tp, 0, 0);
    document.newPage();


    SingleHistogramDialog singleHD=new SingleHistogramDialog();
    JFreeChart barStat2=singleHD.Histogram();

    document.add(addHeaderInfo(width));


    FontFactory.registerDirectories();
    tp.setWidth(width);
    tp.setHeight(height);
    barStat2.draw(g2d, new java.awt.Rectangle(width, height));
    g2d.dispose();
    cb.addTemplate(tp, 0, 0);

    document.close();
}

2 个答案:

答案 0 :(得分:2)

我按字面意思复制了你的代码,并做了一些改动:

public void ExportChart(OutputStream out, int width, int height) throws IOException, DocumentException {
    Rectangle rect = new com.itextpdf.text.Rectangle((float) width, (float) height+130);
    Document document = new com.itextpdf.text.Document(rect);
    PdfWriter writer = null;

    writer=PdfWriter.getInstance(document, out);
    document.open();
    document.add(addHeaderInfo(width));

    DefaultFontMapper mapper = new DefaultFontMapper();
    FontFactory.registerDirectories();
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(width, height);
    Graphics2D g2d = tp.createGraphics(width, height, mapper);
    //barStat is a JFreeChart Objecct
    barStat.draw(g2d, new java.awt.Rectangle(width, height));

    cb.addTemplate(tp, 0, 0);
    document.newPage();


    SingleHistogramDialog singleHD=new SingleHistogramDialog();
    JFreeChart barStat2=singleHD.Histogram();

    document.add(addHeaderInfo(width));

    tp = cb.createTemplate(width, height);
    g2d = tp.createGraphics(width, height, mapper);
    barStat2.draw(g2d, new java.awt.Rectangle(width, height));
    g2d.dispose();
    cb.addTemplate(tp, 0, 0);

    document.close();
}

答案 1 :(得分:0)

我通过这种方式更改上面的代码解决了我的问题

    public void ExportChart(OutputStream out, int width, int height) throws 
    IOException, DocumentException {
    Rectangle rect = new com.itextpdf.text.Rectangle((float) width, (float) 
    height+130);
    Document document = new com.itextpdf.text.Document(rect);
    PdfWriter writer = null;

    writer=PdfWriter.getInstance(document, out);
    document.open();
    document.add(addHeaderInfo(width));

    DefaultFontMapper mapper = new DefaultFontMapper();
    FontFactory.registerDirectories();
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(width, height);
    Graphics2D g2d = tp.createGraphics(width, height, mapper);
    tp.setWidth(width);
    tp.setHeight(height);
    barStat.draw(g2d, new java.awt.Rectangle(width, height));



    Histogram singleHD=new Histogram();
    JFreeChart barStat2=singleHD.Histogram();


    PdfTemplate tp2 = cb.createTemplate(width, height);
    Graphics2D g2d2 = tp2.createGraphics(width, height, mapper);
    tp2.setWidth(width);
    tp2.setHeight(height);
    barStat2.draw(g2d2, new java.awt.Rectangle(width, height));

    g2d.dispose();

    cb.addTemplate(tp, 0, 0);
    document.newPage();
    document.add(addHeaderInfo(width));

    g2d2.dispose();

    cb.addTemplate(tp2, 0, 0);

    document.close();
}