1

I want add set text in textField and staticText as it will divide in equal space in jasper report.

Text should be distributed like below image.

Setting textAlignment to "Justified", but it didn't work.

Can someone suggest me the solution for this?

I have to do it in jasper report and generate an report in PDF file.

<staticText>
    <reportElement x="52" y="34" width="135" height="20" uuid="44eed122-c07e-42d0-b696-a6ad54b759a4"/>
    <textElement textAlignment="Justified" verticalAlignment="Middle" rotation="None">
        <font fontName="Serif" size="12"/>
    </textElement>
    <text><![CDATA[Welcome]]></text>
</staticText>

Text should be distributed like bellow text in PDF report.

Nitin
  • 13
  • 1
  • 5
  • 1
    Could you please depict what 'didn't work' means? What result have you got? – Oleks Jul 12 '19 at 20:35
  • @Oleksandr After applying textAlignment to "Justified", the text is align to normal as textAlignment to "Left". – Nitin Jul 15 '19 at 05:06
  • Was the answer helpful? Maybe I missed some question details? – Oleks Jul 17 '19 at 17:55
  • @Oleksandr Thank you. textAlignment="Justified" property worked for large text or paragraph. But i want the output like i attached image. How i can output the text like this? – Nitin Jul 18 '19 at 05:17
  • Have you checked the workaround I described? Does it work in your case? – Oleks Jul 18 '19 at 14:04
  • @Oleksandr This is working for me. Workaround part is the solution to my problem. Thank you very much. – Nitin Jul 23 '19 at 05:40

1 Answers1

2

Justification - the spaces between words and between glyphs or letters are stretched or compressed in order to align both the left and right ends of consecutive lines of text. When using justification, it is customary to treat the last line of a paragraph separately by simply left or right aligning it, depending on the language direction.

So you need lines of text to see the difference between textAlignment="Left" and textAlignment="Justified" values. I guess the alignment textAlignment="Justified" works properly but it treats the text as "the last line of a paragraph". If you put long text in staticText you will have no troubles (v6.9.0.). See the image below.

The workaround could be something just like this:

  1. Create tabSet variable
<variable name="tabSet" class="java.lang.String">
    <variableExpression><![CDATA["&#09;&#09;&#09;&#09;&#09;"]]></variableExpression>
    <initialValueExpression><![CDATA["&#09;&#09;&#09;&#09;&#09;"]]></initialValueExpression></variableExpression>
</variable>
  1. Add the variable to any expression you need to be justified including the last\first line
<textFieldExpression><![CDATA["Lorem ipsum dolor sit amet"+$V{tabSet}]]></textFieldExpression>
  1. Set alignment and markup
<textElement textAlignment="Justified" markup="html"/>

The full example below:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3  -->
<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="report3" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="00216f45-7b94-4847-94ea-7ea4f1508a6e">
    <scriptlet name="DynTable" class="net.sf.jasperreports.engine.JRDefaultScriptlet">
        <scriptletDescription><![CDATA[]]></scriptletDescription>
    </scriptlet>
    <variable name="tabSet" class="java.lang.String">
        <variableExpression><![CDATA["&#09;&#09;&#09;&#09;&#09;"]]></variableExpression>
        <initialValueExpression><![CDATA["&#09;&#09;&#09;&#09;&#09;"]]></initialValueExpression>
    </variable>
    <detail>
        <band height="560" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="550" height="20" uuid="803f5f42-1d88-4da6-973e-bb70e6e99388"/>
                <textElement textAlignment="Center">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Justified"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement x="0" y="20" width="550" height="20" uuid="803f5f42-1d88-4da6-973e-bb70e6e99388"/>
                <textElement textAlignment="Justified" verticalAlignment="Middle" markup="styled"/>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="40" width="550" height="20" uuid="1132e9a7-8b30-4167-94ce-a013480dca9d"/>
                <textElement textAlignment="Center">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Left"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="60" width="550" height="20" uuid="803f5f42-1d88-4da6-973e-bb70e6e99388"/>
                <textElement textAlignment="Left" verticalAlignment="Justified"/>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="80" width="550" height="20" uuid="bc341f2a-916e-4391-90dc-8b68684d2c6c"/>
                <textElement textAlignment="Center">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Justified"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement x="0" y="100" width="550" height="60" uuid="803f5f42-1d88-4da6-973e-bb70e6e99388"/>
                <textElement textAlignment="Justified" verticalAlignment="Justified" markup="none"/>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="160" width="550" height="20" uuid="8472fd7d-b77c-46bf-9a11-b5f4cd1fff78"/>
                <textElement textAlignment="Center">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Left"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="180" width="550" height="60" uuid="803f5f42-1d88-4da6-973e-bb70e6e99388"/>
                <textElement textAlignment="Left" verticalAlignment="Top"/>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="240" width="550" height="20" uuid="8da01066-6dc6-4551-8530-2ac179fab340"/>
                <textElement textAlignment="Center">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Workaround"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="260" width="550" height="20" uuid="931c0377-e69d-41e9-8c0e-eec6b53d14d1"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Justified" markup="html"/>
                <textFieldExpression><![CDATA["Lorem"+$V{tabSet}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="280" width="550" height="20" uuid="aa466b7b-c67b-4cf7-bb52-09d52fceedf6"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Justified" markup="html"/>
                <textFieldExpression><![CDATA["Lorem ipsum"+$V{tabSet}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="300" width="550" height="20" uuid="e6cb9bd0-80b6-4138-9e58-803caf5b9c54"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Justified" markup="html"/>
                <textFieldExpression><![CDATA["Lorem ipsum dolor"+$V{tabSet}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="1" y="320" width="550" height="20" uuid="113a944e-b09b-419e-abf9-06b76959731d"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Justified" markup="html"/>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit"+$V{tabSet}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="340" width="550" height="20" uuid="920883bb-cd3a-4a26-99e4-ad0a5415033f"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Justified" markup="html"/>
                <textFieldExpression><![CDATA["Lorem ipsum dolor sit amet"+$V{tabSet}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Jasper Report textAlignment

Oleks
  • 1,011
  • 1
  • 14
  • 25
  • This is working for Detail, Summary sections of jasper report but not working for Title, Page Header or Column Header sections of jasper report. Can you please help me for this? – Nitin Jul 30 '19 at 12:12
  • @Nitin pls provide "buggy" examples – Oleks Jul 30 '19 at 15:04
  • I have added my query in bellow thread: https://stackoverflow.com/a/57309179/4261213 – Nitin Aug 01 '19 at 12:37
  • The answer is up to date. The workaround works properly with all sections. – Oleks Nov 07 '19 at 14:34