Insert Pie Chart PDF iText Example JFreeChart Java Tutorial

In this tutorial, we will provide an example through which we will create a Pie Chart in Java, and then stamp this Pie Chart inside a PDF document using iText API. This is an extension of our tutorial on iText that explained how to create a bar chart in a PDF document using iText. For this Pie Chart example, we will use JFreeChart Java API. The structural flow for us would be to create a Pie Chart in JFreeChart API, and then incorporate the methods used in our earlier tutorials to add this Pie Chart to PDF document. Before we proceed with this step  by step guid, make sure you read through our initial tutorials. They are highly helpful for this example and are provided below;
The approach to create a Pie Chart inside a PDF file, involves creating the required dataset for the chart and then get the chart content as a JFreeChart Object. This can be wrapped inside a PDF using the methodology described earlier. Refer to the Self explaining Java Code below that the clearly commented, and explains how to create a Pie Chart in a PDF file using iText and JFreeChart;
/* This example tutorial demonstrates how to draw a Pie chart in a PDF document 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; /* We will use DefaultPieDataset to define the data for the Pie Chart */
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.JFreeChart;
public class PDFPieChartExample {  
     public static void main(String[] args){
        try {
                /* We will define the data for the Pie Chart Using the Code below */
                /* Declare dataset object using the code below */
                DefaultPieDataset myPiedataset = new DefaultPieDataset();
                /* Define Values for the Pie Chart - Programming Languages Percentage Difficulty */
                myPiedataset.setValue("Java", 12.9);
                myPiedataset.setValue("C++", 37.9);
                myPiedataset.setValue("C", 86.5);
                myPiedataset.setValue("VB", 80.5);
                myPiedataset.setValue("Shell Script", 19.5);
                /* With the dataset defined for Pie Chart, we can invoke a method in ChartFactory object to create Pie Chart and Return a JFreeChart object*/
                /* This method returns a JFreeChart object back to us */
                /* We specify the chart title, dataset, legend, tooltip and URLs in this method as input */
                JFreeChart PDFPieChart=ChartFactory.createPieChart("Programming - Pie Chart Example",myPiedataset,true,true,false);
                /* We have a Pie chart object, and now need to find a procedure to insert it into PDF using iText */
                int width=640; /* Width of our chart */
                int height=480; /* Height of our chart */                
                Document PieChart=new Document(new Rectangle(width,height)); /* Create a New Document Object for PDF */                
                /* Create PDF Writer Object that will physically write the PDF file to File Output Stream */
                PdfWriter writer=PdfWriter.getInstance(PieChart,new FileOutputStream("Add_Pie_Chart_Using_JFreeChart.pdf"));
                /* Ready with document objects, open the document object to push contents */
                PieChart.open();
                /* Add some Metadata to identify document later */
                PieChart.addTitle("How to Add a Pie Chart to a PDF file using iText");
                PieChart.addAuthor("Thinktibits");                
                PieChart.addKeywords("iText,PieChart,JFreeChart,PDF,Example Tutorial");
                /* Get Direct Content of the PDF document for writing */
                PdfContentByte Add_Chart_Content= writer.getDirectContent();
                /* Create a template using the PdfContent Byte object */
                PdfTemplate template_Chart_Holder=Add_Chart_Content.createTemplate(width,height);
                /* Create a 2D graphics object and Rectangle object as before to write on the template */
                Graphics2D Graphics_Chart=template_Chart_Holder.createGraphics(width,height,new DefaultFontMapper());                
                Rectangle2D Chart_Region=new Rectangle2D.Double(0,0,540,380);
                /* Invoke the draw method passing the Graphics and Rectangle 2D object to draw the piechart */
                PDFPieChart.draw(Graphics_Chart,Chart_Region);            
                Graphics_Chart.dispose();
                /* Add template to PdfContentByte and then to the PDF document */
                Add_Chart_Content.addTemplate(template_Chart_Holder,0,0);
                /* Close the Document, writer will create a beautiful Pie chart inside the PDF document */
                PieChart.close();
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}
The Java code above inserts a Pie Chart to a PDF file. The output produced in my case is provided below;
JFreeChart iText Pie Chart Example Output
Pie Chart - iText JFreeChart Example Tutorial
As you may see,the Pie Chart is very basic and let us see how to make this a super pie chart..adding effects, 3D etc in our next tutorial..

No comments:

Post a Comment