Jasper Reports - 将动态文本字段及其标签水平对齐

21

我正在使用Jasper报告5.2,iReport 5.2并将报告导出为RTF和PDF格式。

在我的报告中,我想添加一些文本字段以及它们的(静态文本)标签,水平对齐,类似于

         Name:  $F{name}
          Age:  $F{age}
Date of Birth:  $F{dateOfBirth}

但是我无法对齐它们。 这是我的尝试:

  • 位置类型:浮动(适用于所有静态文本和字段)
  • 拉伸类型:无拉伸(适用于所有静态文本和字段)
  • 溢出时拉伸:是(适用于所有动态文本字段)

下面的图片展示了我得到的和我想要的效果。 此外,我的文本字段内容是动态的,即内容大小可能会变化。 enter image description here

我已经阅读了许多论坛但找不到解决方案,请提供建议。

谢谢

4个回答

36

借助容器元素——Frame,可以轻松实现。

您需要将Position Type设置为FloatFrame放置在一起,并将它们两个都放上staticText(标签)和textField(文本字段)。

对于textField,我将Position Type设置为Float,并将Stretch With Overflow设置为true

示例

jrxml文件:

<?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="textfields_allign" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d855bf18-5e9b-4060-8caa-3fdd08abce3b">
    <parameter name="name" class="java.lang.String"/>
    <parameter name="id" class="java.lang.String"/>
    <parameter name="date" class="java.lang.String"/>
    <title>
        <band height="69" splitType="Stretch">
            <frame>
                <reportElement uuid="314bfd5b-7b0a-42f4-aca3-e61f0283f126" positionType="Float" x="213" y="1" width="243" height="20"/>
                <staticText>
                    <reportElement uuid="e07201bb-a677-4dc9-a332-f94e6eca2722" positionType="Float" x="0" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <text><![CDATA[Name]]></text>
                </staticText>
                <textField isStretchWithOverflow="true">
                    <reportElement uuid="66c93a44-9015-4ae9-bf05-b68b2420f3ef" positionType="Float" x="121" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
                </textField>
            </frame>
            <frame>
                <reportElement uuid="8311a483-955e-4280-a9ac-513d7d34495b" positionType="Float" x="213" y="21" width="243" height="20"/>
                <staticText>
                    <reportElement uuid="da404b4e-7908-40ae-8e1b-38e19d9ddc7f" positionType="Float" x="0" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <text><![CDATA[ID]]></text>
                </staticText>
                <textField isStretchWithOverflow="true">
                    <reportElement uuid="e0e312e8-cd2f-48af-8ae8-df95c6195488" positionType="Float" x="121" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$P{id}]]></textFieldExpression>
                </textField>
            </frame>
            <frame>
                <reportElement uuid="8ff2baf9-b0ec-4c8a-b54a-9edd08b200cc" positionType="Float" x="213" y="41" width="243" height="20"/>
                <staticText>
                    <reportElement uuid="b8d97db0-250a-43c8-a2f3-2fa3668c5d15" positionType="Float" x="0" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <text><![CDATA[Date]]></text>
                </staticText>
                <textField isStretchWithOverflow="true">
                    <reportElement uuid="4daa4d17-28be-4ac8-8e1e-2efbeec5f690" positionType="Float" x="121" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </title>
</jasperReport>

iReport中的报告设计:

输入图像描述

结果将通过iReport预览显示:

输入图像描述


4
为了完成Alex K的解决方案:使用Jaspersoft Studio时,不要忘记将“静态文本”属性中的“拉伸类型”设置为“相对于最高对象”。

1
如果值列为空,则跳过整个框架 - 不要忘记设置“当空白时删除行”标志,并在框架属性中填写“表达式打印时”。 - Silence

3
在Jasper Reports 3.6及以后版本(包括5.2)中,有一个使用多个明细区域的功能。您可以将第一个名称块和"Sally Admison..."文本放在第一个区域中。在其下方是您的患者ID和"1234567890Blah...",再下面是另一个细节区域,显示出生日期。这将给您想要的结果。
请勿参考我在此处的帖子,因为它适用于旧版3.5的Jasper Reports。那个版本不允许您使用多个区域,我必须使用一个单独的明细区域。但在5.2中不是这样。您可以尝试使用多个明细区域。享受吧!

虽然这不是非常整洁的解决方案,但它能够正常工作并且让我以不同的方式思考。对于可能遇到相同问题的每个人,@Rachcha提供的上述解决方案都可以很好地工作。 - Ali

0

只需增加您的详细信息窗格的宽度,这样就可以解决我的对齐问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接