1

I have 2 JFree Charts and I would like to put one on each page of PDF.

How could I edit this code?

public void createPdf(String filename) throws IOException, DocumentException, SQLException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    // step 3
    document.open();
    // step 4
    PdfContentByte cb = writer.getDirectContent();
    float width = PageSize.A4.getWidth();
    float height = PageSize.A4.getHeight() / 2;
    // Pie chart
    PdfTemplate pie = cb.createTemplate(width, height);
    Graphics2D g2d1 = new PdfGraphics2D(pie, width, height);
    Rectangle2D r2d1 = new Rectangle2D.Double(0, 0, width, height);
    getPieChart().draw(g2d1, r2d1);
    g2d1.dispose();
    cb.addTemplate(pie, 0, height);
    // Bar chart
    PdfTemplate bar = cb.createTemplate(width, height);
    Graphics2D g2d2 = new PdfGraphics2D(bar, width, height);
    Rectangle2D r2d2 = new Rectangle2D.Double(0, 0, width, height);
    getBarChart().draw(g2d2, r2d2);
    g2d2.dispose();
    cb.addTemplate(bar, 0, 0);
    // step 5
    document.close();
}

Thanks everyone.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Pavel Straka
  • 427
  • 7
  • 19

1 Answers1

0

Use document.newPage(); method to signal that a new page has to be started, and next input'll go to next page. So I believe ideally what you need is:

  1. Open pdf document // as in 3th step of provided example
  2. Setup content writes // as in 4th step of provided example
  3. Write page content // i.e. part of code after "Pie chart" content of provided example
  4. Invoke document.newPage()
  5. Repeat steps 3 and 4 as needed // i.e. after document.newPage()
    paste Bar chart code
  6. Close PDF with document.close()

So writing would look like:

// pie chart
PdfTemplate pie = cb.createTemplate(width, height);
Graphics2D g2d1 = new PdfGraphics2D(pie, width, height);
Rectangle2D r2d1 = new Rectangle2D.Double(0, 0, width, height);
getPieChart().draw(g2d1, r2d1);
g2d1.dispose();
cb.addTemplate(pie, 0, 0);

// new page
document.newPage();

// Bar chart
PdfTemplate bar = cb.createTemplate(width, height);
Graphics2D g2d2 = new PdfGraphics2D(bar, width, height);
Rectangle2D r2d2 = new Rectangle2D.Double(0, 0, width, height);
getBarChart().draw(g2d2, r2d2);
g2d2.dispose();
cb.addTemplate(bar, 0, 0);
Konrad 'Zegis'
  • 15,101
  • 1
  • 16
  • 18
  • Ok, but in this case I have to split the data in chart somehow (in this example Pie Chart and Bar Chart), am I right? But what if I wanted to have a long bar chart? Do I have to split the data - Bar Chart part 1 and Bar Chart part2 for example? Thank You. – Pavel Straka Nov 27 '15 at 10:03
  • You always can rotate diagram to fit it vertically (as shown [here](http://stackoverflow.com/questions/14124593/how-to-rotate-graphics-in-java)), or scale chart. Ideally you should use [ChartPanel](http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/ChartPanel.html) to draw chart in a GUI and it has methods for setting it's scale. Although In this situation scalling Graphics2d, or Rectangle2d should do the trick. – Konrad 'Zegis' Nov 27 '15 at 10:22
  • Rotating the diagram to fit it vertically will not help me. I could use Chart Panel and set smaller scale? Please could You provide some example? Thank You – Pavel Straka Nov 27 '15 at 10:26
  • I'll provide something simple after I'll reach home later today. – Konrad 'Zegis' Nov 27 '15 at 11:25