1

I want to print in block letters in a jasper print. E.g. I have a Bank Format and I want the name to come in the boxes. How does one do that?

Name: [F][I][R][S][T][ ][N][A][M][E]
  • Option1 - Print cell by cell with a ${FIRST_NAME}.charAt(x) - which is unsustainable

  • Option2 - Add spaces between characters but this changes based on font size

  • Option3 - Add padding spaces to the end of the text. Stretch the text to the full width available and characters get distributed equally - how does one do this?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
AVM
  • 303
  • 1
  • 4
  • 15

1 Answers1

1

This is normal achieved using a monospaced font.

If you like to achieve it with other fonts or the spacing is very large you can use a subreport (that is called every time you need the fixed space)

Example

Main report (I'm using parameter to test)

Call the subreport with a datasource that contains each letter, I'm using $P{testText}.split("") , since char[] is not allowed in the JRBeanArrayDataSource.

Note: split will give empty first String in java7 but not in java8

<?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="MainFixedSpace" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d2bcb5ab-c751-4f39-8753-561b8a6ac629">
    <parameter name="testText" class="java.lang.String">
        <defaultValueExpression><![CDATA["Hello world"]]></defaultValueExpression>
    </parameter>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="25" splitType="Stretch">
            <subreport>
                <reportElement x="0" y="0" width="555" height="25" uuid="76f53ca9-da1f-46c8-bb3b-aca0dc43d2d3"/>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanArrayDataSource($P{testText}.split(""))]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "MainFixedSpace_subreport.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </title>
</jasperReport>

Subreport

Setup the the fixed space column count in my case 20 columns on each row (you need to adapted it to your case) and set printOrder="Horizontal" The _THIS field will let you access the letter.

<?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="MainFixedSpace_subreport" columnCount="20" printOrder="Horizontal" pageWidth="555" pageHeight="802" columnWidth="27" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="77ba37de-32e1-4ec6-8496-58d716d0340d">
    <field name="_THIS" class="java.lang.String"/>
    <detail>
        <band height="25" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="27" height="25" uuid="ffb27000-41ba-419f-8836-b24dbb0dbb25"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Output

Result

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109