Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JasperReports: How to break lines base on a character count

I have a long string (150 characters). I put text_1 string parameter in the report and I want automatic jasper breakline at the 50th character.

<textFieldExpression><![CDATA[$F{TEXT_1}]]></textFieldExpression>

Example:

My string: 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789

I want to print with JasperReports:

01234567890123456789012345678901234567890123456789
01234567890123456789012345678901234567890123456789
01234567890123456789012345678901234567890123456789

I want to execute in Jasper, not excute in Java Please suggest me a solution.

like image 551
Dragon Avatar asked Mar 11 '23 21:03

Dragon


1 Answers

Approaches

There are a few approaches:

  • Set the width of textField and set isStretchWithOverflow attribute as true. This solution depends on the font metrics.

  • Use a Java expression. For example you can use Guava library. In this case you should add import statement to the template.

Example

In this example, only the Title band is used to demonstrate both solutions. An empty datasource is used.

Report Template

Demonstrates both variants of solving the task.

The width of the first textField is enough only for showing 50 symbols. With help isStretchWithOverflow property the text will be split into several rows (the height of textField will increase dynamically).

The second textField's expression (look at textFieldExpression) uses the Guava library.

Joiner.on("\n").join(Splitter.fixedLength(50).split(value)) - The Splitter.fixedLength(int) method allow us to split string by every 50 characters and the Joiner.on(String) method help us to join strings with newline character.

<?xml version="1.0" encoding="UTF-8"?>
<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="break_lines" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <import value="com.google.common.base.*"/>
    <parameter name="LONG_TEXT" class="java.lang.String">
        <defaultValueExpression><![CDATA["012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="347" splitType="Stretch">
            <textField isStretchWithOverflow="true">
                <reportElement x="110" y="10" width="280" height="30"/>
                <textFieldExpression><![CDATA[$P{LONG_TEXT}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement x="20" y="100" width="525" height="30"/>
                <textFieldExpression><![CDATA[Joiner.on("\n").join(Splitter.fixedLength(50).split($P{LONG_TEXT}))]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Output result

The output result in Jaspersoft Studio will be:

The result in JSS

A 150-character string is split into 3 rows, each row contains 50 characters.

like image 188
Alex K Avatar answered Mar 24 '23 15:03

Alex K