How to add image as parameter from classpath object class in jasper messages

I developed a jasper report using the ireport constructor, in which I added a logo in the report header. This image is added from a hard-coded path on the local computer. I need to add a logo image from my classpath class. To do this, I created a parameter for the image in the report that comes from the program.

InputStream imgInputStream = this.getClass().getResourceAsStream("header.png"); HashMap<String, Object> parameters = new HashMap<String, Object>(); parameters.put("dateFrom", datum1); parameters.put("dateTo", datum2); parameters.put("logo", imgInputStream); strQuery = "Select calldate,src,dst,duration,disposition,cdrcost from cdrcost where date(calldate) between '" + datum1 + "' and '" + datum2 + "'"; rs = conexiondb.Consulta(strQuery); JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs); //JasperPrint jasperPrint = JasperFillManager.fillReport(reportStream, parameters); JasperRunManager.runReportToPdfStream(reportStream, fos, parameters, resultSetDataSource); 

And below is a snippet of the image from the report:

 <image> <reportElement x="0" y="1" width="555" height="61"/> <imageExpression><![CDATA[$P{logo}]]> </imageExpression> </image> 
+4
source share
2 answers

We always pass an image instead of an InputStream. First upload the image and set it in the parameters map:

 BufferedImage image = ImageIO.read(getClass().getResource("/images/IMAGE.png")); parameters.put("logo", image ); 

Then the parameter is simply defined as:

 <parameter name="logo" class="Object" isForPrompting="false"> <parameterDescription><![CDATA[The letterhead image]]></parameterDescription> <defaultValueExpression><![CDATA[]]></defaultValueExpression> </parameter> 

And when it is placed in the report, it looks like this:

 <image> <reportElement x="324" y="16" width="154" height="38"/> <imageExpression><![CDATA[$P{logo}]]></imageExpression> </image> 
+23
source

You can easily get the URL form of class classpath / classloader. This is a valid input for <imageExpression> and therefore you can use it to insert an image into your pdf file. The following worked for me:

Parameter setting:

 URL url = this.getClass().getClassLoader().getResource("pdf/my_image.tif"); parameters.put("logo", url); 

Report Declaration:

 <parameter name="logo" class="java.net.URL"> <defaultValueExpression><![CDATA[]]></defaultValueExpression> </parameter> 

Use in the report.

 <image> <reportElement x="100" y="30" width="135" height="30"/> <imageExpression><![CDATA[$P{logo}]]></imageExpression> </image> 

Some additional observations

  • Before I used InputStream, it worked perfectly when displaying an image only once. When I needed to repeat the image, InputStream did not work, because the stream was consumed on the first display, so after it it cannot be used. I did not find an easy way to reset it.
  • I found out that URLs can be used here: http://jasperreports.sourceforge.net/sample.reference/images/index.html
+1
source

All Articles