0
votes

I'm having some trouble trying to find the solution for this.

In my report, I have a subreport whose function is to print the name and surnames of the people related to the document I'm printing.

This subreport has just a textfield, in which the name and surnames are imported from the Java code.

The problem is that whenever the name and surnames are too long to be printed in a single line, I must finish to print them in a second one.

I tried two approaches to solve this (please ignore other fields, nameAndSurname is the relevant one):

1. Single detail band with a proper height for two lines.

One line

2. Double detail bands. By Java code, I cut the second surname and import it to the second name and surname field. The second detail only prints when the condition (nameAndSurnameTwo!=null) is true.

enter image description here


I couldn't get a solution for this yet. The problem is, the detail band keeps taking the second line space wheter it prints the second line or not.

In resume, what I need is a band that dynamically changes its height depending on the length of the name and surname I must print. If these are short, just a line. Only if the length of the name and surname are too long, it must take up two line space.

1

1 Answers

1
votes

This is normally solved by using isStretchWithOverflow

The solution is using: Single detail band with a proper height for one line. Setting the attribute on textField to isStretchWithOverflow="true"

Example

<detail>
    <band height="20">
        <textField isStretchWithOverflow="true">
            <reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="133" height="20" uuid="728a0280-ae29-425a-ab57-3ac78855de58"/>
            <textFieldExpression><![CDATA[$F{nameAndSurname}]]></textFieldExpression>
        </textField>
    </band>
</detail>

I would also advise to use stretchType="RelativeToTallestObject" on all other textElements so that they also stretch if your textField overflows.

Furthermore, if you like to force the stretch, you want for example that the Surname is displayed in second line, just add \n example Petter\nFriberg or use html <br/> setting markup to html.