How to create Bar Chart In Pdf in java



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


Download all jars required to execute program >


Program/Example - How to create BarChart In Pdf in java Example Using Itext - 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.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
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 BarChart In Pdf in java Example Using Itext -
*  iText library - core java tutorial
*
*/
public class BarChartInPdfExampleUsingItext {
   public static void main(String[] args) throws Exception {
         
          DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
          defaultCategoryDataset.setValue(190, "LaptopUsers", "1995");
          defaultCategoryDataset.setValue(205, "LaptopUsers", "2000");
          defaultCategoryDataset.setValue(300, "LaptopUsers", "2005");
          defaultCategoryDataset.setValue(350, "LaptopUsers", "2010");
          defaultCategoryDataset.setValue(390, "LaptopUsers", "2015");
         
          JFreeChart jFreeChart = ChartFactory.createBarChart(
                       "LaptopUsers BarChart", //title
                       "Year", // categoryAxisLabel
                       "LaptopUsers", //valueAxisLabel
                       defaultCategoryDataset, //dataset
                       PlotOrientation.VERTICAL, //orientation
                       false, false, false); //legend, tooltips and urls
         
          String pdfFilePath = "E:/BAR CHART created in pdf 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 BarChart
          int height = 300; //height of BarChart
          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 BAR chart on bottom left of page
          document.close();
          System.out.println("PDF created in >> " + pdfFilePath);
   }
}
/*OUTPUT
PDF created in >> E:/BAR CHART created in pdf in java using iText.pdf
*/



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


Summary -

In this core java tutorial we learned How to create Bar 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 the Table and set the Column Width And Spacing In Pdf in java

Rotate cells, cell padding, horizontal and vertical alignment of Cell in table in Pdf in java itext - iText java tutorial example




Creating LIST in pdf in java - iText tutorial >

Create Greek List In pdf in java using iText - iText java tutorial

How to Create Roman List In pdf in java - iText java tutorial

Create ZapfDingbats List In pdf in java - iText java tutorial program

How to Create Ordered List In pdf in java using iText - iText java tutorial

Creating the UnOrdered List In pdf in java - iText java tutorial example

How to Create Lists And SubLists In Pdf in java - iText java tutorial



Creating PASSWORD PROTECTED PDF in java - iText tutorial >

How to Create Password Protected Pdf in java - iText Example




Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >

How to Set the Font Name, Size, Style and Colour In Pdf using itext in java


Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

How To Set SuperScript And SubScript In Pdf in java - iText java tutorial

UnderLine Text In Pdf in java - iText java tutorial example

Strikethrough Text In Pdf in java - iText java tutorial program


HTML to PDF in java - iText tutorial >

Convert Html To Pdf in java using iText - iText java tutorial


Create new page, set page height and width in java - iText tutorial >

How To Create New Pages In Pdf Using Itext - iText java tutorial example

How to Find Pdf Page Height And Width in java - iText java tutorial program




eEdit
Must read for you :