Avoid split of text field to multiple pages - jasper-reports

I have a main report and in the main report a sub report called this:
<detail>
<band height="50">
<subreport>
<reportElement x="0" y="1" width="802" height="49" isPrintInFirstWholeBand="true" isPrintWhenDetailOverflows="true"/>
<subreportParameter name="project_id">
<subreportParameterExpression><![CDATA[$P{project_id}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["subs/Project_planned.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
There is a problem in the subreport because text fields in the detail band are split up on multiple pages what doesn't look very nice. Is there an option to prevent text fields from splitting up to multiple pages when using the property isStretchWithOverflow
Current code of a text field
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToBandHeight" x="555" y="0" width="99" height="21" isPrintInFirstWholeBand="true" forecolor="#000000"/>
<box leftPadding="2">
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement verticalAlignment="Middle">
<paragraph lineSpacing="Single"/>
</textElement>
<textFieldExpression><![CDATA[$F{logic}]]></textFieldExpression>
</textField>
Example on how it looks (sorry for the missing data):

Yes there is, but not on the text field. For the band the text field is in, you can set the Split Type to Prevent. if you set that on the Detail Band and the row needs to overflow to next page, it will move the entire row to the next page.

Related

Hidden property when exporting JasperReport

I want to hide a text field using jasper report so when i will view it in html i wont see its content, but the problem is that it has to be there (proggram JAWS has to read it) so i can't use "printWhenExpression". I tried to use width properties to hide it but so far i have no effect.
<textField>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
If I understood correctly, you need to keep the shape of the box as if the content was here, but you want the content to disappear.
It's true that printWhenExpression will make the whole item disappear.
You can either :
Use a parent item such as a frame to be around the textField, so printWhenExpression is true on the text fields, then the frame will still be here to keep its place.
<frame>
<reportElement x="0" y="0" width="200" height="200" uuid="25b397bf-e718-43ab-8641-4f7c7932253f"/>
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="4e8ea258-88f0-4220-a350-448da63390d1">
<printWhenExpression><![CDATA[the_condition]]></printWhenExpression>
</reportElement>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</frame>
Use a java condition in your text field. If the condition is false, the text will disappear but the text field frame will still be displayed.
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="4e8ea258-88f0-4220-a350-448da63390d1">
<printWhenExpression><![CDATA[the_condition]]></printWhenExpression>
</reportElement>
<textFieldExpression><![CDATA[my_condition ? $V{PAGE_NUMBER} : ""]]></textFieldExpression>
</textField>

how to move a group to next page JasperReport [duplicate]

Hi I am new to Jasper report and I am having a report with one text field and corresponding sub-report in the frame under detail band.Now I need to apply split type to prevent for the detail band, but I couldn't because I have subreport within detail band.Please assist me on this.Thanks in advance.
<detail>
<band height="30">
<frame>
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="500" height="30" backcolor="#333333"/>
<box>
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#9B64C8"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#9B64C8"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#9B64C8"/>
</box>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" isPrintRepeatedValues="false" mode="Opaque" x="0" y="0" width="166" height="30" isPrintInFirstWholeBand="true" isPrintWhenDetailOverflows="true" forecolor="#000000" backcolor="#FFFFFF">
</reportElement>
<box leftPadding="5">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#9BA66D"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#9BA66D"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#9BA66D"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#9BA66D"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Helvetica" size="9" isBold="false" pdfFontName="Helvetica" pdfEncoding="CP1252" isPdfEmbedded="false"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{text1}]]></textFieldExpression>
</textField>
<subreport>
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="0" height="30"/>
<dataSourceExpression><![CDATA[$F{subreport}]]></dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA["sub.jasper"]]></subreportExpression>
</subreport>
</frame>
</band>
</detail>
Second issue:
Since the textField needs to be next to subreport and you need jasper report to try to print the record completely on 1 page.
Design the report correctly (move the subreport and give it the correct dimensions)
<subreport>
<reportElement stretchType="RelativeToTallestObject" x="166" y="0" width="334" height="30" uuid="e812a308-674c-41dc-be83-e872752c8d6d"/>
<dataSourceExpression><![CDATA[$F{subreport}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{absolutePath} + "sub.jasper"]]></subreportExpression>
</subreport>
The subreport should have correct pageWidth, columnWidth and margins
<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="subreport" pageWidth="334" pageHeight="842" columnWidth="332" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="775a7e35-9af8-4206-a155-b05a478c35b0">
Move the splitType="prevent" from subreport to main report.
This will need that your datasource is a JRRewindableDataSource, since jasper report will try to fill band in current page, but if it can not it will need to rewind and fill on next page. You need to implement the moveFirst() method in your datasource.

Split type for frame

Hi I am new to Jasper report and I am having a report with one text field and corresponding sub-report in the frame under detail band.Now I need to apply split type to prevent for the detail band, but I couldn't because I have subreport within detail band.Please assist me on this.Thanks in advance.
<detail>
<band height="30">
<frame>
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="500" height="30" backcolor="#333333"/>
<box>
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#9B64C8"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#9B64C8"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#9B64C8"/>
</box>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement stretchType="RelativeToTallestObject" isPrintRepeatedValues="false" mode="Opaque" x="0" y="0" width="166" height="30" isPrintInFirstWholeBand="true" isPrintWhenDetailOverflows="true" forecolor="#000000" backcolor="#FFFFFF">
</reportElement>
<box leftPadding="5">
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#9BA66D"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#9BA66D"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#9BA66D"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#9BA66D"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Helvetica" size="9" isBold="false" pdfFontName="Helvetica" pdfEncoding="CP1252" isPdfEmbedded="false"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{text1}]]></textFieldExpression>
</textField>
<subreport>
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="0" height="30"/>
<dataSourceExpression><![CDATA[$F{subreport}]]></dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA["sub.jasper"]]></subreportExpression>
</subreport>
</frame>
</band>
</detail>
Second issue:
Since the textField needs to be next to subreport and you need jasper report to try to print the record completely on 1 page.
Design the report correctly (move the subreport and give it the correct dimensions)
<subreport>
<reportElement stretchType="RelativeToTallestObject" x="166" y="0" width="334" height="30" uuid="e812a308-674c-41dc-be83-e872752c8d6d"/>
<dataSourceExpression><![CDATA[$F{subreport}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{absolutePath} + "sub.jasper"]]></subreportExpression>
</subreport>
The subreport should have correct pageWidth, columnWidth and margins
<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="subreport" pageWidth="334" pageHeight="842" columnWidth="332" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="775a7e35-9af8-4206-a155-b05a478c35b0">
Move the splitType="prevent" from subreport to main report.
This will need that your datasource is a JRRewindableDataSource, since jasper report will try to fill band in current page, but if it can not it will need to rewind and fill on next page. You need to implement the moveFirst() method in your datasource.

Merging the cells which have same data

I have a report which has the columns Type,S.No,Date.Here the Type column will have the same value for all rows.So i just want to merge all the cells of Type column.I have referred the below link
Group several same value field into a single cell
and tried with their suggestion.But if i do like that i am getting like in below image.
Edit:
Below is the code i am using in my jrxml for merging the cells which have same data.
<field name="type" class="java.lang.String"/>
<group name="type">
<groupExpression><![CDATA[$F{type}]]></groupExpression>
</group>
In detail band i tried by creating the fake statictext behind the type textfield like below.
<staticText>
<reportElement x="0" y="0" width="121" height="20"/>
<box>
<leftPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<text><![CDATA[]]></text>
</staticText>
<textField>
<reportElement isPrintRepeatedValues="false" x="0" y="0" width="121" height="20"/>
<box>
<topPen lineWidth="0.0"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{type}]]></textFieldExpression>
</textField>
But no use.
Tried by using printWhenExpression in type textfield like below.
<textField>
<reportElement x="0" y="0" width="121" height="20">
<printWhenExpression><![CDATA[$V{type_COUNT} == 1]]></printWhenExpression>
</reportElement>
<box>
<topPen lineWidth="0.0"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{type}]]></textFieldExpression>
</textField>
But no use.
If i add the bottom border for static text then the image looks like below.
Thank You.
Add the bottom border in GroupFooter band for group "type"

