iReport Ultimate Guide - Nimsoft Library
iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library
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
- Page 26 and 27: iReport Ultimate GuideFigure 2-14Re
- Page 28 and 29: iReport Ultimate GuideFigure 2-17Re
- Page 30 and 31: iReport Ultimate Guide9. The last s
- Page 32 and 33: iReport Ultimate GuideWhen we are f
- Page 34 and 35: iReport Ultimate GuideCode Example
- Page 36: iReport Ultimate GuideCode Example
- Page 39 and 40: Basic Notions of iReportjava.util.D
- Page 41 and 42: Basic Notions of iReportof a date,
- Page 43 and 44: Basic Notions of iReport(new java.l
- Page 45 and 46: Report StructureCHAPTER 4REPORT STR
- Page 47 and 48: Report StructureGroup HeaderDetailG
- Page 49 and 50: Report StructureThe page margin dim
- Page 51 and 52: Report StructureFigure 4-6Result of
- Page 53 and 54: Report StructureMultiple columns ar
- Page 55 and 56: Report StructureFigure 4-11Properti
- Page 57 and 58: Report StructureAs you can see in F
- Page 59 and 60: Report Structure4.1.3.12 ImportsThe
- Page 61 and 62: Report Structureexample, in a repor
- Page 63 and 64: Report ElementsCHAPTER 5REPORT ELEM
- Page 65 and 66: Report ElementsFigure 5-3Guidelines
- Page 67 and 68: Report ElementsFigure 5-6Element no
- Page 69 and 70: Report ElementsTable 5-1Formatting
- Page 71 and 72: Report ElementsFigure 5-10Table 5-2
- Page 73 and 74: Report ElementsFigure 5-11Custom el
- Page 75: Report Elements5.1.5.2 RectangleThe
- Page 79 and 80: Report ElementsTable 5-5OptionImage
- Page 81 and 82: Report ElementsCode Example 5-1Dyna
- Page 83 and 84: Report ElementsCode Example 5-2Prin
- Page 85 and 86: Report ElementsPDF font namePDF enc
- Page 87 and 88: Report ElementsFor your convenience
- Page 89 and 90: Report ElementsHere there are some
- Page 91 and 92: Report ElementsThe following briefl
- Page 93 and 94: 5.5 Adding Custom Components and Ge
- Page 95 and 96: Fields, Parameters, and VariablesCH
- Page 97 and 98: Fields, Parameters, and VariablesIn
- Page 99 and 100: 6.1.2 Accessing the SQL Query Desig
- Page 101 and 102: Fields, Parameters, and Variables6.
- Page 103 and 104: 6.2.3 Built-in ParametersFields, Pa
- Page 105 and 106: Fields, Parameters, and VariablesRe
- Page 107 and 108: Fields, Parameters, and Variables6.
- Page 109 and 110: Fields, Parameters, and VariablesTa
- Page 111 and 112: Bands and GroupsCHAPTER 7BANDS AND
- Page 113 and 114: Bands and Groupsexpression may be r
- Page 115 and 116: Bands and GroupsFigure 7-6Dragging
- Page 117 and 118: Bands and GroupsFigure 7-10The firs
- Page 119 and 120: Bands and GroupsFigure 7-14 shows t
- Page 121 and 122: 7.3 Other Group OptionsBands and Gr
- Page 123 and 124: Fonts and StylesCHAPTER 8FONTS AND
- Page 125 and 126: Fonts and StylesThe list of availab
<strong>iReport</strong> <strong>Ultimate</strong> <strong>Guide</strong>5.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, <strong>iReport</strong> pops up a file chooser dialog. This is the mostconvenient way to specify an image to use in the report. <strong>iReport</strong> 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