0

I am using jasper report version 6.1 and wanted page break after pre-defined number of rows in the jr:table component. I have also tried doing the same in version 5 and it works fine but in version 6+ I am not able to do it.

Sharing the links which I followed if any one have other solution please let me know.

http://community.jaspersoft.com/wiki/ireport-how-introduce-page-break-after-pre-defined-number-rows-table-component

http://community.jaspersoft.com/questions/845487/inserting-break-table-print-only-10-rows-page-jasper-reports-372

This is what I have tried:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0  -->
<!-- 2017-05-29T15:29:33 -->
<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_4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="957c1fea-8994-4195-9d7a-2bbf0baf7180">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Max apex"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Dataset1" uuid="435d51b0-6d3e-442e-9a96-ae7412b12278">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Max apex"/>
        <queryString>
            <![CDATA[select * from dept]]>
        </queryString>
        <field name="DEPTNO" class="java.math.BigDecimal"/>
        <field name="DNAME" class="java.lang.String"/>
        <field name="LOC" class="java.lang.String"/>
        <group name="DEPTNO">
            <groupExpression><![CDATA[$F{DEPTNO}]]></groupExpression>
        </group>
        <group name="DNAME">
            <groupExpression><![CDATA[$F{DNAME}]]></groupExpression>
        </group>
        <group name="LOC">
            <groupExpression><![CDATA[$F{LOC}]]></groupExpression>
        </group>
    </subDataset>
    <queryString>
        <![CDATA[select * from dept]]>
    </queryString>
    <field name="DEPTNO" class="java.math.BigDecimal"/>
    <field name="DNAME" class="java.lang.String"/>
    <field name="LOC" class="java.lang.String"/>
    <group name="DEPTNO">
        <groupExpression><![CDATA[$F{DEPTNO}]]></groupExpression>
    </group>
    <group name="DNAME">
        <groupExpression><![CDATA[$F{DNAME}]]></groupExpression>
    </group>
    <group name="LOC">
        <groupExpression><![CDATA[$F{LOC}]]></groupExpression>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="145" splitType="Stretch">
            <componentElement>
                <reportElement positionType="FixRelativeToBottom" x="10" y="0" width="500" height="110" uuid="b37c89ae-600a-4fb8-ac64-e142bfe22e13"/>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="Dataset1" uuid="8f84378b-9508-4763-8265-69e68c41febe">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="00a00921-f5fa-4352-924c-f0531c919f71">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="d88580f8-05d2-4eb0-a098-00e877c58c39"/>
                                <text><![CDATA[DEPTNO]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="30" uuid="15fda94a-b686-4808-b55c-3e08a8e86187"/>
                                <textFieldExpression><![CDATA[$F{DEPTNO}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="140" uuid="72e58aec-00f1-4937-8600-b32a6010d4d6">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="130" height="30" uuid="942c89ca-e914-4809-bb18-3b7065b413f7"/>
                                <text><![CDATA[DNAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="130" height="30" uuid="8b936e63-5f84-4fd7-b21c-8c08fb9550a5"/>
                                <textFieldExpression><![CDATA[$F{DNAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="140" uuid="f312cbac-4554-492c-928f-dd84b5a55d42">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="120" height="30" uuid="ce385917-88aa-4e51-9cae-a7a142f78323"/>
                                <text><![CDATA[LOC]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="120" height="30" uuid="0e43f425-558c-4001-9c44-ca14798bcb75"/>
                                <textFieldExpression><![CDATA[$F{LOC}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Pranav Shah
  • 76
  • 1
  • 8
  • Before giving down voting i think one should think and write in the comment box what needs to be improved don't bother to waste you're time if you are not going to comment its point less. – Pranav Shah May 29 '17 at 06:49
  • I can't see the group expression to page break in your example. – Petter Friberg May 29 '17 at 10:27
  • I'm also confused why 2 queries?, if you have the jr:table in the detail band, the main query maybe should be "SELECT 1 from dual"?, check out this answer to better understand detail band and jr:table component https://stackoverflow.com/a/43889920/5292302 – Petter Friberg May 29 '17 at 10:30
  • Petter great work thanks for making my question more readable :) 1 query for other page items and 1 query for table in my actual scenario i have two different query that is why i have stated 2 query I did not use page break as i dont know where to put it. – Pranav Shah May 29 '17 at 10:41
  • I can't see why [this](http://community.jaspersoft.com/wiki/ireport-how-introduce-page-break-after-pre-defined-number-rows-table-component) is not working?, I will try later tonight, but I'm pretty sure it works. I'm still confused why you need 2 identical queries in your report, you have identical tables? maybe you are having a [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Petter Friberg May 29 '17 at 10:49
  • :) I use to tell people that you might have XY-Problem now it's my turn to hear that.. as i told i did try that and it's not working the same way what it use to in version 5. – Pranav Shah May 29 '17 at 11:02
  • *and you are still sure that you actually need 2 queries?, reflect on it, to me the main query should only return 1 record* I will try the page break tonight in >6 – Petter Friberg May 29 '17 at 13:04
  • No No i dont need two query as i mentioned i am replicating my Original query so in my Original jasper report there are two different query please forget about two query i just need to know is there a way to apply page break – Pranav Shah May 30 '17 at 08:22
  • Have you tried the same !! Any success !! – Pranav Shah Jun 03 '17 at 06:15

1 Answers1

1

The solution to use a a group expression inside the subdatset works perfectly fine also in jasper reports v6+.

Example (built from code and data from this answer)

Adding groupExpression to the subDataset to break to new page every 2 records ($V{REPORT_COUNT}-1)/2)

<subDataset name="tableDataset" uuid="7a53770f-0350-4a73-bfc1-48a5f6386594">
    <field name="User" class="java.lang.String"/>
    <field name="Rep" class="java.math.BigDecimal"/>
    <group name="pageBreakGroup" isStartNewPage="true">
        <groupExpression><![CDATA[(int)(($V{REPORT_COUNT}-1)/2)]]></groupExpression>
    </group>
</subDataset>

Expand code snippet for full code.

<?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="reputation" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a88bd694-4f90-41fc-84d0-002b90b2d73e">
 <property name="com.jaspersoft.studio.data.defaultdataadapter" value="CVS"/>
 <style name="table">
  <box>
   <pen lineWidth="1.0" lineColor="#000000"/>
  </box>
 </style>
 <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
  <box>
   <pen lineWidth="0.5" lineColor="#000000"/>
  </box>
 </style>
 <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
  <box>
   <pen lineWidth="0.5" lineColor="#000000"/>
  </box>
 </style>
 <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
  <box>
   <pen lineWidth="0.5" lineColor="#000000"/>
  </box>
 </style>
 <subDataset name="tableDataset" uuid="7a53770f-0350-4a73-bfc1-48a5f6386594">
  <field name="User" class="java.lang.String"/>
  <field name="Rep" class="java.math.BigDecimal"/>
  <group name="pageBreakGroup" isStartNewPage="true">
   <groupExpression><![CDATA[(int)(($V{REPORT_COUNT}-1)/2d)]]></groupExpression>
  </group>
 </subDataset>
 <parameter name="displayRecordNumber" class="java.lang.Boolean">
  <defaultValueExpression><![CDATA[true]]></defaultValueExpression>
 </parameter>
 <queryString>
  <![CDATA[]]>
 </queryString>
 <title>
  <band height="50">
   <componentElement>
    <reportElement key="table" style="table" x="0" y="0" width="555" height="47" uuid="76ab08c6-e757-4785-a43d-b65ad4ab1dd5"/>
    <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
     <datasetRun subDataset="tableDataset" uuid="07e5f1c2-af7f-4373-b653-c127c47c9fa4">
      <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
     </datasetRun>
     <jr:column width="90" uuid="918270fe-25c8-4a9b-a872-91299cddbc31">
      <printWhenExpression><![CDATA[$P{displayRecordNumber}]]></printWhenExpression>
      <jr:columnHeader style="table_CH" height="30" rowSpan="1">
       <staticText>
        <reportElement x="0" y="0" width="90" height="30" uuid="5cd6da41-01d5-4f74-99c2-06784f891d1e"/>
        <textElement textAlignment="Center" verticalAlignment="Middle"/>
        <text><![CDATA[Record number]]></text>
       </staticText>
      </jr:columnHeader>
      <jr:detailCell style="table_TD" height="30" rowSpan="1">
       <textField>
        <reportElement x="0" y="0" width="90" height="30" uuid="5fe48359-0e7e-44b2-93ac-f55404189832"/>
        <textElement textAlignment="Center" verticalAlignment="Middle"/>
        <textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
       </textField>
      </jr:detailCell>
     </jr:column>
     <jr:column width="90" uuid="7979d8a2-4e3c-42a7-9ff9-86f8e0b164bc">
      <jr:columnHeader style="table_CH" height="30" rowSpan="1">
       <staticText>
        <reportElement x="0" y="0" width="90" height="30" uuid="61d5f1b6-7677-4511-a10c-1fb8a56a4b2a"/>
        <textElement textAlignment="Center" verticalAlignment="Middle"/>
        <text><![CDATA[Username]]></text>
       </staticText>
      </jr:columnHeader>
      <jr:detailCell style="table_TD" height="30" rowSpan="1">
       <textField>
        <reportElement x="0" y="0" width="90" height="30" uuid="a3cdb99d-3bf6-4c66-b50c-259b9aabfaef"/>
        <box leftPadding="3" rightPadding="3"/>
        <textElement verticalAlignment="Middle"/>
        <textFieldExpression><![CDATA[$F{User}]]></textFieldExpression>
       </textField>
      </jr:detailCell>
     </jr:column>
     <jr:column width="90" uuid="625e4e5e-5057-4eab-b4a9-c5b22844d25c">
      <jr:columnHeader style="table_CH" height="30" rowSpan="1">
       <staticText>
        <reportElement x="0" y="0" width="90" height="30" uuid="e1c07cb8-a44c-4a8d-8566-5c86d6671282"/>
        <textElement textAlignment="Center" verticalAlignment="Middle"/>
        <text><![CDATA[Reputation]]></text>
       </staticText>
      </jr:columnHeader>
      <jr:detailCell style="table_TD" height="30" rowSpan="1">
       <textField pattern="#,##0">
        <reportElement x="0" y="0" width="90" height="30" uuid="6be2d79f-be82-4c7b-afd9-0039fb8b3189"/>
        <box leftPadding="3" rightPadding="3"/>
        <textElement textAlignment="Right" verticalAlignment="Middle"/>
        <textFieldExpression><![CDATA[$F{Rep}]]></textFieldExpression>
       </textField>
      </jr:detailCell>
     </jr:column>
    </jr:table>
   </componentElement>
  </band>
 </title>
</jasperReport>

Result in JasperStudio version 6.2

As you can see 3 pages (2 records on each, total record count 5).Result

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109