<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technology Bits and Bytes &#187; hkishorekumar</title>
	<atom:link href="http://blogs.circlesource.com/author/hkishorekumar/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.circlesource.com</link>
	<description>CircleSource Technical Talent ShowCase</description>
	<lastBuildDate>Thu, 10 Dec 2009 20:01:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Inserting charts into PDF</title>
		<link>http://blogs.circlesource.com/2009/01/07/75/</link>
		<comments>http://blogs.circlesource.com/2009/01/07/75/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 08:00:12 +0000</pubDate>
		<dc:creator>hkishorekumar</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blogs.circlesource.com/?p=75</guid>
		<description><![CDATA[

 
JasperReport is report generation tool used to create reports in Java. And also used to include Charts, Images, and Tables into the report. It can produce the reports in multiple formats like PDF, CSV, HTML, XLS etc. In this article, first we discussed about creation of PDF file using jasper report.
 
Tools required to [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="text-align: center;" align="center"><strong><span style="font-size: 15pt; font-family: Calibri;"><br />
</span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: Calibri;"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri; font-size:12pt">JasperReport is report generation tool used to create reports in Java. And also used to include Charts, Images, and Tables into the report. It can produce the reports in multiple formats like <strong>PDF</strong>, <strong>CSV</strong>, <strong>HTML</strong>, <strong>XLS </strong>etc. In this article, first we discussed about creation of <strong>PDF </strong>file using jasper report.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Tools required to generate Jasper reports are JRE 1.5 or above, jasperreport-3.0.0.jar, itext-1.2.3.jar.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Jasper uses <strong>JRXML </strong>files to generate report. For easy to generate <strong>JRXML</strong>, we can use iReport-2.0.4 or later. IReports is a tool which is used to generate <strong>JRXML</strong>. It provides GUI to design the report elements such as titles, fields, variables, parameters, charts etc.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">The generated <strong>JRXML </strong>file contains report design information like title, page footers, page headers, page size, margins etc.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">The <strong>JRXML </strong>file needs to be compiled to generate the JASPER file, which will be used by <strong><em>JasperReport</em></strong> classes to generate the actual report. <strong>JRXML </strong>can be compiled using method <em><strong>compileReport</strong>()</em> of <span class="bold"><em>net.sf.jasperreports.engine.JasperCompileManager</em> class. The compiled binary file will have extension “.<strong>jasper</strong>”. Also we can use iReports to compile the <strong>JRXML </strong>file.</span></span></p>
<p class="MsoNormal"><span class="bold"><span style="font-family: Calibri;"> </span></span></p>
<p class="MsoNormal"><span class="bold"><span style="font-family: Calibri;font-size:12pt">For example to create report in form of <strong>PDF </strong>the code is given below.</span></span></p>
<p class="MsoNormal"><span class="bold"><span style="font-family: Calibri;"> </span></span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">JasperReport jasperReport = null;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">JasperPrint jasperPrint = null;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">InputStream jasperStream = null;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">try {</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"><span> </span>jasperStream = this.getClass().getClassLoader().getResourceAsStream(</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"><span> </span>“fileName.jasper&#8221;);&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;(1)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);&#8212;&#8212;&#8212;&#8212;&#8211;(2)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">jasperPrint = JasperFillManager.fillReport(jasperReport, reportParams, <span> </span>dataSource);&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;(3)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">//exporting the PDF file</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">JasperExportManager.exportReportToPdfFile(jasperPrint, fileName);&#8212;&#8212;&#8212;&#8211;(4)</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">} catch (Exception e) {</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"><span> </span>e.printStackTrace();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">}</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">}</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">In above code <em>JasperReport</em> is instance of <em>net.sf.jasperreports.engine.JasperReport</em> and holds the jasper file stream. We are loading the jasper stream at step (1, 2). Here we are using the <strong><em>JRLoader.loadObject()</em></strong> method to load the jasper stream into <em>jasperReport</em>. Now <em>jasperReport</em> instance contains the compiled “.<strong>jasper</strong>”. The “.<strong>jasper</strong>” is nothing but the compiled <strong>JRXML </strong>which holds the design and information about where the different data should fit.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><em><span style="font-family: Calibri;font-size:12pt">JasperFillManager.fillReport(…..)</span></em><span style="font-family: Calibri;font-size:12pt"> method will returns a instance of <em>JasperPrint</em> class and that can be used to export the data into <strong>PDF, CSV, XLS </strong>etc. In above example at step(3) we are creating <em>JasperPrint</em> instance. It takes the parameters </span></p>
<p class="MsoNormal"><!--[if !supportLists]--><span style="font-family: Symbol;"><span>·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><strong><em><span style="font-family: Calibri;font-size:12pt">jasperReport</span></em></strong><span style="font-family: Calibri;font-size:12pt"><strong> </strong>having the compiled .jasper file, </span><!--[endif]--></p>
<p class="MsoNormal"><!--[if !supportLists]--><span style="font-family: Symbol;"><span>·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><strong><em><span style="font-family: Calibri;font-size:12pt">reportParams</span></em></strong><span style="font-family: Calibri;font-size:12pt"><strong> </strong>is actually a <em>java.util.Map</em>. We can set any parameters and their values as key value pairs in map. And then passed as parameter for method <em>fillReport(…..)</em>.<span> </span>This contains the certain static data to be displayed in report.</span><!--[endif]--></p>
<p class="MsoNormal"><!--[if !supportLists]--><span style="font-family: Symbol;"><span>·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span><strong><em><span style="font-family: Calibri;font-size:12pt">dataSource</span></em></strong><span style="font-family: Calibri;font-size:12pt"><strong> </strong>is a instance of <em>JRDataSource</em>. Whatever the data we want to show in report can be send as <em>dataSource</em>. This contains the DataSource which will be iterated by JASPER report and shown the data in a table.</span><!--[endif]--></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Now the <em>jasperPrint</em> instance is created, which actually holds the data viewed in PDF page. Finally the <em><strong>exportreportToPdfFile</strong>()</em> method of<span> </span><em>JasperExportManger</em> is used to export the report object into the PDF file and stored with the specified file name. We already know that <em>jasperPrint</em> instance has the report object to export on the page. This object will be sent as a parameter to <em>JasperExportManager.exportReportToPdfFile()</em> method. Finally it returns binary content as bite[] array. This array is nothing but the <strong>PDF </strong>file.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Up to now creation of PDF is completed. Now we discussed about creating charts and insert them into PDF file.</span></p>
<p class="MsoNormal"><span style="font-size: 14pt; font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">ChartFx for Java 6.5 Development is a tool to develop charts and maps in the form of J<strong>PEG, PNG</strong> etc. This provides APIs which are required to develop charts. These APIs are used to set charts title, labels, size, background color, axis labels, chart type etc. Charts which are developed using <strong>chartFx </strong>are stored in temporary directory. So we can capture them from temp location and display in <strong>PDF</strong>. </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">For example see the given code.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">String path= null;</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">ChartServer chart1 = new ChartServer(context, request, response);</span></em></strong></p>
<p class="MsoNormal"><strong><span style="font-family: Calibri;font-size:12pt">……………………….</span></strong></p>
<p class="MsoNormal"><strong><span style="font-family: Calibri;font-size:12pt">……………………….</span></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">path = chart1.getHtmlTag(&#8220;1560&#8243;, “2000”, &#8220;PNG&#8221;);</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><em><span style="font-family: Calibri;font-size:12pt">new ChartServer(context, request, response)</span></em><span style="font-family: Calibri;font-size:12pt"> will create chartServer object. This object is the actual chart object which will be used to create a chart image. Here the parameter context is of type </span><code><em><span style="font-size: 10pt; font-family: Calibri;font-size:12pt">javax.servlet.ServletContext</span></em></code><code><span style="font-size: 10pt; font-family: Calibri;">, </span></code><span style="font-family: Calibri;font-size:12pt">request is of type</span><code><span style="font-size: 10pt; font-family: Calibri;font-size:12pt"> <em>javax.servlet.http.HttpServletRequest</em> </span></code><span style="font-family: Calibri;font-size:12pt">and response is of type</span><code><span style="font-size: 10pt; font-family: Calibri;font-size:12pt"> <em>javax.servlet.http.HttpServletResponse.</em> </span></code><span style="font-family: Calibri;font-size:12pt">One more default constructor is also available to create chart server is <em>ChartServer()</em>. There are many more methods are available in this class to set the title of chart, x-axis title, y-axis title, background color, font color, font size, font style etc. In our example we skipped the code of designing part of chart. Any one can design as per their requirement. At the end method <em>getHtmlTag(&#8220;1560&#8243;, “2000”, &#8220;PNG&#8221;)</em>, will return html image tag(<em>&lt;img …. /&gt;</em>) which will have “src” attribute and this attribute has path of chart image file. Also the <em>&lt;img /&gt;</em> element will have the width and height attributes which will hold 1560 and 2000 respectively.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">For our example, think that charts are already created and placed in location “C:images flower.jpeg”. Now we display generated charts into <strong>PDF </strong>report. For that, first create parameter in the <strong>JRXML </strong>file using the iReport.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">In iReports goto <strong>File</strong></span><strong><span style="font-family: Wingdings;"><span>&#8211;&gt;</span></span><span style="font-family: Calibri;">view</span><span style="font-family: Wingdings;"><span>&#8211;&gt;</span></span></strong><span style="font-family: Calibri;font-size:12pt">parameters and then select “parameters” tab, and then click “new” button. Give a name for that parameter and type as String. Later we assign value for this parameter from our java code using <em>java.util.Map</em>.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"><!--[if gte vml 1]&gt;                    &lt;![endif]--><!--[if !vml]--><!--[endif]--></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> <img class="alignnone size-full wp-image-78" src="http://blogs.circlesource.com/wp-content/uploads/2009/01/clip_image0021.jpg" alt="clip_image0021" width="330" height="362" /></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">And then add image element in our <strong>JRXML</strong>. Right click on image element and open properties and click on “Image Tab” then add “Image Expression” for that element.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"><!--[if gte vml 1]&gt;  &lt;![endif]--><!--[if !vml]--><!--[endif]--></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> <img class="alignnone size-full wp-image-77" src="http://blogs.circlesource.com/wp-content/uploads/2009/01/clip_image0022.jpg" alt="clip_image0022" width="380" height="387" /></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">And then design report as per your requirement and compile to generate “.<strong>Jasper</strong>”. Now we have to send path for this image to display in <strong>PDF </strong>from our java code. The java code given below uses the image available at “C:images flower.jpeg” location.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">Map reportParameters = new Hashmap();</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">reportParameters.put(“chartPath”,” C:\images <a href="/////flower.jpeg">\flower.jpeg</a>”);</span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt"> </span></em></strong></p>
<p class="MsoNormal"><strong><em><span style="font-family: Calibri;font-size:12pt">jasperPrint = JasperFillManager.fillReport(jasperReport, reportParameters,datasource);</span></em></strong></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Then pass this map as parameter at the time of creating <em>jasperPrint</em> object. Now the path for the image is known for <em>jasperFillManager</em>. So it creates <em>jasperPrint</em> object including this image.<span> </span></span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-size: 14pt; font-family: Calibri;font-size:12pt">Problem when inserting chart in <strong>PDF</strong></span></p>
<p class="MsoNormal"><span style="font-size: 14pt; font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Some times the inserting chart will have the size that may not be fit into actual size set in <strong>JRXML </strong>file for that image. In this situation while creating the chart, we can divide original chart into number of charts. </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">For example, in <strong>JRXML </strong>we designed the report like it can show chart of 500 pixels height. The chart having 20 bars only perfectly fit in this image element. Now what we have to do is, while creating chart, create two or more charts depending on the result and display them one by one in report. </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">For example, we want to display bar chart for students’ percentages. There are 55 students in class room. As per our design only chart contains bars for 20 students will fit perfectly in image element. Now what we have to do is, while creating chart split it into three charts contains students’ percentage bars of 20, 20, and 15 each. Now display these images into the PDF and they will be displayed perfectly.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt">Wow! Finally we get the charts into <strong>PDF </strong>with the combination of <strong>ChartFX </strong>and Jasper Reports. I hope this will help anyone to develop reports in the form of <strong>PDF</strong>.</span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
<p class="MsoNormal"><span style="font-family: Calibri;font-size:12pt"> </span></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.circlesource.com/2009/01/07/75/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
