Create Pie Chart In Pdf in java using itext


In this core java tutorial we will learn How to create PieChart In Pdf in java Example Using Itext library - core java tutorial with program and examples.



Download all jars required to execute program >


Program/Example to How to create PieChart In Pdf in java Example Using Itext library - core java tutorial

import java.awt.Graphics2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
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;
/**
*
*  How to create PieChart In Pdf in java Example Using Itext -
*  iText library - core java tutorial
*
*/
public class PieChartInPdfExampleUsingItext {
   public static void main(String[] args) throws Exception {
          DefaultPieDataset defaultCategoryDataset = new DefaultPieDataset();
          defaultCategoryDataset.setValue("Linux", 8);
          defaultCategoryDataset.setValue("Mac OS", 15);
          defaultCategoryDataset.setValue("Windows", 75);
          defaultCategoryDataset.setValue("Unix", 2);
          JFreeChart jFreeChart = ChartFactory.createPieChart(
                       "Operating System users",  //pie chart title
                       defaultCategoryDataset, //pie chart dataset
                       false, false, false);  //pie chart> legend, tooltips and urls
          String pdfFilePath = "E:/PIE CHART created in java using iText.pdf";
          OutputStream fos = new FileOutputStream(new File(pdfFilePath));
          Document document = new Document();
          PdfWriter writer = PdfWriter.getInstance(document, fos);
          document.open();
         
          PdfContentByte pdfContentByte = writer.getDirectContent();
          int width = 400; //width of PieChart
          int height = 300; //height of pieChart
          PdfTemplate pdfTemplate = pdfContentByte.createTemplate(width, height);
         
          //create graphics
          Graphics2D graphics2d = pdfTemplate.createGraphics(width, height,
                       new DefaultFontMapper());
         
          //create rectangle
          java.awt.geom.Rectangle2D rectangle2d = new java.awt.geom.Rectangle2D.Double(
                       0, 0, width, height);
          jFreeChart.draw(graphics2d, rectangle2d);
          graphics2d.dispose();
          pdfContentByte.addTemplate(pdfTemplate, 40, 500); //0, 0 will draw PIE chart on bottom left of page
          document.close();
          System.out.println("PDF created in >> " + pdfFilePath);
   }
}
/*OUTPUT
PDF created in >> E:/PIE CHART created in java using iText.pdf
*/


PDF formed after executing above java program will look like this (Pie chart is created in pdf in java using itext) >

Summary -
In this core java tutorial we learned How to create PIE Chart In Pdf in java Example Using Itext - core java tutorial with program and examples.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java


Creating TABLES in pdf in java - iText tutorial >

How to Create the Table In Pdf in java - iText java tutorial

How to do the Cell Alignment In Pdf generated table in Java program

How to Set Cell Background Colour In Pdf Table in java example

How to Set Cell Background Colour In Pdf Table in java - iText java tutorial

How to Create Images In Pdf generated Table in java itext

How to Create Nested Tables in Pdf in java - iText java tutorial

How to Create the Table With Multiple Rows In Pdf in java program



No comments:

Post a Comment