JasperReports Tutorial | #2 | Add Dynamic Image in JasperReport | Code Factory

Donate : Link

Reference Link : Link

Applications : Link

Download Code and Jars : Link

File : ImageExample.jrxml

<?xml version=”1.0" encoding=”UTF-8"?>
<! — Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1 →
<! — 2017–07–08T18:55:10 →
<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=”ImageExample” pageWidth=”595" pageHeight=”842" columnWidth=”555" leftMargin=”20" rightMargin=”20" topMargin=”20" bottomMargin=”20" uuid=”a6ee753e-8e87–44d2–86ac-63c43589fc1e”>
<property name=”com.jaspersoft.studio.data.defaultdataadapter” value=”Sample DB” />
<parameter name=”Image” class=”java.lang.String” />
<queryString>
<![CDATA[select * from emp]]>
</queryString>
<background>
<band splitType=”Stretch” />
</background>
<title>
<band height=”84" splitType=”Stretch”>
<staticText>
<reportElement x=”40" y=”24" width=”210" height=”30" uuid=”af69bb6b-1e48–4a36-a1b5–0244fefff37e” />
<textElement textAlignment=”Center” verticalAlignment=”Middle” />
<text>
<![CDATA[This is image example in jasper report]]>
</text>
</staticText>
<image>
<reportElement x=”340" y=”14" width=”50" height=”50" uuid=”5f12fba6-e86c-44e1–9151–161992a7b919" />
<imageExpression>
<![CDATA[$P{Image}]]>
</imageExpression>
</image>
</band>
</title>
<detail>
<band height=”125" splitType=”Stretch” />
</detail>
</jasperReport>

Call JasperReport from Jsp :

File : index.jsp

<%@page import=”java.io.OutputStream”%>
<%@ page contentType=”application/pdf”%>
<%@ page trimDirectiveWhitespaces=”true”%>
<%@page import=”java.util.HashMap”%>
<%@page import=”java.util.Map”%>
<%@page import=”net.sf.jasperreports.engine.design.JRDesignQuery”%>
<%@page import=”net.sf.jasperreports.engine.xml.JRXmlLoader”%>
<%@page import=”net.sf.jasperreports.engine.design.JasperDesign”%>
<%@page import=”net.sf.jasperreports.view.JasperViewer”%>
<%@ page import=”net.sf.jasperreports.engine.*”%>
<%@ page import=”java.io.File”%>
<%@ page import=”java.io.FileInputStream”%>
<%@ page import=”java.io.FileNotFoundException”%>
<%@ page import=”java.io.InputStream”%>
<%@ page import=”java.sql.Connection”%>
<%@ page import=”java.sql.DriverManager”%>
<%@ page import=”java.sql.SQLException”%>
<head>
<title>Image Example</title>
</head>
<body>
<%
Connection conn = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”, “root”, “root”);
String report = “C:/MyReports/ImageExample.jrxml”;
JasperReport jasperreport = JasperCompileManager.compileReport(report);
Map<String, Object> data = new HashMap<String, Object>();
data.put(“Image”, this.getServletContext().getRealPath(“/”) + “/images/asteroids.jpg”);
//data.put(“Image”, this.getServletContext().getRealPath(“/”)+”/images/green-goblin.png”);
JasperPrint jp = JasperFillManager.fillReport(jasperreport, data, conn);
JasperExportManager.exportReportToPdfStream(jp, response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
out.println(e);
}
%>
</body>
</html>

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store