Inserting a company logo in the
title of your report
JasperReports allows you to place your company logo anywhere in your report. You may even use your logo as a background image or a watermark in a report.
This post shows you how you can place, position, and size your company logo within the title of a business report.
How to do it...
1. Create the ASimpleTitleWithLogo.jrxml file as the Designer tab of iReport shows an empty report with a title (iTwine Technologies) as shown in the following screenshot:
1. Create the ASimpleTitleWithLogo.jrxml file as the Designer tab of iReport shows an empty report with a title (iTwine Technologies) as shown in the following screenshot:
2. Use the palette of components available on the right of your iReport main window to drag an Image component into the Title section of your report.
Now you can browse to your image file that contains your company logo. JasperReports supports the rendering of all popular image formats such as JPG, GIF, BMP, PNG, and so on.
3. Once you have selected the image file, click on Open. Your image will be placed in the Title section of your report, as follows:
4. The logo shown here does not fit and looks a bit too big or small . You need to adjust the size of the image. Click on the logo; its properties will appear in the Properties window just below the Palette of components. Look for the Width property and change its value to 180. Similarly, look for the height property and change it to 80.
Now it looks quite normal, but still its placement in the Title section looks inappropriate, as shown in the following screenshot:
5. Click on the logo and then use the mouse or arrow keys to place it in the top-right corner of the Title section, as shown in the following screenshot:
Now it looks quite normal, but still its placement in the Title section looks inappropriate, as shown in the following screenshot:
5. Click on the logo and then use the mouse or arrow keys to place it in the top-right corner of the Title section, as shown in the following screenshot:
6. Switch to the Preview tab; you will see the preview of your report as shown here:
How it works...
Switch to the XML tab and you will see the following JRXML code:
Notice the <image> tag, shown highlighted in the preceding code. This <image> tag represents the logo you inserted into the Title section.
The <image> tag has a child named <imageExpression>, which specifies the <image> that you want to insert. The <imageExpression> tag provides two important bits of information:
The class attribute of the <imageExpression> tag specifies what type of image you want to use. In this case, you just want to provide the URL of the image. So it is a Java string. Another possibility can be that you want to pass a Java file object to JasperReports. In that case, the value of the class attribute will be java.io.File. The content of the <imageExpression> tag specifies the actual URL.
Note:Add images to source folders and check the images with code.
Also, try to avoid spaces in image filenames.
How it works...
Switch to the XML tab and you will see the following JRXML code:
1: <?xml version="1.0" encoding="UTF-8"?>
2: <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="ASimpleTitleWithLogo" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6ce0ff76-e37a-410f-8a2c-7c91aedf3dc5">
3: <property name="ireport.zoom" value="1.0"/>
4: <property name="ireport.x" value="0"/>
5: <property name="ireport.y" value="0"/>
6: <background>
7: <band splitType="Stretch"/>
8: </background>
9: <title>
10: <band height="89" splitType="Stretch">
11: <staticText>
12: <reportElement uuid="653401f7-e727-457c-a191-10332e94034d" x="0" y="19" width="373" height="34"/>
13: <textElement>
14: <font fontName="Verdana" size="24"/>
15: </textElement>
16: <text><![CDATA[iTwine Technologies]]></text>
17: </staticText>
18: <staticText>
19: <reportElement uuid="35f19b2c-b8d1-4a52-af9f-7710c6146ece" x="0" y="56" width="207" height="20"/>
20: <textElement>
21: <font fontName="Verdana" size="13"/>
22: </textElement>
23: <text><![CDATA[Because Resources are Finite!]]></text>
24: </staticText>
25: <image>
26: <reportElement uuid="41135236-92a2-432d-98e2-553d71354715" x="375" y="9" width="180" height="80"/>
27: <imageExpression><![CDATA["C:\\Documents and Settings\\Revan\\Desktop\\iTwine Technogis Logo.jpeg"]]></imageExpression>
28: </image>
29: </band>
30: </title>
31: <pageHeader>
32: <band height="35" splitType="Stretch"/>
33: </pageHeader>
34: <columnHeader>
35: <band height="61" splitType="Stretch"/>
36: </columnHeader>
37: <detail>
38: <band height="125" splitType="Stretch"/>
39: </detail>
40: <columnFooter>
41: <band height="45" splitType="Stretch"/>
42: </columnFooter>
43: <pageFooter>
44: <band height="54" splitType="Stretch"/>
45: </pageFooter>
46: <summary>
47: <band height="42" splitType="Stretch"/>
48: </summary>
49: </jasperReport>
Notice the <image> tag, shown highlighted in the preceding code. This <image> tag represents the logo you inserted into the Title section.
The <image> tag has a child named <imageExpression>, which specifies the <image> that you want to insert. The <imageExpression> tag provides two important bits of information:
The class attribute of the <imageExpression> tag specifies what type of image you want to use. In this case, you just want to provide the URL of the image. So it is a Java string. Another possibility can be that you want to pass a Java file object to JasperReports. In that case, the value of the class attribute will be java.io.File. The content of the <imageExpression> tag specifies the actual URL.
Note:Add images to source folders and check the images with code.
Also, try to avoid spaces in image filenames.
Comments
Post a Comment