jasper-reports

How to add table of content in Jasper report


I am using JasperSoft Studio. I am generating a report which having Chapter and sub-chapter also. but all these Chapter and Sub-Chapter configured in Sub-report.

How I can get all these Chapter and Sub-chapter name with link, page number in a table of content formatted in one page like a standard table of content of a document?

EDIT: Moved users answer to question (it should be an edit of the question)

Bookmarks are coming in jasperserver (in separate tab ) but not coming in report. In TOC all the field evaluationTime=report. Here is my TOC jrxml code .

<detail>
    <band height="25" splitType="Stretch">
        <property name="local_mesure_unitheight" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.height" value="px"/>
        <printWhenExpression><![CDATA[$F{level} == 1]]></printWhenExpression>
        <textField evaluationTime="Report" hyperlinkType="LocalAnchor">
            <reportElement style="HeadingLabel1" x="0" y="5" width="470" height="20" uuid="379fcff2-021d-4761-a89a-eeb772723b1b">
                <property name="local_mesure_unitheight" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                <property name="local_mesure_unity" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.y" value="px"/>
            </reportElement>
            <textFieldExpression><![CDATA[$F{label}]]></textFieldExpression>
            <hyperlinkAnchorExpression><![CDATA[$F{label}]]></hyperlinkAnchorExpression>
        </textField>
        <textField evaluationTime="Auto" hyperlinkType="LocalAnchor">
            <reportElement style="HeadingIndex1" x="470" y="5" width="45" height="20" uuid="a31e80f3-f1fa-4643-adbe-77a61c7b9051">
                <property name="local_mesure_unitheight" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                <property name="com.jaspersoft.studio.unit.y" value="px"/>
            </reportElement>
            <textElement textAlignment="Right" verticalAlignment="Bottom"/>
            <textFieldExpression><![CDATA[$V{PAGE_NUMBER} + $F{pageIndex} + 1]]></textFieldExpression>
            <hyperlinkAnchorExpression><![CDATA[$F{label}]]></hyperlinkAnchorExpression>
        </textField>
    </band>
    <band height="25" splitType="Stretch">
        <property name="local_mesure_unitheight" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.height" value="px"/>
        <printWhenExpression><![CDATA[$F{level} == 2]]></printWhenExpression>
        <textField evaluationTime="Report" hyperlinkType="LocalAnchor">
            <reportElement style="HeadingLabel2" x="40" y="5" width="430" height="20" uuid="379fcff2-021d-4761-a89a-eeb772723b1b">
                <property name="local_mesure_unitheight" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                <property name="com.jaspersoft.studio.unit.y" value="px"/>
            </reportElement>
            <textFieldExpression><![CDATA[$F{label}]]></textFieldExpression>
            <hyperlinkAnchorExpression><![CDATA[$F{label}]]></hyperlinkAnchorExpression>
        </textField>
        <textField evaluationTime="Auto" hyperlinkType="LocalAnchor">
            <reportElement style="HeadingIndex2" x="470" y="5" width="45" height="20" uuid="a31e80f3-f1fa-4643-adbe-77a61c7b9051">
                <property name="local_mesure_unitheight" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                <property name="com.jaspersoft.studio.unit.y" value="px"/>
            </reportElement>
            <textElement textAlignment="Right" verticalAlignment="Bottom"/>
            <textFieldExpression><![CDATA[$V{PAGE_NUMBER} + $F{pageIndex} + 1]]></textFieldExpression>
            <hyperlinkAnchorExpression><![CDATA[$F{label}]]></hyperlinkAnchorExpression>
        </textField>
        <textField evaluationTime="Report" hyperlinkType="LocalAnchor">
            <reportElement style="HeadingLabel2" x="20" y="5" width="20" height="20" uuid="ae12cafd-22e8-45ae-a1a7-8e00a8208cf0">
                <property name="local_mesure_unitheight" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                <property name="local_mesure_unity" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.y" value="px"/>
            </reportElement>
            <textFieldExpression><![CDATA[$V{level2Index} + "."]]></textFieldExpression>
            <hyperlinkAnchorExpression><![CDATA[$F{label}]]></hyperlinkAnchorExpression>
        </textField>
    </band>
    <band height="15" splitType="Stretch">
        <property name="local_mesure_unitheight" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.height" value="px"/>
        <printWhenExpression><![CDATA[$F{level} == 3]]></printWhenExpression>
        <textField evaluationTime="Report" hyperlinkType="LocalAnchor">
            <reportElement style="HeadingLabel3" x="40" y="0" width="430" height="15" uuid="379fcff2-021d-4761-a89a-eeb772723b1b">
                <property name="local_mesure_unitheight" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
            </reportElement>
            <textFieldExpression><![CDATA[$F{label}]]></textFieldExpression>
            <hyperlinkAnchorExpression><![CDATA[$F{label}]]></hyperlinkAnchorExpression>
        </textField>
        <textField evaluationTime="Auto" hyperlinkType="LocalAnchor">
            <reportElement style="HeadingIndex3" x="470" y="0" width="45" height="15" uuid="a31e80f3-f1fa-4643-adbe-77a61c7b9051">
                <property name="local_mesure_unitheight" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
            </reportElement>
            <textElement textAlignment="Right" verticalAlignment="Bottom"/>
            <textFieldExpression><![CDATA[$V{PAGE_NUMBER} + $F{pageIndex} + 1]]></textFieldExpression>
            <hyperlinkAnchorExpression><![CDATA[$F{label}]]></hyperlinkAnchorExpression>
        </textField>
    </band>
