Java iText Create PDF Pie Chart Tutorial - Embed Colored Pie Charts

In one of our earlier posts, we described how to create a Colored Pie Chart using JFreeChart Java API. That tutorial finally converted the chart as a JPG image for us. I have been getting some emails on how to embed a JFreechart created pie chart in a PDF file using iText Java library. So, in this tutorial, I will take a leaf out of my earlier posts and extend the example discussed earlier and stamp the Pie Chart into the PDF document by using the iText library. If you are landing on this post right from the internet, refer to the original Java example linked earlier. The explanation code offered earlier would be pruned in such a way that we get a PDF file in the output after creating and coloring the chart.

So, we have the code to create Pie Chart. Let us now extend the code to create a PDF document out of it.You may wish to read the tutorial that explains how to insert a standard Pie Chart to PDF in iText before you look into the code for this section.
/* This example tutorial explains how to generate a colored Pie chart in a PDF file using iText Java API, and JFreeChart Java API */
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import org.jfree.data.general.DefaultPieDataset; 
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.JFreeChart;
import java.awt.Color;
public class EmbedPieChartPDF {  
     public static void main(String[] args){
        try {
                /* Create Pie Chart */
                DefaultPieDataset myColoredPieChart = new DefaultPieDataset();                
                myColoredPieChart.setValue("Java", 12.9);
                myColoredPieChart.setValue("C++", 37.9);
                myColoredPieChart.setValue("C", 86.5);
                myColoredPieChart.setValue("VB", 80.5);
                myColoredPieChart.setValue("Shell Script", 19.5);                
                JFreeChart myColoredChart=ChartFactory.createPieChart("Programming - Colored Pie Chart Example",myColoredPieChart,true,true,false);
                /* Color Pie Chart */
                PiePlot ColorConfigurator = (PiePlot)myColoredChart.getPlot();                
                ColorConfigurator.setSectionPaint("Java", new Color(160, 160, 255));
                ColorConfigurator.setSectionPaint("C++", Color.ORANGE);
                ColorConfigurator.setSectionPaint("C", Color.GREEN);
                ColorConfigurator.setSectionPaint("VB", Color.PINK);
                ColorConfigurator.setSectionPaint("Shell Script", Color.YELLOW);               
                /* We have to insert this colored Pie Chart into the PDF file using iText now */                
                int width=640; /* Width of our chart */
                int height=480; /* Height of our chart */                
                Document PieChart=new Document(new Rectangle(width,height));             
                PdfWriter writer=PdfWriter.getInstance(PieChart,new FileOutputStream("My_Colored_Pie_Chart.pdf"));                
                PieChart.open();
                /* Add some Metadata to identify document later */
                PieChart.addTitle("How to color your Pie Chart and embed in a PDF file using iText");
                PieChart.addAuthor("Thinktibits");                
                PieChart.addKeywords("iText,Color PieChart,JFreeChart,PDF,Example Tutorial");                
                PdfContentByte Add_Chart_Content= writer.getDirectContent();                
                PdfTemplate template_Chart_Holder=Add_Chart_Content.createTemplate(width,height);                
                Graphics2D Graphics_Chart=template_Chart_Holder.createGraphics(width,height,new DefaultFontMapper());                
                Rectangle2D Chart_Region=new Rectangle2D.Double(0,0,540,380);                
                myColoredChart.draw(Graphics_Chart,Chart_Region);            
                Graphics_Chart.dispose();                
                Add_Chart_Content.addTemplate(template_Chart_Holder,0,0);                
                PieChart.close();
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}
This example produces the same output as of JPG file, but rather embeds the chart into the PDF file. The good point is the size of the file. The PDF file size came out to be only 4 KB, whereas the JPG image had a size of around 25 KB. Interesting!.On the same lines, you can also insert the exploded pie chart into a PDF file using iText. Stay tuned!

No comments:

Post a Comment