13.07.2015 Views

iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>iReport</strong> <strong>Ultimate</strong> <strong>Guide</strong>As 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

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

Saved successfully!

Ooh no, something went wrong!