iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library

13.07.2015 Views

iReport Ultimate Guide5.2 Working with ImagesFigure 5-17Image elementAn image is the most complex of the graphic elements. It can be used to insert raster images (such as GIF, PNG and JPEGimages) in the report, but it can be also used as a canvas object to render, for example, a Swing component, or to leveragesome custom rendering code.When you drag an image element from the Palette into the Designer, iReport pops up a file chooser dialog. This is the mostconvenient way to specify an image to use in the report. iReport will not save or store the selected image anywhere, it will justuse the file location, translating the absolute path of the selected image into an expression to locate the file when the report isexecuted. The expression is then set as the value for the Image Expression property. Here is a sample expression:"C:\\Documents and Settings\\gtoffoli\\Desktop\\splashscreen.png"As you can see, this is a Java (or Groovy or JavaScript) expression, not just the value of a file path. It starts and ends withdouble quotes, and the back slash character (\) is escaped with another back slash (\\).The Image Expression Class defines what kind of object is returned by the Image Expression. In this case, it is of the typejava.lang.String, but there are several other options.Table 5-4 summarizes the values that the Image Expression Class can adopt and describes how the Image Expressionresult is interpreted and used.Table 5-4 Image Expression Class ValuesTypejava.lang.Stringjava.io.FileInterpretationA string is interpreted like a file name. UR will try to interpret the string likean absolute path. If no file is found, it will try to load a resource from the classpath withthe specified name. Correct expressions are:“c:\\devel\\ireport\\myImage.jpg”“com/mycompany/resources/icons/logo.gif”Specifies a file object to load as an image.A correct expression could be:new java.io.File(“c:\\myImage.jpg”)76

Report ElementsTable 5-4TypeImage Expression Class Values, continuedInterpretationjava.net.URLjava.io.InputStreamjava.awt.ImageJRRenderableSpecifies the java.net.URL object. It is useful when you have to export the report inHTML format. A correct expression could be:new java.net.URL(“http://127.0.0.1/test.jpg”)Specifies a java.io.InputStream object which is ready for reading. In this case,we do not consider that the image exists and that it is in a file. In particular, we couldread the image from a database and return the inputStream for reading. A correctexpression could be:MyUtil.getInputStream(${MyField})Specifies a java.awt.Image object; it is probably the simplest object to return whenan image has to be created dynamically. A correct expression could be:MyUtil.createImage()Specifies an object that uses thenet.sf.jasperreports.engine.JRRenderable interface.You are free to add an image by explicitly defining the full absolute path of the image file in your expression. This is an easyway to add an image to the report, but, overall, it has a big impact on the report’s portability, since the file may not be found onanother machine (for instance, after deploying the report on a web server or running the report on a different computer).There are two best practices here:• Parametrize the image expression containing the folder where your images resides (possibly using a parameter with adefault value), then composing the expression like this:$P{MY_IMAGES_DIRECTORY} + “myImage.png”At run time in a hypothetical application, the value for the parameter MY_IMAGES_DIRECTORY can be set by theapplication itself. If a value for the parameter is not provided, we can still return a default value (we’ll see how to create aparameter and set a default value in the next chapter). The advantage of this solution is that the location of the directorywhere the images reside is not defined discretely within the report, but can be provided dynamically.• The second option is to use the classpath. The classpath defines the directories and JAR file locations where a Javaapplication like UR looks for classes and resources. If the application uses the Java Virtual Machine, it isusually easy to add directories to the classpath.In iReport, the classpath can be extended from the Options dialog (Window > Options > iReport > Classpath). When animage is in the classpath, the only required information UR needs in order to find and render the image is theresource name (that is a kind of path that is relative to the classpath). By default, when executing a report, iReport adds thedirectory in which the report resides to the classpath. Suppose you have a report in a certain directory, let’s sayc:\test\myReport.jrxml, and in the same directory you have an image named myImage.png. To use it in the report, you canset Image Expression to myImage.png. Since the report’s directory is in the classpath, the image will be foundautomatically.This process is still valid if the image resides in a subdirectory of the classpath. You will have to specify the subdirectorypath, using a Unix-style path notation. For example, if your image resides in c:\test\images rather than c:\test, the resourceis found with the expression /images/myImage.png.This method of resolving resource locations is applied in many other parts of UR, as well (for example, inlocating a subreport Jasper file, a resource bundle, a scriptlet class, and so on).77

Report ElementsTable 5-4TypeImage Expression Class Values, continuedInterpretationjava.net.URLjava.io.InputStreamjava.awt.ImageJRRenderableSpecifies the java.net.URL object. It is useful when you have to export the report inHTML format. A correct expression could be:new java.net.URL(“http://127.0.0.1/test.jpg”)Specifies a java.io.InputStream object which is ready for reading. In this case,we do not consider that the image exists and that it is in a file. In particular, we couldread the image from a database and return the inputStream for reading. A correctexpression could be:MyUtil.getInputStream(${MyField})Specifies a java.awt.Image object; it is probably the simplest object to return whenan image has to be created dynamically. A correct expression could be:MyUtil.createImage()Specifies an object that uses thenet.sf.jasperreports.engine.JRRenderable interface.You are free to add an image by explicitly defining the full absolute path of the image file in your expression. This is an easyway to add an image to the report, but, overall, it has a big impact on the report’s portability, since the file may not be found onanother machine (for instance, after deploying the report on a web server or running the report on a different computer).There are two best practices here:• Parametrize the image expression containing the folder where your images resides (possibly using a parameter with adefault value), then composing the expression like this:$P{MY_IMAGES_DIRECTORY} + “myImage.png”At run time in a hypothetical application, the value for the parameter MY_IMAGES_DIRECTORY can be set by theapplication itself. If a value for the parameter is not provided, we can still return a default value (we’ll see how to create aparameter and set a default value in the next chapter). The advantage of this solution is that the location of the directorywhere the images reside is not defined discretely within the report, but can be provided dynamically.• The second option is to use the classpath. The classpath defines the directories and JAR file locations where a Javaapplication like UR looks for classes and resources. If the application uses the Java Virtual Machine, it isusually easy to add directories to the classpath.In <strong>iReport</strong>, the classpath can be extended from the Options dialog (Window > Options > <strong>iReport</strong> > Classpath). When animage is in the classpath, the only required information UR needs in order to find and render the image is theresource name (that is a kind of path that is relative to the classpath). By default, when executing a report, <strong>iReport</strong> adds thedirectory in which the report resides to the classpath. Suppose you have a report in a certain directory, let’s sayc:\test\myReport.jrxml, and in the same directory you have an image named myImage.png. To use it in the report, you canset Image Expression to myImage.png. Since the report’s directory is in the classpath, the image will be foundautomatically.This process is still valid if the image resides in a subdirectory of the classpath. You will have to specify the subdirectorypath, using a Unix-style path notation. For example, if your image resides in c:\test\images rather than c:\test, the resourceis found with the expression /images/myImage.png.This method of resolving resource locations is applied in many other parts of UR, as well (for example, inlocating a subreport Jasper file, a resource bundle, a scriptlet class, and so on).77

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!