1
votes

I have a customer request to split details of personal information (name, address, secondary addresses, etc) across multiple columns, if the dynamic size does not fit in the current column. As a caveat, the name field, followed by the word '(Continued)', is desired, as the title for the segments in subsequent columns.

Is this behavior possible with jasper-reports?

I have tried toggling an 'inBetween' flag with the beforeDetailEval() and afterDetailEval() hooks, checking that flag in the column header band, to optionally reprint the name + 'Continued', but the column header always calls the scriptlet method AFTER the afterDetailEval() method (does not make sense to my little mind), failing since the 'inBetween' flag is reset to false.

(Note: Detail band has been set with splitType to both 'Immediate' and 'Stretch', but neither approach has behaved as expected. The entire band just gets restarted at the beginning of the next column)

Is this possible, and if so, are there any suggestions for creative solutions for this scenario?

Also, there is strange behavior displaying where if a detail is split across multiple columns, the first part of the detail (up to the subreport element)is duplicated on the following column. This behavior is also undesirable to the customer, and needs to be adjusted so nothing (except the title) is duplicated.

This is the syntax desired by the customer:

Name 1                              Name 2
Address 1                           (Continued)
City 1                              AL – Address 3
State 1                             AL - City 3
Zip 1                               AL - State 3
Additional Locations:               AL - Zip 3
AL-Address 1                        ...
AL-City 1
AL-State 1
AL-Zip 1
AL-Address 2
AL-City 2
AL-State 2
AL-Zip 2

Name 2
Address 2
City 2
State 2
Zip 2
Additional Locations:
AL-Address 1
AL – City 1
AL-State 1
AL – Zip 1
AL – Address 2
AL – City 2
AL – State 2
AL – Zip 2

This is what we are actually getting:

Name1                    Name1
Address1                 Address1
City1                    City1
State1                   State1
Zip1                     Zip1
Additional Locations     Additional Locations
AL-Name1                 AL-City2
AL-Address1              AL-State2
AL-City1                 AL-State2
AL-State1
AL-Zip1
AL-Name2
AL-Address2

