JasperReports text field stretching - can stretch height be limited? - jasper-reports

I've got the following problem with a JasperReports subreport that I've been trying to solve for the past couple of days with no success. Below is an image of what I'm trying to do.
I've got a subreport in one of my reports that is relatively simple. Its got a title band with a single text field (marked brown in the image) for the subreport title and a detail band with two items: a subreport (marked blue in the image) printing my line items and a text field (marked red in the image) printing the comments associated with the line items printed to the left of it. The line item subreport can print a variable number of line items (datasource backed by a bean collection). My customers requirement is that the comments field stretches and shows the text entered, but be limited by the height of the line item section (represented by the arrows on the image). I somehow need to prevent the comments from stretching beyond the line items and make them of equal height (there should be no green box in the image).
Does anyone have any tips how this could be (and can it at all be) achieved in JasperReports?
p.s.
We are currently compiling our reports with JR version 3.6

Put both the subreport and the text field into a frame. For the text field, set "Stretch with overflow" to false and "Stretch type" to "Relative to tallest object"
The text field will stretch relative to the tallest object in it's container. It's container is now the frame, so the "tallest object" can only be the subreport. Make sure the elements are the same height when you define them, otherwise they will grow at different rates.
JRXML sample:
<band height="50">
<frame>
<reportElement x="0" y="0" width="555" height="50"/>
<subreport>
<reportElement x="0" y="0" width="378" height="50"/>
...
</subreport>
<textField isStretchWithOverflow="false">
<reportElement stretchType="RelativeToTallestObject" x="378" y="0" width="177" height="50"/>
...
</textField>
</frame>
</band>

Related

Subreports with alternating backgrounds

I'm using iReport 3.7.6. (with no possibility of using a newer version).
I have a main report and a subreport. Subreport repeats multiple times and I need to set alternating background to subreports, but I need to show subreports one below each other so that they split when the new page starts, not showing them on page only if they fit.
This is what I want:
I tried multiple things but I cannot seem to get it to work.
I tried using frame and setting alternate row style and putting all the elements in that frame:
<style name="AlternateRowStyle" mode="Opaque">
<conditionalStyle>
<conditionExpression><![CDATA[$V{REPORT_COUNT} % 2 == 0]]></conditionExpression>
<style backcolor="#E5E7E9"/>
</conditionalStyle>
</style>
but I get results like in this post: Splitting a band's background frame
Then I tried using background band (for simplicity, I just added frame with one color, so no alternation) but I have experienced difficulties in this approach too.
My subreport has only Detail and Background band to which I've set equal height and split type immediate.
Main report only has detail band (I simplified it).
Subreport snippet:
<background>
<band height="250" splitType="Immediate">
<frame>
<reportElement mode="Opaque" x="0" y="0" width="600" height="250" backcolor="#E5E7E9"/>
</frame>
</band>
</background>
<detail>
<band height="250" splitType="Immediate">
//content not important
</band>
</detail>
Main report snippet:
<detail>
<band height="660" splitType="Immediate">
<subreport>
<reportElement positionType="Float" x="0" y="0" width="600" height="250"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{objects})]]></dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "subreport1.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
But this approach results in wierd behaviour: Background from subreport shows on each page of main report only once and only at the beginning of the page, no matter how many subreports are actually on one page of main report, like background is set to main report (shown on picture). I would like each report to have the background.
I'd appreciate any help.

Jasper Reports 5: Title Band, how to create a footer?

i have a title band. my report is landscape 595 by 842. i simply want the title band to take up a full page and be able to add text to the very bottom of that title page/band.
i have tried position fix relative to bottom. it still floats up. i have tried making the title band 559 high (with 36 top margin to make 595). that works except there is tons of white space and so if something on the title expands (dynamic data) the text at title page bottom breaks to second page.
Got it! Problem is that title band does not stretch, so fix relative to bottom does not work. Solution is to add a background band, height of report (minus margin). Then put page footer there, is will show on title and all other pages. Then add a blank page footer to save white space for the real footer in the background. done.
Why not use only the pageFooter and then if you only like it to display on first page use the printWhenExpression.
<pageFooter>
<band height="50">
<printWhenExpression><![CDATA[$V{PAGE_NUMBER}.intValue()==1]]></printWhenExpression>
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="dd4c229b-7453-4026-b01e-cfc325053335"/>
<textFieldExpression><![CDATA["You will only see me on first page"]]></textFieldExpression>
</textField>
</band>
</pageFooter>
Note the pageFooter will be present on every page (even if blank), this is the famous firstPageFooter request / tracker, but this seems not to be your case since you are already adding it later...

How to Hide lines after isPrintRepeatedValues="false" in Jasper reports

