Skip to main content

Post 10: iReport: Setting margins for your report and aligning the report header relative to report margins

Setting margins for your report and aligning the report header relative to report margins
You will often need to set margins for your report as well as its individual sections such as the report title, header, body, and footer.

This post shows you how you can set the margins for your report and also how to align the different components of your header with respect to each other as well as the report margin.

Getting ready

In order to work with this recipe, you will need the PostgreSQL database. Refer to the Post6:  iReport: Installation of PostgreSQL, which shows how you can install and run PostgreSQL. Note that your installation of PostgreSQL should be up and running before you proceed.

To inserting following the required data in database jasperdb2(only if data is not present ) then refer Post 8: iReport: Using dynamic titles thatcan change during report processing for Create a table named CustomerInvoices, Create table and copy sample data into the database.


  

You will connect iReport to your database hosted on PostgreSQL using the steps described in the Post 7:  iReport: Creating a report from relational data


How to do it...
1. Create the HeaderMarginAndAlignmentSample.jrxml file. This will display a report with a title and a multi-line header containing three name-value pairs of components, as shown next. Each name-value pair contains a static text and a text field component. For example, the first name-value pair shown contains a static text component (that is, Customer Name:) and a text field component (that is, $F{CustomerName}). 


  

2. Click on the Format menu and then choose the Page format item from the drop-down menu, as shown here: 
 
 
3. From the Page format window you may choose the format, report dimensions, margins, and page orientation of your report. This post focuses on margins. Other features will be covered in later recipes. Choose 0.5 inches as the left margin (a bit more then the default value) because most reports need some binding on the left side. Leave all other margins (that is right, top, and bottom) at their default values. 



4. You will notice that the name-value pairs of the multi-line header are not properly aligned. 

 

You can align first two pairs in two columns and center-align the third. Right-click on the Static Text component (Customer Name:) of the first name-value pair. A pop-up window will appear. Select Align from the pop-up menu, then select the Align to Left Margin option from the pop-up menu. 


You will see that the static text component is shifted to the extreme left side of the report margin, as shown next: 


5. Click on the text field component ($F{CustomerName}) of the Customer Name: pair. Use the mouse or arrow keys to drag it to the left till it sits adjacent to the Customer Name static text component.



6. Right-click on the text field component of the second name-value pair. A pop-up menu will appear; select Align from the pop-up menu, and then choose the Align to Right Margin option from the menu. This will shift the text field component to the extreme right of the report margin, as follows:



7. Now click on the static text component of the second name-value pair (Invoice Period:). Use the mouse or arrow keys to drag it to right till it sits adjacent to the accompanying text field component you right-aligned in step 6, as shown below.

8. Now look at the third name-value pair, which is on the second line of the header. You want to centre-align this name-value pair. Unfortunately, there is no way to accurately center-align this name-value pair using iReport. You will have to do it manually. For this purpose, hold down the Ctrl key and select the static text component (that is Product Name:). While holding the Ctrl key down, select the Text Field component of this name-value pair. This will select both components simultaneously, as shown next:
 9. Right-click on the selected name-value pair; a pop-up menu will appear. Select the Position option from the pop-up menu. A submenu will pop up; choose the Center Horizontally option from the submenu. 
 

This will center-align the selection, making the two components overlap each other, as shown in the following screenshot:

10. Left-click on the name-value pair, which is now overlapped and center-aligned. This will select the text field component of the name-value pair. Press the right-arrow key  48 times to move the component to the right. Now left-click on the static text component and press the left-arrow key 48 times to move the static text component to the left. Now this name-value pair is approximately center-aligned, as shown here:

 11. Save your report by selecting Save from the File menu and switch to the Preview tab. You will get a preview as shown here:


