iReport Ultimate Guide - Nimsoft Library

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

13.07.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!