iReport Ultimate Guide - Nimsoft Library
iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library
iReport Ultimate GuideAs always, all the measurements must be set in pixels.The four sides of the border can be edited individually by selecting them in the preview frame. When no sides are selected,changes are applied to all of them.Image elements can have a hyperlink. Not all the export formats support them, but they have been verified in HTML, PDF andXLS. To define a hyperlink, right-click the image element and select the Hyperlink menu item. The hyperlink definition dialogwill appear. We will explain in depth how to define an hyperlink using this dialog later in the chapter.5.2.2 Loading an Image from the Database (BLOB Field)If you need to print images that are stored in a database (that is, using a BLOB column) what you need to do is assign the fieldthat will get the BLOB value the type java.awt.Image (report fields will be explained in the next chapter). Create an imageelement by dragging the image element tool from the palette into the designer (that is, into the Detail band), click Cancel whenthe file chooser prompts. Then, in the image element properties sheet, change the Image Expression Class tojava.awt.Image and set as Image Expression the field object (that is, $F{MyImageField} ).5.2.3 Creating an Image DynamicallyTo create an image dynamically requires some Java knowledge. Here we will show the best solution to modify or create animage to be printed in a report.There are several ways to create an image dynamically in UR. The first option is to write a class that produces ajava.awt.Image object and call a method of this class in the Image Expression of the image element. The expression wouldlook like:MyImageGenerator.generateImage()where MyImageGenerator is a class with the static method generateImage() that returns the java.awt.Image object.The problem with this solution is that, since the image created would be a raster image with a specific with and height, in thefinal result there could be there a loss of quality, especially when the document is zoomed in, or when the final output is a PDFfile.Generally speaking, the best format of an image that must be rendered in a document is an SVG, which provides high imagequality regardless of original capture resolution. In order to ease the generation of a custom image, UR provides aninterface called JRRenderable that a developer can implement to get the best rendering result. A convenient class to initialuse of this interface is JRAbstractSVGRenderable. The only method to implement here is:public void render(Graphics2D g2d, Rectangle2D rect)which is where you should put your code to render the image. Code Example 5-1 shows a simple implementation of aJRAbstractSVGRenderable to paint the outline text “UR!!” inside an image element using a gradientbackground.Code Example 5-1 Dynamic image generationpackage com.jaspersoft.ireport.samples;import java.awt.Color;import java.awt.Font;import java.awt.GradientPaint;import java.awt.Graphics2D;import java.awt.Rectangle;import java.awt.Shape;import java.awt.font.FontRenderContext;import java.awt.font.TextLayout;import java.awt.geom.AffineTransform;import java.awt.geom.Rectangle2D;importnet.sf.jasperreports.engine.JRAbstractSvgRenderer;import net.sf.jasperreports.engine.JRException;80
Report ElementsCode Example 5-1Dynamic image generation, continued/**** @author gtoffoli*/public class CustomImageRenderer extends JRAbstractSvgRenderer {public void render(Graphics2D g2d, Rectangle2D rect) throws JRException {// Save the Graphics2D affine transformAffineTransform savedTrans = g2d.getTransform();Font savedFont = g2d.getFont();// Paint a nice background...g2d.setPaint(new GradientPaint(0,0, Color.ORANGE,0,(int)rect.getHeight(), Color.PINK));g2d.fillRect(0,0 , (int)rect.getWidth(), (int)rect.getHeight());Font myfont = new Font("Arial Black", Font.PLAIN, 50);g2d.setFont(myfont);FontRenderContext frc = g2d.getFontRenderContext();String text = new String("UR!!!");TextLayout textLayout = new TextLayout(text, myfont, frc);Shape outline = textLayout.getOutline(null);Rectangle r = outline.getBounds();// Translate the graphic to center the textg2d.translate((rect.getWidth()/2)-(r.width/2),rect.getHeight()/2+(r.height/2));g2d.setColor(Color.BLACK);g2d.draw(outline);// Restore the Graphics2D affine transformg2d.setFont(savedFont);g2d.setTransform( savedTrans );}}The final result is shown in Figure 5-19. The CustomImageRenderer class implements the interfaceJRAbstractSvgRenderer. The renderer just fills the background with the fillRect method using a Gradient Paint, createsa shape out of the “UR!!!” text, and renders the shape centered with a translation.81
- 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 and 76: Report Elements5.1.5.2 RectangleThe
- Page 77 and 78: Report ElementsTable 5-4TypeImage E
- Page 79: Report ElementsTable 5-5OptionImage
- 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
- Page 127 and 128: Fonts and StylesFigure 8-5Font Exte
- Page 129 and 130: Fonts and StylesFigure 8-7Font Exte
Report ElementsCode Example 5-1Dynamic image generation, continued/**** @author gtoffoli*/public class CustomImageRenderer extends JRAbstractSvgRenderer {public void render(Graphics2D g2d, Rectangle2D rect) throws JRException {// Save the Graphics2D affine transformAffineTransform savedTrans = g2d.getTransform();Font savedFont = g2d.getFont();// Paint a nice background...g2d.setPaint(new GradientPaint(0,0, Color.ORANGE,0,(int)rect.getHeight(), Color.PINK));g2d.fillRect(0,0 , (int)rect.getWidth(), (int)rect.getHeight());Font myfont = new Font("Arial Black", Font.PLAIN, 50);g2d.setFont(myfont);FontRenderContext frc = g2d.getFontRenderContext();String text = new String("UR!!!");TextLayout textLayout = new TextLayout(text, myfont, frc);Shape outline = textLayout.getOutline(null);Rectangle r = outline.getBounds();// Translate the graphic to center the textg2d.translate((rect.getWidth()/2)-(r.width/2),rect.getHeight()/2+(r.height/2));g2d.setColor(Color.BLACK);g2d.draw(outline);// Restore the Graphics2D affine transformg2d.setFont(savedFont);g2d.setTransform( savedTrans );}}The final result is shown in Figure 5-19. The CustomImageRenderer class implements the interfaceJRAbstractSvgRenderer. The renderer just fills the background with the fillRect method using a Gradient Paint, createsa shape out of the “UR!!!” text, and renders the shape centered with a translation.81