Search code examples
jasper-reports

JasperReports - Header beside the data


I need to generate a report where the left half of the page contains the header data (twice) and the right half of the page contains the detail records.

Something like this

I read that the bands always span the full width of the page so clearly I cannot resize the header band to only half the width of the page.

Setting the report as 2-columns does not seem to help as it only allows me to split the detail records in columns.

I also need the header displayed twice but that may be the subject of another question.

Complete JasperReport newbie using JasperSoft Studio for the first time.


Solution

  • Try this code:

    Main 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="report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4ddfa540-9b62-4339-9334-18733f7469cc">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["/home/jft/code/jasper-report/Genratedreports/"]]></defaultValueExpression>
    </parameter>
    <parameter name="dataForSubreport1" class="java.util.List"/>
    <parameter name="dataForSubreport2" class="java.util.List"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="100">
            <subreport>
                <reportElement uuid="8798ed7b-f389-4037-9381-3862d2f3e43a" x="167" y="20" width="388" height="40"/>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dataForSubreport1})]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub1.jasper"]]></subreportExpression>
            </subreport>
            <subreport>
                <reportElement uuid="f99fdb31-afc3-4127-8148-8e0426cd71a6" positionType="Float" x="167" y="60" width="388" height="40"/>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dataForSubreport2})]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub1.jasper"]]></subreportExpression>
            </subreport>
            <staticText>
                <reportElement uuid="5ff63fe1-a2f6-472a-9d93-47cbcefba84d" x="0" y="20" width="167" height="40"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Header]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="5ff63fe1-a2f6-472a-9d93-47cbcefba84d" positionType="Float" x="0" y="60" width="182" height="40"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Header]]></text>
            </staticText>
        </band>
    </title>
    </jasperReport>
    

    SubReport

    <?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="sub1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="48bc0742-6980-42a7-9a52-6f125fb83bc2">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <field name="name" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="ac3c1a86-6d0d-4b63-b339-744528573666"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    </jasperReport>
    

    You need to make create main report and then its subreport. DOnt forget to touch second static field over subreport, otherwise it will not float down.

    Enjoy.