How to make JasperReports groups start on new page only when needed? - jasper-reports

I'm using JasperReports and iReport (output to PDF), and I have a problem with a certain report:
Inside the report I have defined a group. On average, each "instance" of that group only contains a small number of detail rows, so I don't want each of them to start on a new page, since it would be a waste of paper (the output is printed on paper).
But on the other hand, I don't want to have a group that starts on one page and ends on the next one. For example, if on page 1 I can fit the first two groups and half of the third group, I want the entire third group to start on page 2, and so on.
Is there any way to do that?

The "group" property keepTogether and isStartNewPage will solve your problem and makes group result together and always starts new group from new page.
<group name="CITY" isStartNewPage="true" keepTogether="true">
<groupExpression><![CDATA[$F{CITY}]]></groupExpression>
<groupHeader>
...
</groupHeader>
</group>

The <group> property keepTogether does exactly what you want! It was added in around Jasper 3.6.2, see this discussion.
So:
<group name="CITY" keepTogether="true">
<groupExpression><![CDATA[$F{CITY}]]></groupExpression>
<groupHeader>
...
</groupHeader>
</group>

Right Click on the detail band and Add another Detail Band. You can group your page using these detail band blocks.
Add sufficient height to each band so that it will not overlap on the new page

For displaying group data in new page in Table component we need to set the following properties.
Right on the group name --> Advanced properties --> select new page as true.
observe the output , the group data is displayed in new page.
Regards,
Harish

View > Report Groups > Modify > Min Heiht to Start New Page = 100

Related

jasper report detect if datail band or subreport is present in current page

