I have list of data to print. I want to know if there is any way to find if the row is the first row in JasperReports's report for every page?
This task can be solved with in several ways in addition of using JasperReports engine built-in variable PAGE_COUNT
.
We can create the variable with resetType="Page"
for counting number of rows at page.
<variable name="counterOnPage" class="java.lang.Integer" resetType="Page" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
The simple csv datasource will be good for samples.
First page. Row 1
First page. Row 2
First page. Row 3
First page. Row 4
First page. Row 5
Second page. Row 1
Second page. Row 2
Second page. Row 3
Second page. Row 4
Second page. Row 5
Third page. Row 1
Third page. Row 2
Third page. Row 3
Third page. Row 4
Third page. Row 5
The name of data adapter for this datasource in the example below is rows.csv. The name of a field is name
The height of report is enough for showing only 5 rows per page.
<?xml version="1.0" encoding="UTF-8"?>
<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="Using variable" pageWidth="250" pageHeight="75" whenNoDataType="AllSectionsNoDetail" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="rows.csv"/>
<field name="name" class="java.lang.String"/>
<variable name="counterOnPage" class="java.lang.Integer" resetType="Page" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<detail>
<band height="15">
<textField>
<reportElement x="0" y="0" width="180" height="15" uuid="1b535a7e-7a8e-4e44-91ff-c0b8415afcf1"/>
<textFieldExpression><![CDATA[($V{counterOnPage} == 1) ? $F{name} + "!" : $F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
For first row we will add the symbol "!" to the end of a string.
The result (pdf file) will be:
Here is a little hack using parameters map ($P{REPORT_PARAMETERS_MAP}
).
We can set some "flag" (instead of using variable) for marking the first row at page. We can put textField at pageHeader for setting value isFirst at report's Map, for example.
<textField>
<reportElement x="0" y="0" width="100" height="1"/>
<textFieldExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.put("isFirst", true)]]></textFieldExpression>
</textField>
-- we are initializing the value of our flag.
We should add check for isFirst value and change the value of this flag after first use. The fake textField will do this job
<textField>
<reportElement x="180" y="0" width="0" height="15"/>
<textFieldExpression><![CDATA[((Boolean) $P{REPORT_PARAMETERS_MAP}.put("isFirst", false)) ? "" : ""]]></textFieldExpression>
</textField>
-- we are reseting the value of our flag.
The datasource will be the same.
<?xml version="1.0" encoding="UTF-8"?>
<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="Using Map" pageWidth="250" pageHeight="76" whenNoDataType="AllSectionsNoDetail" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="rows.csv"/>
<field name="name" class="java.lang.String"/>
<pageHeader>
<band height="1">
<textField>
<reportElement x="0" y="0" width="100" height="1"/>
<textFieldExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.put("isFirst", true)]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="15">
<textField>
<reportElement x="0" y="0" width="180" height="15">
</reportElement>
<textFieldExpression><![CDATA[($P{REPORT_PARAMETERS_MAP}.get("isFirst") != null && ((Boolean) $P{REPORT_PARAMETERS_MAP}.get("isFirst")) == true) ? "! " + $F{name} + "!" : $F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="180" y="0" width="0" height="15"/>
<textFieldExpression><![CDATA[((Boolean) $P{REPORT_PARAMETERS_MAP}.put("isFirst", false)) ? "" : ""]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
For the first row we will add the symbol "!" to the begin and the end of a string.
The result (pdf file) will be: