Jasper Reports: justify distributed align Text Field/Static Text

2.8k views Asked by At

I'm using Jasper Studio 6.1.0 to design PDF report.

In my report, I want to align text (text field or static text) as "justify distributed" like microsoft excel (show image below).

https://i.stack.imgur.com/YtIbc.jpg

I've read many forums but could not find a solution, please suggest.

Thanks!

1

There are 1 answers

0
adalbert On

If You are looking for something that works like soft return in MS Word (Shift+Enter) and want to achive textAlignment="Justified" in textElement no matter, where a break line is the trick below may be helpfull.
pdf fragment example
In TextField You can

  • set textAlignment="Justified" of textElement
  • set 2 Tab Stops: "left" at 0px, "right" at the full width of textField
  • use \t (or 	 if you prefer using markup="html") in the place where You want to break line. If You want to justify the last or single line use \t\t (or 		)
         <textField>
                <reportElement x="0" y="270" width="540" height="21">                
                </reportElement>
                <textElement textAlignment="Justified">
                    <paragraph>
                        <tabStop position="0" alignment="Left"/>
                        <tabStop position="540" alignment="Right"/>
                    </paragraph>
                </textElement>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet\t\t"]]></textFieldExpression>
                </textField>


and the whole example:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.18.1.final using JasperReports Library version 6.18.1-9d75d1969e774d4f179fb3be8401e98a0e6d1611  -->
<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="SoftReturn" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e0df1bfc-6397-4160-9999-538487a658d0">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <detail>
        <band height="600" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="px"/>
            <textField>
                <reportElement x="0" y="70" width="260" height="150" uuid="626378db-b76e-44b4-b768-e1996f005058">
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    <property name="com.jaspersoft.studio.unit.x" value="px"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Justified">
                    <paragraph>
                        <tabStop position="0" alignment="Left"/>
                        <tabStop position="260" alignment="Right"/>
                    </paragraph>
                </textElement>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." + "\n\n\n" + "Lorem ipsum dolor sit amet,\tconsectetur adipiscing elit,\tsed do eiusmod tempor incididunt ut labore et\tdolore magna aliqua.\t\t"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="280" y="70" width="260" height="150" uuid="626378db-b76e-44b4-b768-e1996f005058"/>
                <box>
                    <pen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Justified" markup="html">
                    <paragraph>
                        <tabStop position="0" alignment="Left"/>
                        <tabStop position="260" alignment="Right"/>
                    </paragraph>
                </textElement>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." + "<br><br><br>" + "Lorem ipsum dolor sit amet,&#09;consectetur adipiscing elit,&#09;tsed do eiusmod tempor incididunt ut labore et&#09;dolore magna aliqua.&#09;&#09;"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="238" width="540" height="21" uuid="cf7e3f9c-b381-4ee4-9e9a-4b3b2cd4e0e7">
                    <property name="com.jaspersoft.studio.unit.x" value="px"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Justified">
                    <paragraph>
                        <tabStop position="0" alignment="Left"/>
                        <tabStop position="540" alignment="Right"/>
                    </paragraph>
                </textElement>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet\t\t"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="270" width="540" height="21" uuid="658101ab-6f3b-499d-a717-0e6db6050986">
                    <property name="com.jaspersoft.studio.unit.x" value="px"/>
                </reportElement>
                <box>
                    <pen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Justified" markup="html">
                    <paragraph>
                        <tabStop position="0" alignment="Left"/>
                        <tabStop position="540" alignment="Right"/>
                    </paragraph>
                </textElement>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet&#09;&#09;"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>