This is a sample main jrxml file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-06-22T09:48:08 -->
<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="Blank_A4" columnCount="2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" scriptletClass="path.to.my.scriptlet" uuid="af53d807-7975-4ff7-bfc5-e438944aa795">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Name" class="java.lang.String"/>
    <field name="Address" class="java.lang.String"/>
    <field name="City" class="java.lang.String"/>
    <field name="State" class="java.lang.String"/>
    <field name="Zip" class="java.lang.String"/>
    <field name="AdditionalLocations" class="java.util.List"/>
    <variable name="IN_MIDDLE_OF_DETAIL" class="java.lang.Boolean" calculation="System">
        <initialValueExpression><![CDATA[Boolean.False]]></initialValueExpression>
    </variable>
    <columnHeader>
        <band height="74" splitType="Stretch">
            <textField isBlankWhenNull="true">
                <reportElement x="-10" y="0" width="100" height="30" uuid="258dce49-fb61-4887-bb30-69e80e96d8f1">
                    <printWhenExpression><![CDATA[$V{IN_MIDDLE_OF_DETAIL}]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="-10" y="40" width="100" height="30" uuid="c18b2fab-900b-4641-aaef-2520510a3510">
                    <printWhenExpression><![CDATA[$V{IN_MIDDLE_OF_DETAIL}]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA["(CONTINUED)"]]></textFieldExpression>
            </textField>
        </band>
    </columnHeader>
    <detail>
        <band height="426" splitType="Stretch">
            <textField>
                <reportElement x="0" y="10" width="100" height="30" uuid="853a1b0d-a49e-45c4-8183-83cfd69bf5af"/>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="50" width="100" height="30" uuid="46697c6d-1c2f-422b-9170-6b2f36ce13ba"/>
                <textFieldExpression><![CDATA[$F{Address}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="90" width="100" height="30" uuid="31730cd5-0b17-452e-8a22-d16ea2061605"/>
                <textFieldExpression><![CDATA[$F{City}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="120" width="100" height="30" uuid="b4a4c671-dc63-41b4-b080-9ad5b750bb58"/>
                <textFieldExpression><![CDATA[$F{State}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="160" width="100" height="30" uuid="8f8bec7c-3d4a-466b-8b02-2bb82e61904d"/>
                <textFieldExpression><![CDATA[$F{Zip}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="0" y="220" width="100" height="110" uuid="8ef583fd-fa23-47d8-80d0-90de4f6478a0"/>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{AdditionalLocations})]]></dataSourceExpression>
                <subreportExpression><![CDATA["Blank_A4_1.jasper"]]></subreportExpression>
            </subreport>
            <staticText>
                <reportElement x="10" y="198" width="100" height="30" uuid="4887e0c1-3da2-4350-9454-4a3e33c0fe71"/>
                <text><![CDATA[Additional Locations]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>

This is the sample subreport jrxml file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-06-22T10:39:43 -->
<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="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9b8a1d93-2ee6-4d29-b9ec-7c56a3917729">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Address" class="java.lang.String"/>
    <field name="City" class="java.lang.String"/>
    <field name="State" class="java.lang.String"/>
    <field name="Zip" class="java.lang.String"/>
    <detail>
        <band height="168" splitType="Stretch">
            <textField isBlankWhenNull="true">
                <reportElement x="0" y="10" width="100" height="30" uuid="3d74e624-6b6f-40e9-87d2-ddb5022668b2"/>
                <textFieldExpression><![CDATA[$F{Address}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="50" width="100" height="30" uuid="5df31fa3-38ee-44a4-8931-e8eef45fb7a6"/>
                <textFieldExpression><![CDATA[$F{City}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="89" width="100" height="30" uuid="964a282f-54c7-44a3-954d-27d1c70d3d0c"/>
                <textFieldExpression><![CDATA[$F{State}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="130" width="100" height="30" uuid="e1349222-d945-4dec-8454-e2d6e3fb6a2a"/>
                <textFieldExpression><![CDATA[$F{Zip}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

This is the basic scriptlet code to toggle the value of IN_MIDDLE_OF_DETAIL:

@Override
public void beforeDetailEval() throws JRScriptletException
{
    setVariableValue("IN_MIDDLE_OF_DETAIL", Boolean.TRUE)
}


@Override
public void afterDetailEval() throws JRScriptletException
{
    setVariableValue("IN_MIDDLE_OF_DETAIL", Boolean.FALSE)
}
1
Thanks for the idea. The question is very broad, and as I read over it, will try to fine tune it. The name, address, etc, are all separate fields from the datasource.piper1970
but are you splitting on columns or on pages? I can't see multiple columns in your example?.Petter Friberg
I am splitting on columns (or at least, attempting to). Just realized in the mock jrxml file I didn't set columnCount to 2. Has been adjusted.piper1970
Two works for my problems. Is just a mock page. ACME proprietary rules don't allow me to put actual code uppiper1970
No problem mock up code is also better since it reduce the code size, I have answered, trying to show you how you can do this in jasper-report, actually first I taught all the text was in one textField, sorry for the confusion, but yeah we needed a minimal reproducible examplePetter Friberg

1 Answers

0
votes

Drop the scriptlet, there is no need for this, lets use the fact that subreport's has its own page count, hence when it breaks in main report it counts a page

The solution I would use is the pageHeader band in the subreport with a printWhenExpression on page number (>1).

Which means that the text that you like to display when subreport splits to new "page" (name and "Continue") goes into pageHeader of the subreport and should be printed on all subreport "pages" except the first one.

Since name does not seems to be part of the subreport datasource I will pass this as a parameter to the subreport.

Code in subreport to display header when page > 1

<pageHeader>
    <band height="44">
    <printWhenExpression><![CDATA[$V{PAGE_NUMBER}>1]]></printWhenExpression>
    <textField>
        <reportElement x="0" y="0" width="220" height="20" uuid="8cce4cab-6ccf-4ddb-b1f1-508dc97bfcfe"/>
        <textElement verticalAlignment="Middle">
            <font isBold="true"/>
        </textElement>
        <textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
    </textField>
    <textField>
        <reportElement x="0" y="20" width="220" height="19" uuid="c18b2fab-900b-4641-aaef-2520510a3510"/>
        <textElement verticalAlignment="Middle">
            <font isItalic="true"/>
        </textElement>
        <textFieldExpression><![CDATA["(Continued)"]]></textFieldExpression>
    </textField>
</band>
</pageHeader>

Example output

Example

I have cut down page height to reduce image

Full example

**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="Blank_A4" columnCount="2" pageWidth="595" pageHeight="500" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="af53d807-7975-4ff7-bfc5-e438944aa795">
	<queryString>
		<![CDATA[]]>
	</queryString>
	<field name="name" class="java.lang.String"/>
	<field name="address" class="java.lang.String"/>
	<field name="city" class="java.lang.String"/>
	<field name="state" class="java.lang.String"/>
	<field name="zip" class="java.lang.String"/>
	<field name="additionalLocations" class="java.util.List"/>
	<detail>
		<band height="141" splitType="Immediate">
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="20" width="280" height="20" uuid="46697c6d-1c2f-422b-9170-6b2f36ce13ba">
					<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
				</reportElement>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="40" width="280" height="20" uuid="31730cd5-0b17-452e-8a22-d16ea2061605"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="60" width="280" height="20" uuid="b4a4c671-dc63-41b4-b080-9ad5b750bb58"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{state}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="80" width="280" height="20" uuid="8f8bec7c-3d4a-466b-8b02-2bb82e61904d"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{zip}]]></textFieldExpression>
			</textField>
			<subreport>
				<reportElement x="0" y="120" width="280" height="20" isPrintWhenDetailOverflows="true" uuid="8ef583fd-fa23-47d8-80d0-90de4f6478a0"/>
				<subreportParameter name="name">
					<subreportParameterExpression><![CDATA[$F{name}]]></subreportParameterExpression>
				</subreportParameter>
				<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{additionalLocations})]]></dataSourceExpression>
				<subreportExpression><![CDATA["C:/Users/pette/JaspersoftWorkspace/MyReports/SplitSubreport.jasper"]]></subreportExpression>
			</subreport>
			<staticText>
				<reportElement x="0" y="100" width="280" height="20" uuid="4887e0c1-3da2-4350-9454-4a3e33c0fe71"/>
				<textElement verticalAlignment="Middle">
					<font isItalic="false" isUnderline="true"/>
				</textElement>
				<text><![CDATA[Additional Locations]]></text>
			</staticText>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement x="0" y="0" width="280" height="20" uuid="258dce49-fb61-4887-bb30-69e80e96d8f1"/>
				<textElement verticalAlignment="Middle">
					<font isBold="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>

**subreport 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="Blank_A4_1" pageWidth="280" pageHeight="200" columnWidth="280" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="9b8a1d93-2ee6-4d29-b9ec-7c56a3917729">
	<parameter name="name" class="java.lang.String"/>
	<queryString>
		<![CDATA[]]>
	</queryString>
	<field name="address" class="java.lang.String"/>
	<field name="city" class="java.lang.String"/>
	<field name="state" class="java.lang.String"/>
	<field name="zip" class="java.lang.String"/>
	<pageHeader>
		<band height="44">
			<printWhenExpression><![CDATA[$V{PAGE_NUMBER}>1]]></printWhenExpression>
			<textField>
				<reportElement x="0" y="20" width="220" height="19" uuid="c18b2fab-900b-4641-aaef-2520510a3510"/>
				<textElement verticalAlignment="Middle">
					<font isItalic="true"/>
				</textElement>
				<textFieldExpression><![CDATA["(Continued)"]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="0" y="0" width="220" height="20" uuid="8cce4cab-6ccf-4ddb-b1f1-508dc97bfcfe"/>
				<textElement verticalAlignment="Middle">
					<font isBold="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
			</textField>
		</band>
	</pageHeader>
	<detail>
		<band height="85" splitType="Stretch">
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement x="0" y="-1" width="220" height="21" uuid="3d74e624-6b6f-40e9-87d2-ddb5022668b2"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="20" width="220" height="20" uuid="5df31fa3-38ee-44a4-8931-e8eef45fb7a6"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="40" width="220" height="20" uuid="964a282f-54c7-44a3-954d-27d1c70d3d0c"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{state}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="60" width="220" height="20" uuid="e1349222-d945-4dec-8454-e2d6e3fb6a2a"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{zip}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>