views:

288

answers:

1

I need to put images in my Jasper reports that match rows in a database. I have the image data in proprietary archive files which I can easily extract in Java code. I need to know how to connect my Java code to the Jasper report to make the extraction process happen, something like BIRT's onRowSets() method. (I need overview documentation about how the fill process works.)

If I define a POJO/bean data source, I won't be able to easily use the database as well. Can I embed the necessary Java extraction code right in the XML? This example at the Jasper Assistant site seems to indicate that I can:

   <imageExpression class="java.io.File">
      <![CDATA[(new File($P{BaseDir}, "logo_"+$P{ReportCollecter}+".gif").exists()) 
         ? new File($P{BaseDir}, "logo_"+$P{ReportCollecter}+".gif") 
         : new File($P{BaseDir}, "logo_BLANK.gif")]]>
   </imageExpression>

Is that new File() actually Java code embedded in the XML? I'm willing to write code, but I can't seem to find any complete class code examples (I think I want something like an extended image object).

A: 

By default, the current version of iReport (3.6.0) uses Groovy (a sort of Java scripting language) which has full access to any Java libraries in your classpath.

All I had to do was write some code to extract the image from the archive and return an InputStream object (from a ByteArrayInputStream). Then I made a jar file and called the function from the Jasper jrxml file like so:

<imageExpression class="java.io.InputStream">
   <![CDATA[
com.mycompany.jasper.MyImageExtractor.getImage($F{IMAGE_URL}, $V{JasperVariable})
   ]]>
</imageExpression>

(You can also link to your code with the iReport GUI -- set the "Image Expression" property for your new image to the line after the "CDATA")

For simpler Java coding needs, you can write simple Groovy code right in the XML of the report.

Note that Jasper variables can be accessed in Groovy with $V{VariableName} and SQL Database columns can be accessed with $F{ColumnName}.

sventech