I have requirement to hide the line after setting a property isPrintRepeatedValues="false" and values is not repeated but the getting line.and check the below image and highlighted with red color arrow.
That is shouldn't come and USA will come central. Can i achive this requirement if possible?
You can use subreport for all fields exclude first field.
First field and subreport placed into frame with borders.
For first field set properties:
stretchType="RelativeToBandHeight"
verticalAlignment="Middle"
First field haven't borders.

How to increase height of 'band' element if the value in 'textFieldExpression' has greater height than the height specified in "band height"?

My detail element is as follows:-
<detail>
<band height="22" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement uuid="a59a8d47-59cb-490e-953d-d4b1e2af52c0" x="0" y="0" width="191" height="20"/>
<box leftPadding="5" rightPadding="5"/>
<textElement verticalAlignment="Middle">
<font fontName="ARIALUNI" size="10" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{key}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement uuid="09798815-2a54-42db-963a-3f443a5fc1c2" x="191" y="0" width="500" height="20"/>
<box leftPadding="5" rightPadding="5"/>
<textElement verticalAlignment="Middle">
<font fontName="ARIALUNI" size="10" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</band>
</detail>
If I set band height="30", then the value of second correctly overflows but at same time extra white space is visible for rows where there is no overflow. With band height="22", no extra whitespace and also no overflows (which is a requirement). So can the height of band be set dynamically so that height increases when overflow takes place, otherwise it (height) maintains a low value (without extra-space).
This already happens automatically. If a band contains content that stretches, it will also stretch to accommodate that content. Or, as the JasperReports Ultimate Guide puts it:
"When stretchable text fields are present on a report section, the height of the report section
itself is affected by the stretch."
This makes sense when you think about when the report is filled. Theoretically you could always pass longer and longer data, making the text field stretch further and further. If the band height had to be set manually to accommodate this, you would be forever updating the design.
So what's the solution? Easy: Design everything at the minimum height, set fields to stretch if they need to, and don't worry about the band height.
What about the white space?
The white space you're seeing is probably due to the way space in the design is preserved at the end of a band. With your example, the band height is 22 pixels, but the content only fills a height of 20 pixels, leaving a 2 pixel row of white space at the bottom of the band. When you increase the band height to 30 pixels, without increasing the size of the content (I'm assuming this is the case), you will have created 10 pixels of white space.
The white space you leave in you design will be included in the filled report, repeated for each instance of the band and still included if the content stretches. So after your text field stretches to be 30 pixels tall, the band will stretch to 40 pixels to preserve the 10 pixels of white space.

JasperReports: does positionType=''Float'' work at all for staticText?

I need to have a staticText to float below another element with variable height. I'm taking a sample from the book JasperReports for Java Programmers. It says that it's possible to do it. The difference with my sample is that I use a hard coded string in textFieldExpression, they use a $F{} field.
<textField isStretchWithOverflow="true" hyperlinkType="None">
<reportElement x="0" y="0" width="100" height="24"/>
<box>
<pen lineWidth="0.5" lineStyle="Solid"/>
</box>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["jjjjjjjjjjj oooooooooo uuuuuuuuuu aaaaaaaaaa xxxxxxxxx u ttttt"]]></textFieldExpression>
</textField>
<staticText>
<reportElement positionType="Float" x="0" y="25" width="500" height="30"/>
<box>
<pen lineWidth="0.5" lineStyle="Solid"/>
</box>
<textElement/>
<text><![CDATA[ This staticText element has a positionType of "Float" ppp ]]></text>
</staticText>
It says that when you use positionType="Float", the y attribute is ignored, because the staticText is positioned below the textField. This appears to work, BUT... is it possible they are BS'ing me? The textField has y=0 and height=24. The staticText has y=25. Coincidence? If I change the staticText's y, it is obvious that it isn't ignored: the element is positioned wherever I tell it to. There's no floating!!!
Quote from the book:
As we can see, setting the positionType attribute of <reportElement> to Float made JasperReports ignore the y position of the <staticText> element, which was pushed down by the stretched <textField> element.
No, we don't see how it is being ignored because y=25 is where we expected it!
Does positionType="Float" work for staticText at all?
Thanks!
My understanding of the floating nature is that the floating object will move down if necessary, but if the previous object is smaller than expected, it doesn't move up.
I don't recall having any problems with floating objects not actually floating down when necessary, whether they be static text or text fields.
On another note, if you find a text field works, you can use it to display static text by putting your text in quotes in the text field value (making it a Java object).
This answer applies (different elements, same situation).
One thing I didn't take right from the book's sample is that the textField's declared height (24) is less than the actual height when the element is stretched. So, y="25" on the staticText would map above its actual position; the element is actually floated down!