I have a need to create a style that would be applied to all "cells"/Element/object on a report.
is there a way to do this other than creating condition for each field/Variable ?
For a simple example using the code below I want to create a conditional style what would work for Col1..Col5 to test for 0 and make the background blue else leave it white. For this example I know creating 5 conditions is not the big of deal but I am dealing with 60 for my report.
<?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="report4a" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fe23f2e2-ee8e-403b-b870-7785fe8941a6">
<queryString>
<![CDATA[select 1 as col1, 0 as col2, 2 as col3, 0 as col4, 10 as col5 from dual
union
select 4 as col1, 0 as col2, 7 as col3, 1 as col4, 12 as col5 from dual
union
select 5 as col1, 0 as col2, 8 as col3, 0 as col4, 16 as col5 from dual
union
select 0 as col1, 0 as col2, 0 as col3, 1 as col4, 110 as col5 from dual
union
select 7 as col1, 0 as col2, 7 as col3, 0 as col4, 180 as col5 from dual
union
select 0 as col1, 0 as col2, 0 as col3, 1 as col4, 107 as col5 from dual ]]>
</queryString>
<field name="COL1" class="java.math.BigDecimal"/>
<field name="COL2" class="java.math.BigDecimal"/>
<field name="COL3" class="java.math.BigDecimal"/>
<field name="COL4" class="java.math.BigDecimal"/>
<field name="COL5" class="java.math.BigDecimal"/>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="077997f5-c89c-4fdf-b89e-aa43b7cf77e3" x="0" y="0" width="111" height="20"/>
<textElement/>
<text><![CDATA[COL1]]></text>
</staticText>
<staticText>
<reportElement uuid="1421164b-ba30-4104-bf62-1e75dd04f757" x="111" y="0" width="111" height="20"/>
<textElement/>
<text><![CDATA[COL2]]></text>
</staticText>
<staticText>
<reportElement uuid="03802088-f5b9-4412-aae7-64ec6c54c4c3" x="222" y="0" width="111" height="20"/>
<textElement/>
<text><![CDATA[COL3]]></text>
</staticText>
<staticText>
<reportElement uuid="b44d853c-0ea5-49ea-8420-e4c40d65c3c3" x="333" y="0" width="111" height="20"/>
<textElement/>
<text><![CDATA[COL4]]></text>
</staticText>
<staticText>
<reportElement uuid="25534117-80fa-43ee-b90c-f150d4b8e0e0" x="444" y="0" width="111" height="20"/>
<textElement/>
<text><![CDATA[COL5]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="8cffe1bc-e06b-48ec-96fc-a3464687a883" x="0" y="0" width="111" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{COL1}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="03ec79ef-9614-4929-b464-450b9b9e8a80" x="111" y="0" width="111" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{COL2}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="79a5d64c-18b4-4a58-89c7-d72de8072759" x="222" y="0" width="111" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{COL3}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="6a5606ff-0f1a-4dec-a494-90c493c794a0" x="333" y="0" width="111" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{COL4}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="c2c5f7b1-4d70-49a8-8745-f9337d07457b" x="444" y="0" width="111" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{COL5}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Yes, you can do it in this manner.
Step 1
For example we have a template:
<?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="styles_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fafb8cb9-d85f-40ad-a31b-0aabd9f6ba93">
<queryString>
<![CDATA[SELECT id, name, cost FROM product]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="NAME" class="java.lang.String"/>
<field name="COST" class="java.math.BigDecimal"/>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement uuid="4c746058-73f8-44b1-b8a4-b503291d4c23" x="54" y="22" width="446" height="35"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Styles sample.
Without using the default style]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20">
<staticText>
<reportElement uuid="f91514a2-601f-4045-a294-68be1929c712" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement uuid="1efff9bb-0a2a-438d-9e7b-3d29f1d4c537" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement uuid="c061bcd5-9a9d-4ad6-89f4-a99103bdb742" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Cost]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="848d3bcf-4c06-4a8a-b880-e5cdae5749e2" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="5557f5a7-4db6-4b58-a99f-b0ccf88c69c5" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="b77c6785-28d4-4395-b99c-2dbc336d00b3" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The result will be (via preview in iReport):
Step 2
Now we are decide to add the default style (defStyle). For example we are changing the forecolor of all elements to blue color.
Our template in this case is:
<?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="styles_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fafb8cb9-d85f-40ad-a31b-0aabd9f6ba93">
<style name="defStyle" isDefault="true" forecolor="#3333FF"/>
<queryString>
<![CDATA[SELECT id, name, cost FROM product]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="NAME" class="java.lang.String"/>
<field name="COST" class="java.math.BigDecimal"/>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement uuid="4c746058-73f8-44b1-b8a4-b503291d4c23" x="54" y="22" width="446" height="35"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Styles sample.
Using the default style]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20">
<staticText>
<reportElement uuid="f91514a2-601f-4045-a294-68be1929c712" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement uuid="1efff9bb-0a2a-438d-9e7b-3d29f1d4c537" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement uuid="c061bcd5-9a9d-4ad6-89f4-a99103bdb742" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Cost]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="848d3bcf-4c06-4a8a-b880-e5cdae5749e2" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="5557f5a7-4db6-4b58-a99f-b0ccf88c69c5" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="b77c6785-28d4-4395-b99c-2dbc336d00b3" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
And the result will be (via preview in iReport):
The color is changed. Notes that we did not apply this style to any element (in the whole report's template).
Step 3
And now we want to add another conditional style (pink). We want to change the forecolor of the textField with cost value if the cost is bigger than 25 - we make it pink.
Our template in this case is:
<?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="styles_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fafb8cb9-d85f-40ad-a31b-0aabd9f6ba93">
<style name="defStyle" isDefault="true" forecolor="#3333FF"/>
<style name="pink" style="defStyle">
<conditionalStyle>
<conditionExpression><![CDATA[$F{COST} > 25]]></conditionExpression>
<style forecolor="#CC00CC"/>
</conditionalStyle>
</style>
<queryString>
<![CDATA[SELECT id, name, cost FROM product ORDER BY cost DESC]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="NAME" class="java.lang.String"/>
<field name="COST" class="java.math.BigDecimal"/>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement uuid="4c746058-73f8-44b1-b8a4-b503291d4c23" x="8" y="22" width="539" height="35"/>
<textElement textAlignment="Center" verticalAlignment="Middle" markup="html"/>
<text><![CDATA[Styles sample.
Using the default style and the another conditional style which applied to the <i>textField</i> with <b>cost</b> field]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20">
<staticText>
<reportElement uuid="f91514a2-601f-4045-a294-68be1929c712" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement uuid="1efff9bb-0a2a-438d-9e7b-3d29f1d4c537" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement uuid="c061bcd5-9a9d-4ad6-89f4-a99103bdb742" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Cost]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="848d3bcf-4c06-4a8a-b880-e5cdae5749e2" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="5557f5a7-4db6-4b58-a99f-b0ccf88c69c5" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="b77c6785-28d4-4395-b99c-2dbc336d00b3" style="pink" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
We added the new style pink which is overrides the defStyle and we applied it to the textFiled in third column cost. We did not apply any other style to any other elements.
The result will be (via preview in iReport):
Note:
We can override the behavior of all styles with help of net.sf.jasperreports.style.{style_property_suffix} parameters.
More info about Conditional Styles
Don't forget about the net.sf.jasperreports.style.evaluation.time.enabled property.
Related
I'm trying to display several tables in one Jasper Report and I use sub reports for this feature. But I'm stuck with displaying several sub reports. Actually, only one of them (the first) is displayed.
Here is my main report.jrxml:
<?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="multitable" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1c6dc1c9-67af-43d5-bf1e-9243557c8048">
<property name="net.sf.jasperreports.json.source" value="C:/jdd/projects/StackTrace/jasper/multitable.json"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["reports/hardware/"]]></defaultValueExpression></parameter>
<parameter name="title" class="java.lang.String"/>
<parameter name="period" class="java.lang.String"/>
<parameter name="DS1" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
<parameter name="DS2" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<detail>
<band height="120" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="50" uuid="91249d6e-57f7-4b6d-8d2a-05059efa2a47"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="652" y="0" width="100" height="50" uuid="67e1ca07-588a-4b2b-ae93-ecbdb544b01b"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA[$P{period}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="0" y="50" width="555" height="20" uuid="48a7bbe4-b8ce-4a0d-a6e1-6ddd288e5602"/>
<dataSourceExpression><![CDATA[$P{DS1}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "printersSubreport.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
<summary>
<band height="305" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="50" uuid="91249d6e-57f7-4b6d-8d2a-05059efa2a47"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="0" y="50" width="555" height="32" uuid="780a1f25-065e-494a-ba1d-86a33b464343"/>
<dataSourceExpression><![CDATA[$P{DS2}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subjectSubreport.jasper"]]></subreportExpression>
</subreport>
</band>
</summary>
</jasperReport>
Here is subjectSubreport.jrxml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<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="multitable_summary" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="2a869c20-1e7d-4baa-92d7-c8d555f1f344">
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="subject" class="java.lang.String"/>
<field name="zipCode" class="java.lang.Integer"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="23">
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="a9730039-8418-4e58-a2eb-0babe8d2a380"/>
<text><![CDATA[Subject]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="a49b18ef-63c3-42bb-ae81-8d7d72ac9086"/>
<box leftPadding="0">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Subject]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="c1c70fed-d887-429f-a3b0-ed645517e41f"/>
<box leftPadding="0">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[ZipCode]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="401436b3-d9a1-40bd-8a1d-dfa3f162f842"/>
<box leftPadding="0">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{subject}]]></textFieldExpression>
</textField>
<textField pattern="###0">
<reportElement x="100" y="0" width="100" height="20" uuid="4447d3b0-6d3e-4795-8b5f-6a09d620448b"/>
<box leftPadding="0">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{zipCode}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
And here is printersSubreport.jrxml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<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="multitable_subreport" pageWidth="555" pageHeight="802" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="5f89d251-66b1-4658-9af5-c057de138fb6">
<field name="statusDesc" class="java.lang.String"/>
<field name="lastUpdateTime" class="java.lang.Integer"/>
<field name="criticalCount" class="java.lang.String"/>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="bbb8857c-471c-4aca-a8fc-336d2748d7b9"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Description]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="3fde89c9-d9a3-4972-8e3b-b2e619a51601"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[UpdateTime]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="200" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="e82ece60-c92e-4d0b-8881-29d7d0d78e81"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Count]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="cdec861a-8b3b-413f-8b99-74d0e18ee19d"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{statusDesc}]]></textFieldExpression>
</textField>
<textField pattern="###0.##">
<reportElement x="100" y="0" width="100" height="20" uuid="9dfb9f78-74e5-4562-84fd-ebb87c3203ce"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{lastUpdateTime}]]></textFieldExpression>
</textField>
<textField pattern="ยค #,##0.00">
<reportElement x="200" y="0" width="100" height="20" uuid="923ff62d-ea1f-410e-94d3-fa7c574c001b"/>
<box>
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{criticalCount}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
When I switch sub reports the first of them displays correctly, and the other one disappears. In JasperSoft Studio everything is fine, the problem starts when I export them to pdf/xls.
Actually, only one of them (the first) is displayed.. When I switch sub reports the first of them displays correctly, and the other one disappears
You are declaring:
<parameter name="DS1" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
<parameter name="DS2" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
The JRDatasource is consumable, which means that if you use it to fill one report it will be consumed hence for next report calling next() on same datasource will return false.
What you are experiencing is what happens if you pass same (same Object) datasource for DS1 and DS2, DS1 will be consumed filling first subreport and then since DS1 == DS2 DS2 will already be consumed when filling second subreport, second report will have no records (empty).
Solution
You need to make a clone DS1, create a new Object and pass that as DS2. The JsonDataSource is theoretically also a JRRewindableDataSource, so you could call moveFirst() before filling second subreport, but this is not very easy from the jrxml file so I don't suggest trying to use this method.
I'm using iReport to create a Jasper Report template, and I need to display one group per page:
Group 1: (Page One)
-Field1 Field2 Field3
Group 2: (Page Two)
-Field1 Field2 Field3
And so on.
I tried setting the Maximize Band Height property to true in the "Group Footer" band using iReport, but some empty pages are added when I try to review.
You can use isStartNewPage attribute of group.
You can add Group Header band and
1) set "Start on a new page" attribute as true
2) set band height as 0 (if you don't need it).
The working sample
The jrxml file:
<?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="group_break" language="groovy" pageWidth="400" pageHeight="400" columnWidth="360" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2bcb6f3d-2169-44f6-9e8a-db54ea21b5e0">
<queryString>
<![CDATA[SELECT DOCUMENTID, PRODUCTID, PRICE FROM POSITIONS ORDER BY DOCUMENTID]]>
</queryString>
<field name="DOCUMENTID" class="java.lang.Integer"/>
<field name="PRODUCTID" class="java.lang.Integer"/>
<field name="PRICE" class="java.math.BigDecimal"/>
<group name="docId" isStartNewPage="true">
<groupExpression><![CDATA[$F{DOCUMENTID}]]></groupExpression>
<groupHeader>
<band/>
</groupHeader>
</group>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="111" height="20" uuid="596f5fb4-7e6d-4e7e-88bf-b9c37d0fb1a1"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center"/>
<text><![CDATA[Position]]></text>
</staticText>
<staticText>
<reportElement x="111" y="0" width="111" height="20" uuid="4884f663-00cf-4b5f-9cc1-05d698b8154a"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center"/>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement x="222" y="0" width="111" height="20" uuid="26c8d18f-2281-405c-a41b-bddcbcdebdbb"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center"/>
<text><![CDATA[Price]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="111" height="20" uuid="fb6369eb-4b92-41dd-b5b8-94a9210bf315"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textFieldExpression><![CDATA[$F{DOCUMENTID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="111" y="0" width="111" height="20" uuid="9fcbb785-06fa-4f7a-bc6d-301cc8db4388"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textFieldExpression><![CDATA[$F{PRODUCTID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="222" y="0" width="111" height="20" uuid="0d18c9fe-5a70-4890-b94d-f26d88bc97da"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textFieldExpression><![CDATA[$F{PRICE}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="50">
<textField>
<reportElement x="0" y="20" width="80" height="20" uuid="47486fe9-e21f-4913-a9c4-cef0d3e2df39"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="80" y="20" width="40" height="20" uuid="0f7df66b-bbe6-4373-962e-519584c44541"/>
<textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
The result
The output result generated in iReport via preview mode
I'm using jasperreport 4.7.0
I have a query where I order by name then date .
Now the clients wants that when the name changes that we add an horizontal line (see attached img - red line)
Is there a way to accomplish this without duplicating the query and the fields ?
Result :
For solving your task you can use Data Grouping.
The possible steps (there are a lot of way to reach your target) for adding line are:
Create the report Group (via context menu Add Report Group in iReport) on field
Add Group Footer Band
Add Rectangle element to the Group Footer Band
Set Height, Forecolor and Backcolor for this Rectangle - drawing "red pencil line"
The sample:
<?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="line_in_group" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f1394ead-7ad6-4371-979d-5a13d1bdde4d">
<queryString>
<![CDATA[SELECT id, city, street FROM address ORDER BY city]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="CITY" class="java.lang.String"/>
<field name="STREET" class="java.lang.String"/>
<group name="cityGroup">
<groupExpression><![CDATA[$F{CITY}]]></groupExpression>
<groupFooter>
<band height="2">
<rectangle>
<reportElement uuid="6564e743-2a45-4b51-89a5-e3ec6aee291f" x="0" y="0" width="300" height="2" forecolor="#FF0000" backcolor="#FF0000"/>
</rectangle>
</band>
</groupFooter>
</group>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="77860b22-95f6-41b6-955a-f8991843e221" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement uuid="77860b22-95f6-41b6-955a-f8991843e221" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[City]]></text>
</staticText>
<staticText>
<reportElement uuid="77860b22-95f6-41b6-955a-f8991843e221" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Street]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="7e375aa3-fab5-4761-bab9-a0570a5442b1" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="7e375aa3-fab5-4761-bab9-a0570a5442b1" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="7e375aa3-fab5-4761-bab9-a0570a5442b1" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{STREET}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The result will be (via preview in iReport):
In this sample I've create a group for the field CITY.
I am trying to create a watermark with a text in a report. For e.g. have a text like this is a good report printed horizontally repeatedly at the background of a report. I know this can be done in the background band but don't know how.
UPDATE
Your solution worked. I want to implement this in another way. I want to print the same text repeatedly at the background. Let's say i want to print the text welcome to a new year. I will want it to print this way
welcome to a new year welcome to a new year welcome to a new year welcome to a new year
welcome to a new year welcome to a new year welcome to a new year welcome to a new year
welcome to a new year welcome to a new year welcome to a new year welcome to a new year
welcome to a new year welcome to a new year welcome to a new year welcome to a new year
welcome to a new year welcome to a new year welcome to a new year welcome to a new year
...
All these will be at the background. Can i achieve something like this without manually creating the text fields to fill the page.
Yes, you are right. You can use Background band for printing watermark text (or picture).
You should set the height for this band - I think it is better to set height of this band the same as the report's height.
For example, for A4 format I have set the 802 pixels for Background's band height. The formula for calculating this number was: the Report's height (842 pixels for A4 format) - the Left Margin's width (20 in my case) - the Right Margin's width (20 in my case).
After setting size for the Background band you can put image or staticText (textField) elements to this (Background) band.
You can rotate the text of staticText (textField) element with help of rotation attribute. The available values of this attribites are:
None (no rotation - 0 degrees),
Left (rotate 270 degrees),
Right (rotate 90 degrees),
UpsideDown (rotate 180 degrees).
In the working sample I've used styles for setting the rotation attribute for textField and for one element I've set value for this attribute directly to the textField element:
<?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="watermark_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bc290dd1-903c-4740-8484-16a2ebe872d2">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="Rotated180" mode="Opaque" forecolor="#FF33CC" hAlign="Center" vAlign="Middle" rotation="UpsideDown" fontName="Curlz MT" fontSize="40"/>
<style name="Rotated270" mode="Opaque" forecolor="#FF33CC" hAlign="Center" vAlign="Middle" rotation="Left" fontName="Curlz MT" fontSize="40"/>
<style name="Rotated90" mode="Opaque" forecolor="#FF33CC" hAlign="Center" vAlign="Middle" rotation="Right" fontName="Curlz MT" fontSize="40"/>
<style name="NotRotated" mode="Opaque" forecolor="#FF33CC" hAlign="Center" vAlign="Middle" rotation="None" fontName="Curlz MT" fontSize="40"/>
<queryString>
<![CDATA[SELECT ID, NAME, COST FROM PRODUCT ORDER BY NAME]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="NAME" class="java.lang.String"/>
<field name="COST" class="java.math.BigDecimal"/>
<background>
<band height="802" splitType="Stretch">
<image hAlign="Center">
<reportElement uuid="42c96db8-4afd-4834-ba69-6b211df0c42f" style="Rotated180" mode="Opaque" x="118" y="372" width="318" height="58"/>
<imageExpression><![CDATA["D:\\java\\jasper_reports\\happy_new_year_h.png"]]></imageExpression>
</image>
<staticText>
<reportElement uuid="d7401e33-f1b3-49ca-85d1-2923d82508f6" style="NotRotated" x="106" y="22" width="342" height="63"/>
<textElement/>
<text><![CDATA[Happy New Year!]]></text>
</staticText>
<staticText>
<reportElement uuid="d7401e33-f1b3-49ca-85d1-2923d82508f6" style="Rotated270" x="24" y="236" width="93" height="330"/>
<textElement/>
<text><![CDATA[Happy New Year!]]></text>
</staticText>
<staticText>
<reportElement uuid="d7401e33-f1b3-49ca-85d1-2923d82508f6" style="Rotated90" x="445" y="236" width="93" height="330"/>
<textElement/>
<text><![CDATA[Happy New Year!]]></text>
</staticText>
<staticText>
<reportElement uuid="d7401e33-f1b3-49ca-85d1-2923d82508f6" style="Rotated180" x="106" y="702" width="342" height="63"/>
<textElement/>
<text><![CDATA[Happy New Year!]]></text>
</staticText>
<staticText>
<reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Opaque" x="79" y="85" width="428" height="139" forecolor="#9999FF"/>
<textElement textAlignment="Center" verticalAlignment="Middle" rotation="Right">
<font fontName="Edwardian Script ITC" size="40" isItalic="true"/>
</textElement>
<text><![CDATA[Happy New Year!]]></text>
</staticText>
</band>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement uuid="6d708618-09e4-433d-ab48-bb9cbd0b832b" x="92" y="29" width="370" height="20"/>
<textElement>
<font size="14" isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Sample of report with watermark]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="0" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="200" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Cost]]></text>
</staticText>
<staticText>
<reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="100" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="0" y="0" width="100" height="20"/>
<box leftPadding="10" rightPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="200" y="0" width="100" height="20"/>
<box leftPadding="10" rightPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The image element does not support the rotation attribute, but you can create any image you want manually.
Note: in this sample I've used mode="Opaque" for textField and image elements.
The result will be (via iReport preview function):
For solving your case (several texts as watermark) I've put several textField elements to the Background band. The report's template is:
<?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="watermark_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bc290dd1-903c-4740-8484-16a2ebe872d2">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="68"/>
<queryString>
<![CDATA[SELECT ID, NAME, COST FROM PRODUCT ORDER BY NAME]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="NAME" class="java.lang.String"/>
<field name="COST" class="java.math.BigDecimal"/>
<background>
<band height="802" splitType="Stretch">
<staticText>
<reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Transparent" x="63" y="112" width="428" height="56" forecolor="#808080"/>
<textElement textAlignment="Center" verticalAlignment="Middle" rotation="None">
<font fontName="Serif" size="40" isItalic="true"/>
</textElement>
<text><![CDATA[this is a good report]]></text>
</staticText>
<staticText>
<reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Transparent" x="63" y="272" width="428" height="56" forecolor="#808080"/>
<textElement textAlignment="Center" verticalAlignment="Middle" rotation="None">
<font fontName="Serif" size="40" isItalic="true"/>
</textElement>
<text><![CDATA[this is a good report]]></text>
</staticText>
<staticText>
<reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Transparent" x="63" y="432" width="428" height="56" forecolor="#808080"/>
<textElement textAlignment="Center" verticalAlignment="Middle" rotation="None">
<font fontName="Serif" size="40" isItalic="true"/>
</textElement>
<text><![CDATA[this is a good report]]></text>
</staticText>
<staticText>
<reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Transparent" x="63" y="594" width="428" height="56" forecolor="#808080"/>
<textElement textAlignment="Center" verticalAlignment="Middle" rotation="None">
<font fontName="Serif" size="40" isItalic="true"/>
</textElement>
<text><![CDATA[this is a good report]]></text>
</staticText>
</band>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement uuid="6d708618-09e4-433d-ab48-bb9cbd0b832b" x="92" y="29" width="370" height="20"/>
<textElement>
<font size="14" isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Sample of report with watermark]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="0" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="200" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Cost]]></text>
</staticText>
<staticText>
<reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="100" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="0" y="0" width="100" height="20"/>
<box leftPadding="10" rightPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="200" y="0" width="100" height="20"/>
<box leftPadding="10" rightPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Note: in this sample I've used mode="Transparent" for textField elements.
The report's design (in iReport):
The result will be (via iReport preview function):
You can find more information in Watermark across the page in JasperReports post if you want to set custom rotation angle.
UPDATE: You can solve your second issue, for example, with help of StringUtils.repeat(java.lang.String, int) method from Commons Lang API.
You can add import instruction (<import value="org.apache.commons.lang3.StringUtils"/> in my sample) to your report's template and use the expression like this:
<textFieldExpression><![CDATA[StringUtils.repeat("Welcome to a New Year! ", $P{numberOfRepeats})]]></textFieldExpression>
The jrxml file:
<?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="watermark_sample" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bc290dd1-903c-4740-8484-16a2ebe872d2">
<import value="org.apache.commons.lang3.StringUtils"/>
<parameter name="numberOfRepeats" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression><![CDATA[10]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT ID, NAME, COST FROM PRODUCT ORDER BY NAME]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="NAME" class="java.lang.String"/>
<field name="COST" class="java.math.BigDecimal"/>
<background>
<band height="802" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement uuid="26290245-c5cf-4f28-b637-c5b0253910a5" mode="Transparent" x="21" y="112" width="470" height="56" forecolor="#808080"/>
<textElement textAlignment="Center" verticalAlignment="Middle" rotation="None" markup="none">
<font fontName="Serif" size="20" isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA[StringUtils.repeat("Welcome to a New Year! ", $P{numberOfRepeats})]]></textFieldExpression>
</textField>
</band>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement uuid="6d708618-09e4-433d-ab48-bb9cbd0b832b" x="92" y="29" width="370" height="20"/>
<textElement>
<font size="14" isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Sample of report with watermark]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="0" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="200" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Cost]]></text>
</staticText>
<staticText>
<reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="100" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
<box leftPadding="10" rightPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="0" y="0" width="100" height="20"/>
<box leftPadding="10" rightPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="200" y="0" width="100" height="20"/>
<box leftPadding="10" rightPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The result will be (via iReport preview function):
Details:
In this sample I've used the Commons Lang library v. 3.1. You should add this library to the application's classpath (in my case it was iReport's cp).
I've used the parameter numberOfRepeats (java.lang.Integer) for setting the number of phrases to generate.
Hello everyone,
I am using static text element in my column band.In that element i have aligned my text vertically in center of the element.I need the same alignment in the excel file when i am exporting the jrxml file.But i am getting the default horizontal alignment ..Please provide me with the solution
You can try this sample:
<?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="test_excel_columns" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[SELECT ID, FIRSTNAME, LASTNAME, STREET, CITY FROM ADDRESS]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="FIRSTNAME" class="java.lang.String"/>
<field name="LASTNAME" class="java.lang.String"/>
<field name="STREET" class="java.lang.String"/>
<field name="CITY" class="java.lang.String"/>
<columnHeader>
<band height="35" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="75" height="35"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement x="75" y="0" width="271" height="35"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="346" y="0" width="100" height="35"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Street]]></text>
</staticText>
<staticText>
<reportElement x="446" y="0" width="100" height="35"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[City]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="75" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="75" y="0" width="271" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{FIRSTNAME} + " " + $F{LASTNAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="346" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{STREET}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="446" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The output in Excel preview is:
My iReport version is 4.1.3.