I'm developing a report with jasper.
The main report has several subreports.
Only one of these subreport has a fixed text with a note number, something like this.
my text<sup>(2)</sup>
In main report I must print the footnote, but only in all the pages in which the text with the note is present.
An image to better explain:
Is there something that permit to detect if a subreport is present in the current page?
Or something that permit to detect if a detail band is present in the current page?
Or something that permit to detect if a particular text will be printed in current page?
Or other strategy to handle this particular situation?
. Create Variable in main report 'subReportCount'
Type = Integer, default 0
Reset Type = 'Page'
. Place this variable (hidden) somewhere after Subreport
. Create variable 'SubVar' in subreport , expression == $V{REPORT_COUNT}
. In main, go to subreport properties -> Edit return Values - >
Set From Varible= SubVar , To Variable=subReportCount, Calculation type = Sum
. Page Footer Print When Expression = $V{subReportCount} > 0
This may work if you are trying if subreport is printed atleast once in page.
Does not matter if you place subreport variable in report or not.
I tried on my local , its working for similar situation
Variable in Main report
<variable name="subReportCount" class="java.lang.Integer" resetType="Page" incrementType="Page" calculation="Sum">
<variableExpression><![CDATA[$V{subReportCount}.equals( null ) ? 0 : $V{subReportCount}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
Return Value for the subreport
<returnValue subreportVariable="SubVar" toVariable="subReportCount"/>
Print when expression for Footer element, in my case I am showing Page number
<printWhenExpression><![CDATA[$V{subReportCount} > 0]]></printWhenExpression>
This should work , if not please share your jrxml and sample data.
Good Luck!

JasperReport Studio / JRXML / JasperReports number format of table element

following problem in JasperReport Studio:
I designed a query, I designed, inserted a table element in the Report but when I preview the data numbers look like this:
3.0083728739827928739279
I would like them to look like this:
3.01
In a element I can just add a pattern. For example:
<textField pattern="#,##0.###">
This (i.e.: jr:table pattern="#,##0.###") does not work with a table element.
So how do I format a table?
Thanks and best regards, Joachim
edit 25/07/2018:
one solution is to add the pattern to each textField -element within the jr:table -element by editing the JRMXL code, still I would like to know how to reach this setting via a GUI (i.e. JasperReport Studio)
Set the Pattern using Java decimal Formate.
Example
<field name="data" class="java.lang.Double"/>
"<textFieldExpression><![CDATA[new DecimalFormat("0.0").format($F{data})]]></textFieldExpression>"
I hope Its properly work for you.

Jasper Report - Current Number of Group in Page

I have a PDF report split in several pages.
In every page I may have one or more groups. What I need is a count of current group number inside that page resetting at page change.
So the first group in page has 1, the next one 2 etc...
When I change a Page, the count has to restart from one.
I have found this good variable definition but it counts current group number for the entire report, ignoring the page.
<variable name="currentGroupNumber" class="java.lang.Integer" incrementType="Group" incrementGroup="GroupName">
<variableExpression><![CDATA[($V{GroupName_COUNT} == 1) ? $V{currentGroupNumber}+1 : $V{currentGroupNumber}]]></variableExpression>
<initialValueExpression><![CDATA[1]]></initialValueExpression>
</variable>
Any ideas?
The variable is missing resetType="Page"
Also, the variable looks a little more complex than it should, something like the following might do:
<variable name="currentGroupNumber" class="java.lang.Integer" calculation="Count" incrementType="Group" incrementGroup="GroupName" resetType="Page">
<variableExpression><![CDATA["foo"]]></variableExpression>
</variable>

JasperReport table column sum for page

I need TO add a column sum to column footer of table object for each page.
If I create variable and set sum aggregation function than sum value appear for full report. But I need sum column data for each page, not for full report.
Can anyone help me solve this problem?
This is not a solution for the jr:table component since it uses a subdataset where its not allowed to set resetType="Page", but using a subreport it can be used.
I don't think there is a work around in components (jr:table,jr:list) using a subdataset but would be happy If someone can prove me wrong
Solution using subreport
On your variable set resetType="Page"
es.
<variable name="var" class="java.lang.Double" resetType="Page" calculation="Sum"/>
....
</variable>
For more info on resetType see JRVariable API
On your textField set evaluationTime="Page"
es.
<textField evaluationTime="Page">
....
</textField>
For more info on evaluationTime see JRTextField API
In case of Dataset set resetType="Report"

Jasper subreport not showing

I am using Eclipse JasperReports Studio and have run into a problem I can't resolve. I have a pretty basic report with detail fields which return about 30 rows. I have a subreport I want to incorporated into the main report but when I add it nothing displays. I have tried adding it to the Column Footer, in the Page Footer with my Page x of x fields, and in the detail field. Which didn't work so well since it added a blank spot between every row retrieved. I have other reports with sub-reports in various bands and they all work fine. Here is the subreport code for the col footer:
<columnFooter>
<band height="110">
<subreport>
<reportElement isPrintRepeatedValues="false" x="0" y="10" width="555" height="100" uuid="34205e71-ec6b-422c-aa27-057678430999"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["The_Report.jasper"]]></subreportExpression>
</subreport>
</band>
</columnFooter>
Any obvious reasons why this won't show up? I don't get any errors and the main report runs fine. The subreport works fine on it's own and it is in the same directory as the main report. I compiled it there and the .jasper file is there as well.
I came across the same problem when using subreports.
In the editor: deleting the content form connectionExpression
And putting $P{REPORT_PARAMETERS_MAP} in the Parameters Map Expression did the trick for me.
Source code: <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
Verify that the Subreport Expression is pointing to the correct/expected subreport. There are times (often due to copying and/or hand-editing existing JRXML files) where the Subreport Expression is still pointing to the subreport Jasper file of the original, Main report. I just ran into this case, myself.
Make sure that
<subreportExpression><![CDATA["The_Report.jasper"]]></subreportExpression>
isn't supposed to be something like
<subreportExpression><![CDATA["The_New_SubReport.jasper"]]></subreportExpression>
or contain the Subreport directory like
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "The_New_SubReport.jasper"]]></subreportExpression>
You should also verify that your subreport doesn't require data in order to display. If it does and your query doesn't return any data, you can do the following:
SELECT FIELD_NAME
FROM TABLE
WHERE KEY = $P{KEY}
UNION ALL
SELECT NULL AS FIELD_NAME
FROM DUAL WHERE (
SELECT COUNT(*)
FROM TABLE
WHERE KEY = $P{KEY}
) = 0
A quick reminder, after double checking that you have the correct values for all the <subreportExpression> elements. Make sure you Build All the project files.
The main report will call the compiled version of the subreport. So once you have tested and saved your subreport, click the Build All button or click on the Menu option under the 'Project' menu at the top of the window.