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 recipe 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.
In order to work with this recipe, you will need the PostgreSQL database. Refer to the installPostgreSQL.txt
file included in the source code download for this chapter, which shows how you can install and run PostgreSQL. Note that your installation of PostgreSQL should be up and running before you proceed.
The source code for this chapter also includes a file named copySampleDataIntoPGS.txt
. The copySampleDataIntoPGS.txt
file will help you create a database named jasperdb1
, a table named CustomerInvoices
with five columns (InvoiceID, CustomerName, InvoicePeriod, ProductName
, and InvoiceValue)
and copy sample data for this recipe into the table.
After inserting the required data, you will connect iReport to your database hosted on PostgreSQL using the steps described in the Creating a report from relational data recipe later in Chapter 4.
HeaderMarginAndAlignmentSample.jrxml
file from the Task7
folder of the source code for this chapter. 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})
.Switch to XML tab to see the following JRXML code:
<?xml version="1.0" encoding="UTF-8"?> <jasperReport leftMargin="36" rightMargin="20" topMargin="20" bottomMargin="20"> <!-- other tags of JasperReports XML --> <pageHeader> <band height="63" splitType="Stretch"> <staticText> <reportElement x="0" y="11" width="100" height="20"/> <textElement> <font isBold="true"/> </textElement> <text><![CDATA[Customer Name :]]></text> </staticText> <textField> <reportElement x="100" y="11" width="100" height="20"/> <textElement/> <textFieldExpression class="java.lang.String"> <![CDATA[$F{field}]]> </textFieldExpression> </textField> <!-- other tags of JasperReports XML --> </band> </pageHeader> <!-- other tags of JasperReports XML --> </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).
18.224.44.53