</detail>

EDIT: here is my main report jrxml.

<property name="com.jaspersoft.studio.data.defaultdataadapter" value="TCD"/>
<property name="net.sf.jasperreports.print.create.bookmarks" value="true"/>
<property name="com.jaspersoft.studio.book.group.cover.header" value="Cover and Table of Contents"/>
<property name="com.jaspersoft.studio.book.group.cover.footer" value="Backcover"/>
<property name="ireport.jasperserver.url" value="http://localhost:8080/jasperserver/"/>
<property name="ireport.jasperserver.user" value="jasperadmin"/>
<property name="ireport.jasperserver.report.resource" value="/reports/TCD_book_page_files/main_jrxml"/>
<property name="ireport.jasperserver.reportUnit" value="/reports/TCD_book_page"/>
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<queryString>
    <![CDATA[SELECT 1 NO FROM DUAL]]>
</queryString>
<field name="NO" class="java.lang.Integer"/>
<group name="cover">
    <groupHeader>
        <part evaluationTime="Report" uuid="56ab525c-754f-4f48-a52c-7cc23934be3d">
            <property name="net.sf.jasperreports.bookmarks.data.source.parameter" value="REPORT_DATA_SOURCE"/>
            <p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
                <subreportExpression><![CDATA["TCD_book_page_toc.jasper"]]></subreportExpression>
            </p:subreportPart>
        </part>
    </groupHeader>
</group>
<detail>
    <part uuid="5196d1c8-d060-450f-b9f5-9089918b7389">
        <p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
            <subreportParameter name="REPORT_CONNECTION">
                <subreportParameterExpression><![CDATA[$P{REPORT_CONNECTION}]]></subreportParameterExpression>
            </subreportParameter>
            <subreportExpression><![CDATA["TCD_Page.jrxml"]]></subreportExpression>
        </p:subreportPart>
    </part>
</detail>


Solution

  • The https://jasperreports.sourceforge.net/sample.reference/tableofcontents/README.html does work even if you have the bookmarks in your subreport's. (in example they are in subreport).

    Some important notice:

    The main report should have the net.sf.jasperreports.print.create.bookmarks set in order to collect bookmarks at report generation time.

    The Table of Contents part needs to be evaluated after other report parts; if the part is to be printed at the beginning of the document it should have Report evaluation (and no subsequent parts should have the same evaluation).

    If you do not want to use this feature of jasper report, you need to generate your own scriplet to collect data see for example:

    how-can-i-build-a-table-of-contents-in-ireport-jasperreports