Jasper Report Foundset Images

Servoy Version: 6.1.2 - build 1421
Jaspersoft iReport Designer 4.6.0

I need help / clarification regarding the printing of images from a foundset passed to a Jasper report (not static images passed as parameters to the report…I understand how to do that).

I found several discussions on modifying the Jasper report XML, and they all basically say the same thing:

<field name="image_field"/>
<import value="net.sf.jasperreports.engine.util.JRImageLoader"/> 
<imageExpression class="java.awt.Image"><![CDATA[JRImageLoader.loadImage((byte[])$F{image_field})]]></imageExpression>
Image Field Expression: JRImageLoader.loadImage((byte[])$F{image_field})

I tried this, but iReport complains about the <import…> line of XML code (depending where I place it in the XML) and issues error messages like:

Error loading the report template:
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.

and

Error loading the report template:
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘import’. One of '{“http://jasperreports.sourceforge.net/jasperreports”:box, “http://jasperreports.sourceforge.net/jasperreports”:graphicElement, “http://jasperreports.sourceforge.net/jasperreports”:imageExpression, “http://jasperreports.sourceforge.net/jasperreports”:anchorNameExpression, “http://jasperreports.sourceforge.net/jasperreports”:hyperlinkReferenceExpression, “http://jasperreports.sourceforge.net/jasperreports”:hyperlinkAnchorExpression, “http://jasperreports.sourceforge.net/jasperreports”:hyperlinkPageExpression,

So, one of my questions is where does this <import…> code go?

Rob Gansevles suggested another approach:

Alternatively, you can also call a global method from the report:

function toAWTImage(media)
{
    var img = new Packages.java.io.ByteArrayInputStream(media);
    return Packages.javax.imageio.ImageIO.read(img);
}

and use field <field
name="globals.toAWTImage(media_field_name_or_variable_with_media_field_data)"
class="java.awt.Image"/> in your report.

I created the global method in Servoy and added the field to the report’s XML. But, when I attempt compile the report, the following error is issued:

Error filling print… Unknown column name : globals.toAWTImage(mail_signature)
net.sf.jasperreports.engine.JRException: Unknown column name : globals.toAWTImage(mail_signature)

So, I am obviously misunderstanding how this works. Actually, I am surprised that the report can call a Servoy method, because I thought that the data flow was one-way from Servoy to the report (or, am I misunderstanding this as well?).

I would greatly appreciate any assistance with this.

Thanks in advance!

I still have this problem and am open to suggestions. Anyone?

kwpsd:

I found several discussions on modifying the Jasper report XML, and they all basically say the same thing:

<field name="image_field"/>
Image Field Expression: JRImageLoader.loadImage((byte[])$F{image_field}) > ``` > > > I tried this, but iReport complains about the line of XML code (depending where I place it in the XML) and issues error messages like: > ... > So, one of my questions is where does this code go?

Hi Kim,
You do not need to add that <import…>. You should be able to simply use:

<imageExpression class="java.awt.Image"><![CDATA[net.sf.jasperreports.engine.util.JRImageLoader.loadImage((byte[])$F{image_field})]]></imageExpression>

Regards,
Andrei

kwpsd:
Rob Gansevles suggested another approach:

Alternatively, you can also call a global method from the report:

function toAWTImage(media)
{
var img = new Packages.java.io.ByteArrayInputStream(media);
return Packages.javax.imageio.ImageIO.read(img);
}

and use field in your report.



I created the global method in Servoy and added the field to the report's XML. But, when I attempt compile the report, the following error is issued:



> Error filling print... Unknown column name : globals.toAWTImage(mail_signature) 
> net.sf.jasperreports.engine.JRException: Unknown column name : globals.toAWTImage(mail_signature)

Kim, on this one: please try using

<field name="toAWTImage(media_field_name_or_variable_with_media_field_data)" class="java.awt.Image"/>

so without the globals. prefix, possibly like suggested here on the forum.

Hope this helps,
Andrei

Hi, Andrei…and thank you for repsonding.

You do not need to add that <import…>. You should be able to simply use:
Code: Select all

I guess I was looking at some old threads that recommended using the <import…> line of XML code. Your recommendation worked but not before changing the ‘image_field’ property in iReport from java.io.InputStream to java.lang.Object.

Again, thank you for your assistance!