Vertical text alignment in jasper reports

jasper-reports

I have a table in a jasper report, and I need the contents of certain cells in that table to be vertically aligned. I'm attempting to edit the report with iReport.

In iReport I can go into the properties of a cell and see that the vertical align is set to "Middle". Additionally, when I look at the XML directly (see below), I can see that the textElement tag has a verticalAlignment="Middle" attribute.

So near as I can tell the text should be vertically aligned within its little box, and yet it will not align properly.

I'm hoping that someone experienced with vertically aligning things in jasper reports can point out what I'm doing wrong. Thanks much.

<textField 
  isStretchWithOverflow="false"
  isBlankWhenNull="true" 
  evaluationTime="Now" 
  hyperlinkType="None"
  hyperlinkTarget="Self" >

  <reportElement
    x="227"
    y="0"
    width="31"
    height="14"
    key="textField-4"/>

  <box>
    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    <bottomPen lineWidth="0.0" lineColor="#000000"/>
    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
  </box>

  <textElement textAlignment="Center" verticalAlignment="Middle">
    <font fontName="Times New Roman" pdfFontName="Times-Roman" size="8"/>
  </textElement>

  <textFieldExpression class="java.lang.String"><![CDATA[$F{someVariableName}]]></textFieldExpression>
</textField>

So, to be clear, what I have in my report is something like this:

|--------|
|  text  |
|        |
|        |
|--------|

And what I want is something like this:

|--------|
|        |
|  text  |
|        |
|--------|

Best Answer

Here's how I fixed this problem (iReport 3.7.6):

In Designer tab:

  1. Right Click on the Static Text Box
  2. Select "Padding and Borders"
  3. In the pop up box, Increase the "Top" or "Bottom" value by, for example, 5, and click OK to see the result of this change.

As you can see, this will allow you to position your Static Text exactly how you want.

If you can only make the change in the XML, this is how the change showed up in XML:

    <box topPadding="4"/>

Example w/ the Static Text code block:

    <staticText>
    <reportElement positionType="Float" mode="Opaque" x="14" y="27" width="118" height="14" forecolor="#FFFFFF" backcolor="#909090"/>
    <box topPadding="4"/>
    <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None" lineSpacing="Single" markup="none">
    <font fontName="Arial" size="7" isBold="true" isUnderline="false" isStrikeThrough="false" isPdfEmbedded="false"/>
    </textElement>
    <text><![CDATA[SAMPLE TEXT SAMPLE TEXT]]></text>
    </staticText>
Related Topic