How it works...
Switch to XML tab to 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="HeaderMarginAndAlignmentSample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="539" leftMargin="36" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8bd85fae-bf3c-4628-a8d1-715b586b2a70">  
3:       <property name="ireport.zoom" value="1.0"/>  
4:       <property name="ireport.x" value="0"/>  
5:       <property name="ireport.y" value="0"/>  
6:       <queryString>  
7:            <![CDATA[ SELECT * from "public"."CustomerInvoices"  
8:  WHERE "public"."CustomerInvoices"."CustomerName" = 'iTwine Technologies'  
9:  AND "public"."CustomerInvoices"."InvoicePeriod" = 'Mar14'  
10:  AND "public"."CustomerInvoices"."ProductName" = 'Tech Material']]>  
11:       </queryString>  
12:       <field name="InvoiceID" class="java.lang.Integer"/>  
13:       <field name="CustomerName" class="java.lang.String"/>  
14:       <field name="ProductName" class="java.lang.String"/>  
15:       <field name="InvoiceValue" class="java.lang.Float"/>  
16:       <field name="InvoicePeriod" class="java.lang.String"/>  
17:       <background>  
18:            <band splitType="Stretch"/>  
19:       </background>  
20:       <title>  
21:            <band height="79" splitType="Stretch">  
22:                 <staticText>  
23:                      <reportElement uuid="8fb03fc4-33fa-4698-81a9-6c7f1e7ff1b7" x="113" y="13" width="329" height="43"/>  
24:                      <textElement>  
25:                           <font fontName="Verdana" size="24"/>  
26:                      </textElement>  
27:                      <text><![CDATA[Monthly Customer Invoices]]></text>  
28:                 </staticText>  
29:            </band>  
30:       </title>  
31:       <pageHeader>  
32:            <band height="71" splitType="Stretch">  
33:                 <staticText>  
34:                      <reportElement uuid="ae7a7a01-9238-40d7-aa84-2ac9466b4bbd" x="0" y="13" width="100" height="20"/>  
35:                      <textElement/>  
36:                      <text><![CDATA[Customer Name]]></text>  
37:                 </staticText>  
38:                 <textField>  
39:                      <reportElement uuid="27bd0d9d-1260-4de2-8f62-01c0a6ddb63a" x="113" y="13" width="100" height="20"/>  
40:                      <textElement/>  
41:                      <textFieldExpression><![CDATA[$F{CustomerName}]]></textFieldExpression>  
42:                 </textField>  
43:                 <staticText>  
44:                      <reportElement uuid="5444c960-b674-43f9-9967-8a4631524c80" x="339" y="21" width="100" height="20"/>  
45:                      <textElement/>  
46:                      <text><![CDATA[Product Name]]></text>  
47:                 </staticText>  
48:                 <textField>  
49:                      <reportElement uuid="7991a110-72a2-4480-af8d-c87f5ba23cd4" x="439" y="21" width="100" height="20"/>  
50:                      <textElement/>  
51:                      <textFieldExpression><![CDATA[$F{ProductName}]]></textFieldExpression>  
52:                 </textField>  
53:                 <staticText>  
54:                      <reportElement uuid="32769ca7-2abb-43df-a21d-b0d45e2b0146" x="160" y="51" width="100" height="20"/>  
55:                      <textElement/>  
56:                      <text><![CDATA[Invoice Period]]></text>  
57:                 </staticText>  
58:                 <textField>  
59:                      <reportElement uuid="e1a7f64c-dae0-411c-983d-81b957815e97" x="260" y="51" width="100" height="20"/>  
60:                      <textElement/>  
61:                      <textFieldExpression><![CDATA[$F{InvoicePeriod}]]></textFieldExpression>  
62:                 </textField>  
63:            </band>  
64:       </pageHeader>  
65:       <columnHeader>  
66:            <band height="61" splitType="Stretch"/>  
67:       </columnHeader>  
68:       <detail>  
69:            <band height="125" splitType="Stretch"/>  
70:       </detail>  
71:       <columnFooter>  
72:            <band height="45" splitType="Stretch"/>  
73:       </columnFooter>  
74:       <pageFooter>  
75:            <band height="54" splitType="Stretch"/>  
76:       </pageFooter>  
77:       <summary>  
78:            <band height="42" splitType="Stretch"/>  
79:       </summary>  
80:  </jasperReport> 

In this JRXML, you can see from the <jasperReport> tag that its attribute named leftMargin is highlighted. Notice that you set the leftMargin attribute to 0.5 inches in step 3 of this recipe. iReport automatically translates the value 0.5 inches into a corresponding value of the leftMargin attribute.
The <band> tag of <pageHeader> contains three pairs of <staticText> and <textField> tags. I have shown only one name-value pair for the sake of clarity. The <staticText> and <textField> tags contain a <reportElement> child. Look at the x and y attributes of the <reportElement> tag that I have shown highlighted. The y attributes of both <reportElement> tags have the same values. The x attributes are different and their difference is equal to the width of the first component (the width attribute of the first <reportElement> tag).


Note:The best way to judge these attribute values is by printing the final report.





Comments

Post a Comment

Popular posts from this blog

TIME TABLE: VTU BE/B.Tech June/July 2016 Exam Time table Draft Layout

Semester-wise June-July 2016 Exam time table (Draft Layout) Are You Using updated Kwikstudy App?

Walkins Bangalore: 01st-Oct-2016

1. UTC Aerospace : GET – S&IS-Software Team hiring Electronics & CSE / IT Freshers ! Walkin on 1st OCT | BE/BTECH - 2015 & 2016 both batch can try Selection Process : Written Test Group Discussion Technical & HR Interviews How To Apply : It is a walk-in drive for 2016 batch eligible freshers on 1 Oct 2016 at Bangalore location. Interested and eligible candidates can walk-in to the following venue on 1 Oct 2016 Date of Drive : 01 Oct 2016 Reporting Time : 8:00 AM to 11:30 AM Venue : UTC Aerospace Systems – Site 1, Sy. Nos. 14/1 & 15/1, Maruthi Industrial State, Phase 2, Hoody Village, Whitefield Road, K R Puram Hobli, Bangalore – 560048 ______________________________________ 2. HP : Software Eng/ Software Tester (Call based ,less chance of getting entry , try at your own risk) Event Date : 1-October-2016 Event Time : 8 : 00 am Job Location : Bangalore / Chennai Job Description: Software Engineer /

How to get VTU Provisional Degree Certificate (PDC) ?

Here is the procedure to get the Provisional Degree Certificate from the VTU by passed out candidates. You can get the certificate through post by sending the required documents to VTU. Documents and things required: 1. Provisional Degree Certificate (PDC) application form 2. DD of specified fees amount or print copy of receipt if you paid the fees online 3. A letter to the VTU registrar 4. A4 size envelope Step 1: Fill the PDC application You can download the PDC application form from the VTU website. Click below link for application download. PDC application form download     After downloading the application take a print of it and properly fill the form. Step 2: Make a DD of specified amount as the fees of PDC: Go to the bank and make the DD of prescribed fee amount for the PDC. The fee amount is specified in the above link from where you downloaded the application. Make the DD in favor of " Finance Officer, VTU Belgaum ".   Wr