Insert image in iReport using xml datasource

Share

When using an XML file as a datasource for a report designed with iReport-Designer for JasperReports, you sometimes want to add an image, which also should be retrieved from the XML datasource.
For this, we have to first find a way to represent that image in the XML file, and then, interpret it from there and obtain an actual image file inside the report.

  • Represent the image in the XML file
  • In order to represent an image inside an XML file, we have to obtain a string out of it, so we can write that string inside an xml tag. We will use a byte array that represents the image, and encode it using Apache’s Base64 class which provides utility methods for encoding/decoding.

    import org.apache.commons.codec.binary.Base64;
    import org.apache.commons.codec.binary.StringUtils;
    ...
    //Obtain an input stream for the image we want to represent in the xml
    InputStream is = ClassName.class.getResourceAsStream("image.jpg");
    //create a byte array of the appropriate size for holding the image bytes
    byte[] buffer = new byte[is.available()];
    //populate the byte array with actual data
    is.read(buffer);
    //obtain an encoded string from the byte array, which will be written inside the xml
    String encodedImgStr = StringUtils.newStringIso8859_1(Base64
    					.encodeBase64(buffer));

    Write the encoded base64 message inside the xml like:
    <image><![CDATA[ ... Base64 mess here ... ]]></image></code>

  • Read the actual image from the XML file inside iReport
    1. inside the report that has the previously created xml file as a datasource, create a String field named "image", which represents the encoded string from the datasource.
    2. then create a variable which will interpret this string and transforms it into a ByteArrayInputStream. The variable has java.io.ByteArrayInputStream as a variable class, and as a variable expression:
    3. new ByteArrayInputStream(Base64.decodeBase64(StringUtils.getBytesIso8859_1($F{image})))

      Note! you have to add these imports for the report element
      org.apache.commons.codec.binary.Base64
      org.apache.commons.codec.binary.StringUtils
      and add the commons-codec.jar file to the iReport classpath (Tools->Options->Classpath)

    4. create a visual image field which uses the previously created variable as an image expression and java.io.InputStream as the expression class

    And you should see the image properly rendered inside the report

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close