Java Class to create the PDF using the compiled .jasper file.
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class JasperReportFill {
public static void main(String[] args) {
String sourceFileName =
"C://Users//sanal//dbs.jasper";
DataBeanList DataBeanList = new DataBeanList();
ArrayList<DataBean> dataList = DataBeanList.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource =
new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
JRDataSource dataSource = new JREmptyDataSource();
// Make sure the output directory exists.
File outDir = new File("C:/jasperoutput");
outDir.mkdirs();
System.out.println("PDF CREATED!");
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(sourceFileName,parameters, beanColDataSource);
// Export to PDF.
JasperExportManager.exportReportToPdfFile(jasperPrint,"C://Users//sanal//test.pdf");
} catch (JRException e) {
e.printStackTrace();
}
}
}
Create a DataBean POJO with 3 attributes .
private String subjectName;
private Integer marks;
private Integer percentage;
DataBeanList Method will populate the databean object based on the constructor parameters.
Below the JRXML
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="dbs" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ba415345-304b-4c2d-8021-e84b13474b53">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="subjectName" class="java.lang.String"/>
<field name="marks" class="java.lang.Integer"/>
<field name="percentage" class="java.lang.Integer"/>
<title>
<band height="20" splitType="Stretch"/>
</title>
<pageHeader>
<band height="10" splitType="Stretch"/>
</pageHeader>
<summary>
<band height="200" splitType="Stretch">
<barChart>
<chart>
<reportElement x="17" y="24" width="231" height="154" uuid="63cb182e-1fb6-4e72-a8c0-8f83fef71691"/>
<chartTitle>
<titleExpression><![CDATA["JR Bar Chart"]]></titleExpression>
</chartTitle>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<categorySeries>
<seriesExpression><![CDATA[$F{subjectName}]]></seriesExpression>
<categoryExpression><![CDATA[$F{subjectName}]]></categoryExpression>
<valueExpression><![CDATA[$F{marks}]]></valueExpression>
</categorySeries>
</categoryDataset>
<barPlot isShowTickMarks="false">
<plot/>
<itemLabel/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</barPlot>
</barChart>
<xyBarChart>
<chart>
<reportElement x="288" y="24" width="252" height="154" uuid="8e067d93-7652-4237-b693-eaf631f720a7"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<xyDataset>
<xySeries>
<seriesExpression><![CDATA[$F{subjectName}]]></seriesExpression>
<xValueExpression><![CDATA[$F{percentage}]]></xValueExpression>
<yValueExpression><![CDATA[$F{marks}]]></yValueExpression>
</xySeries>
</xyDataset>
<barPlot>
<plot/>
<itemLabel/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</barPlot>
</xyBarChart>
</band>
</summary>
</jasperReport>
finally how it looks

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class JasperReportFill {
public static void main(String[] args) {
String sourceFileName =
"C://Users//sanal//dbs.jasper";
DataBeanList DataBeanList = new DataBeanList();
ArrayList<DataBean> dataList = DataBeanList.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource =
new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
JRDataSource dataSource = new JREmptyDataSource();
// Make sure the output directory exists.
File outDir = new File("C:/jasperoutput");
outDir.mkdirs();
System.out.println("PDF CREATED!");
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(sourceFileName,parameters, beanColDataSource);
// Export to PDF.
JasperExportManager.exportReportToPdfFile(jasperPrint,"C://Users//sanal//test.pdf");
} catch (JRException e) {
e.printStackTrace();
}
}
}
Create a DataBean POJO with 3 attributes .
private String subjectName;
private Integer marks;
private Integer percentage;
DataBeanList Method will populate the databean object based on the constructor parameters.
Below the JRXML
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="dbs" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ba415345-304b-4c2d-8021-e84b13474b53">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="subjectName" class="java.lang.String"/>
<field name="marks" class="java.lang.Integer"/>
<field name="percentage" class="java.lang.Integer"/>
<title>
<band height="20" splitType="Stretch"/>
</title>
<pageHeader>
<band height="10" splitType="Stretch"/>
</pageHeader>
<summary>
<band height="200" splitType="Stretch">
<barChart>
<chart>
<reportElement x="17" y="24" width="231" height="154" uuid="63cb182e-1fb6-4e72-a8c0-8f83fef71691"/>
<chartTitle>
<titleExpression><![CDATA["JR Bar Chart"]]></titleExpression>
</chartTitle>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<categorySeries>
<seriesExpression><![CDATA[$F{subjectName}]]></seriesExpression>
<categoryExpression><![CDATA[$F{subjectName}]]></categoryExpression>
<valueExpression><![CDATA[$F{marks}]]></valueExpression>
</categorySeries>
</categoryDataset>
<barPlot isShowTickMarks="false">
<plot/>
<itemLabel/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</barPlot>
</barChart>
<xyBarChart>
<chart>
<reportElement x="288" y="24" width="252" height="154" uuid="8e067d93-7652-4237-b693-eaf631f720a7"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<xyDataset>
<xySeries>
<seriesExpression><![CDATA[$F{subjectName}]]></seriesExpression>
<xValueExpression><![CDATA[$F{percentage}]]></xValueExpression>
<yValueExpression><![CDATA[$F{marks}]]></yValueExpression>
</xySeries>
</xyDataset>
<barPlot>
<plot/>
<itemLabel/>
<categoryAxisFormat>
<axisFormat/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</barPlot>
</xyBarChart>
</band>
</summary>
</jasperReport>
finally how it looks