hello can any one help me in adding many number of bar charts to a single pdf. i have a code taken from d website to create a bar chart and save it in pdf. but couldn't save multiple bar charts to a single pdf.
import com.itextpdf.awt.DefaultFontMapper;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
public class PieChartDemo {
public static void main(String[] args) {
writeChartToPDF(generateBarChart(), 500, 400, "F:\\pdf\\bar.pdf");
//TODO: Add code to generate PDFs with charts
}
public static JFreeChart generateBarChart() {
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.setValue(791, "Population", "1750 AD");
dataSet.setValue(978, "Population", "1800 AD");
dataSet.setValue(1262, "Population", "1850 AD");
dataSet.setValue(1650, "Population", "1900 AD");
dataSet.setValue(2519, "Population", "1950 AD");
dataSet.setValue(6070, "Population", "2000 AD");
JFreeChart chart = ChartFactory.createBarChart(
"World Population growth", "Year", "Population in millions",
dataSet, PlotOrientation.VERTICAL, false, true, false);
return chart;
}
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
PdfWriter writer = null;
Document document = new Document();
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
for(int i=0;i<3;i++){
chart.draw(graphics2d, rectangle2d);
}
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
}