Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a column name spanning multiple lines in jasper report generation

I've written jasper specification to generate PDF from a table data source. This is working fine. Now i have to add few more columns and the report is now not looking good. I'm now thinking if I can squeeze in the column names in multiple lines like the one below

Maintenance Date

to

Maintenance

Date

Is it possible to achieve this in Jasper?

Regards, Paul

like image 904
paulhudson Avatar asked Oct 10 '22 10:10

paulhudson


2 Answers

Are your column names hard-coded? Do you just need to change "Maintenance Date" to "Maintenance\nDate" to have a carriage return?

Note: you cannot add a "\n" character to a Static Text element. You need to use a Text Field. Fortunately, you can just right-click on a Static Text element in iReport and transform it to a Text Field.

I guess that might solve it. If not, then you may need to make the question clearer.

like image 87
mdahlman Avatar answered Oct 13 '22 10:10

mdahlman


If you are using JasperReports API you can use this sample:

    //Detail
    band = new JRDesignBand();
    band.setHeight(40);

    JRDesignStaticText staticText = new JRDesignStaticText();
    staticText.setX(0);
    staticText.setY(0);
    staticText.setWidth(60);
    staticText.setHeight(20);
    staticText.setMode(ModeEnum.OPAQUE);
    staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
    staticText.setStyle(boldStyle);
    staticText.setText("ID: ");
    staticText.getLineBox().getLeftPen().setLineWidth(1);
    staticText.getLineBox().getTopPen().setLineWidth(1);
    staticText.getLineBox().setLeftPadding(10);
    band.addElement(staticText);

    textField = new JRDesignTextField();
    textField.setX(60);
    textField.setY(0);
    textField.setWidth(200);
    textField.setHeight(20);
    textField.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
    textField.setStyle(normalStyle);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.Integer.class);
    expression.setText("$F{Id}");
    textField.setExpression(expression);
    textField.getLineBox().getTopPen().setLineWidth(1);
    textField.getLineBox().getRightPen().setLineWidth(1);
    textField.getLineBox().setLeftPadding(10);
    band.addElement(textField);

    staticText = new JRDesignStaticText();
    staticText.setX(0);
    staticText.setY(20);
    staticText.setWidth(60);
    staticText.setHeight(20);
    staticText.setMode(ModeEnum.OPAQUE);
    staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
    staticText.setStyle(boldStyle);
    staticText.setText("Name: ");
    staticText.getLineBox().getLeftPen().setLineWidth(1);
    staticText.getLineBox().getBottomPen().setLineWidth(1);
    staticText.getLineBox().setLeftPadding(10);
    band.addElement(staticText);


    textField = new JRDesignTextField();
    textField.setStretchWithOverflow(true);
    textField.setX(60);
    textField.setY(20);
    textField.setWidth(200);
    textField.setHeight(20);
    textField.setPositionType(PositionTypeEnum.FLOAT);
    textField.setStyle(normalStyle);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$F{FirstName} + \" \" + $F{LastName}");
    textField.setExpression(expression);
    textField.getLineBox().getRightPen().setLineWidth(1);
    textField.getLineBox().getBottomPen().setLineWidth(1);
    textField.getLineBox().setLeftPadding(10);
    band.addElement(textField);

    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);

The result will be:

enter image description here

This is almost identical to this snippet of jrxml file:

<detail>
    <band height="40" splitType="Stretch">
        <staticText>
            <reportElement x="0" y="0" width="60" height="20"/>
            <box leftPadding="10">
                <topPen lineWidth="1.0"/>
                <leftPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <text><![CDATA[ID: ]]></text>
        </staticText>
        <textField>
            <reportElement x="60" y="0" width="200" height="20"/>
            <box leftPadding="10">
                <topPen lineWidth="1.0"/>
                <rightPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Id}]]></textFieldExpression>
        </textField>
        <staticText>
            <reportElement x="0" y="20" width="60" height="20"/>
            <box leftPadding="10">
                <leftPen lineWidth="1.0"/>
                <bottomPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <text><![CDATA[Name: ]]></text>
        </staticText>
        <textField>
            <reportElement x="60" y="20" width="200" height="20"/>
            <box leftPadding="10">
                <bottomPen lineWidth="1.0"/>
                <rightPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <textFieldExpression><![CDATA[$F{FirstName} + " " + $F{LastName}]]></textFieldExpression>
        </textField>
    </band>
</detail>
like image 27
Alex K Avatar answered Oct 13 '22 12:10

Alex K