How to have a column name spanning multiple lines in jasper report generation

I've written jasper specification to generate PDF from a table data source. This is working fine. Now i have to add few more columns and the report is now not looking good. I'm now thinking if I can squeeze in the column names in multiple lines like the one below
Maintenance Date
to
Maintenance
Date
Is it possible to achieve this in Jasper?
Regards,
Paul
Are your column names hard-coded? Do you just need to change "Maintenance Date" to "Maintenance\nDate" to have a carriage return?
Note: you cannot add a "\n" character to a Static Text element. You need to use a Text Field. Fortunately, you can just right-click on a Static Text element in iReport and transform it to a Text Field.
I guess that might solve it. If not, then you may need to make the question clearer.
If you are using JasperReports API you can use this sample:
//Detail
band = new JRDesignBand();
band.setHeight(40);
JRDesignStaticText staticText = new JRDesignStaticText();
staticText.setX(0);
staticText.setY(0);
staticText.setWidth(60);
staticText.setHeight(20);
staticText.setMode(ModeEnum.OPAQUE);
staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
staticText.setStyle(boldStyle);
staticText.setText("ID: ");
staticText.getLineBox().getLeftPen().setLineWidth(1);
staticText.getLineBox().getTopPen().setLineWidth(1);
staticText.getLineBox().setLeftPadding(10);
band.addElement(staticText);
textField = new JRDesignTextField();
textField.setX(60);
textField.setY(0);
textField.setWidth(200);
textField.setHeight(20);
textField.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
textField.setStyle(normalStyle);
expression = new JRDesignExpression();
expression.setValueClass(java.lang.Integer.class);
expression.setText("$F{Id}");
textField.setExpression(expression);
textField.getLineBox().getTopPen().setLineWidth(1);
textField.getLineBox().getRightPen().setLineWidth(1);
textField.getLineBox().setLeftPadding(10);
band.addElement(textField);
staticText = new JRDesignStaticText();
staticText.setX(0);
staticText.setY(20);
staticText.setWidth(60);
staticText.setHeight(20);
staticText.setMode(ModeEnum.OPAQUE);
staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
staticText.setStyle(boldStyle);
staticText.setText("Name: ");
staticText.getLineBox().getLeftPen().setLineWidth(1);
staticText.getLineBox().getBottomPen().setLineWidth(1);
staticText.getLineBox().setLeftPadding(10);
band.addElement(staticText);
textField = new JRDesignTextField();
textField.setStretchWithOverflow(true);
textField.setX(60);
textField.setY(20);
textField.setWidth(200);
textField.setHeight(20);
textField.setPositionType(PositionTypeEnum.FLOAT);
textField.setStyle(normalStyle);
expression = new JRDesignExpression();
expression.setValueClass(java.lang.String.class);
expression.setText("$F{FirstName} + \" \" + $F{LastName}");
textField.setExpression(expression);
textField.getLineBox().getRightPen().setLineWidth(1);
textField.getLineBox().getBottomPen().setLineWidth(1);
textField.getLineBox().setLeftPadding(10);
band.addElement(textField);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
The result will be:
This is almost identical to this snippet of jrxml file:
<detail>
<band height="40" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="60" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
</box>
<textElement/>
<text><![CDATA[ID: ]]></text>
</staticText>
<textField>
<reportElement x="60" y="0" width="200" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{Id}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="20" width="60" height="20"/>
<box leftPadding="10">
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
</box>
<textElement/>
<text><![CDATA[Name: ]]></text>
</staticText>
<textField>
<reportElement x="60" y="20" width="200" height="20"/>
<box leftPadding="10">
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{FirstName} + " " + $F{LastName}]]></textFieldExpression>
</textField>
</band>
</detail>