ZetCode

JasperReports textField's textAdjust

last modified February 21, 2024

In this article we cover textField's textAdjust attribute in JasperReports library.

JasperReports is an open-source reporting library. It can create reports in various formats including PDF, HTML, XLS, or CSV. JasperReports creates page-oriented, ready-to-print documents.

The textAdjust attribute in JasperReports is used to control the behavior of a text field when the content is too large for the defined dimensions. It was introduced in JasperReports Library 7.5.0, replacing the deprecated isStretchWithOverflow attribute.

It can take the following values:

Example

The next example demonstrates all three options.

report.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
        "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<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="report" topMargin="20" bottomMargin="20">

    <style name="defFont" isDefault="true" vAlign="Top" hAlign="Left"/>
    <parameter name="text"/>

    <detail>
        <band height="330">

            <rectangle>
                <reportElement x="3" y="3" width="490" height="85"/>
            </rectangle>

            <textField textAdjust="CutText">
                <reportElement x="5" y="5" width="490" height="80"/>
                <textFieldExpression>
                    <![CDATA[$P{text}]]>
                </textFieldExpression>
            </textField>

            <rectangle>
                <reportElement x="3" y="90" width="490" height="105"/>
            </rectangle>

            <textField textAdjust="StretchHeight">
                <reportElement x="5" y="95" width="490" height="80"/>
                <textFieldExpression>
                    <![CDATA[$P{text}]]>
                </textFieldExpression>
            </textField>

            <rectangle>
                <reportElement x="3" y="200" width="490" height="85"/>
            </rectangle>

            <textField textAdjust="ScaleFont">
                <reportElement x="5" y="205" width="490" height="80"/>
                <textFieldExpression>
                    <![CDATA[$P{text}]]>
                </textFieldExpression>
            </textField>

        </band>
    </detail>

</jasperReport>

We have three text fields in the report.

<parameter name="text"/>

The larger text is passed to the report via the name parameter.

<textField textAdjust="CutText">
    <reportElement x="5" y="5" width="490" height="80"/>
    <textFieldExpression>
        <![CDATA[$P{text}]]>
    </textFieldExpression>
</textField>

In this textField, we set the textAdjust property to CutText value.

<rectangle>
    <reportElement x="3" y="90" width="490" height="105"/>
</rectangle>

To understand where the text fields begin and end, we place rectangles around them.

report.gvy
package com.zetcode

@Grab(group='net.sf.jasperreports', module='jasperreports', version='6.21.0')
@Grab(group='com.github.librepdf', module='openpdf', version='1.3.39')

import net.sf.jasperreports.engine.JasperCompileManager
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.JasperExportManager
import net.sf.jasperreports.engine.JREmptyDataSource

def text = '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id justo 
lacus. Nullam semper sed nulla finibus semper. Sed tristique ipsum et
ullamcorper ullamcorper. In interdum sapien eu scelerisque faucibus. Sed nec ex
at tortor varius aliquet. Donec cursus lorem massa, faucibus auctor neque
dignissim id. Quisque eu ultrices nisl, et commodo ligula. Phasellus neque nunc,
interdum quis lectus a, cursus egestas tellus. Fusce dictum ligula sed porta
scelerisque. 
'''

def xmlFile = 'report.xml'
def jreport = JasperCompileManager.compileReport(xmlFile)

def params = ['text': text]
def jrPrint = JasperFillManager.fillReport(jreport, params,
        new JREmptyDataSource())

JasperExportManager.exportReportToPdfFile(jrPrint, 'report.pdf')

This Groovy file builds the report. We define the text and passed it via a text parameter to the report.

In this article we have worked with the textField's textAdjust attribute.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all JasperReports tutorials.