如何让Jasper报表多行文本域下方的线条始终位于文本底部
解决Jasper报表文本域换行后线条位置偏移的问题
要让线条始终跟随文本域底部,核心是让线条位置依赖文本域的拉伸高度,推荐用Frame容器包裹文本域和线条,利用Frame的自动拉伸特性实现自适应布局:
具体修改步骤
- 用
<frame>包裹目标文本域和线条,Frame初始高度设为文本域的初始高度(16px),并设置stretchType="RelativeToTallestObject"让Frame自动适配内部最高元素高度。 - 将文本域的
positionType改为FixRelativeToTop,确保其在Frame内顶部位置固定,保留isStretchWithOverflow="true"允许文本换行拉伸。 - 修改线条的
positionType为FixRelativeToBottom,并将y值设为0,让线条始终贴在Frame底部。
修改后的代码片段
<!-- 原有image元素保持不变 --> <image scaleImage="FillFrame" hAlign="Left" vAlign="Middle" onErrorType="Blank"> <reportElement x="0" y="0" width="50" height="50" uuid="b53633a0-fb5a-4c88-baf5-872d9ef92e8e"> <property name="com.jaspersoft.studio.unit.height" value="px"/> <property name="com.jaspersoft.studio.unit.width" value="px"/> </reportElement> <imageExpression><![CDATA[$P{logo_estado}]]></imageExpression> </image> <!-- 用Frame包裹文本域和线条 --> <frame> <reportElement x="0" y="54" width="573" height="16" stretchType="RelativeToTallestObject" uuid="custom-frame-uuid"/> <textField isStretchWithOverflow="true"> <reportElement positionType="FixRelativeToTop" x="123" y="0" width="326" height="16" uuid="70e693d6-aeeb-434f-acd6-4600047a53fc"/> <textElement textAlignment="Center"> <font fontName="SansSerif" size="12" isBold="true"/> </textElement> <textFieldExpression><![CDATA["LIQUIDACIÓN DE " + $P{descLiquidacion}]]></textFieldExpression> </textField> <line> <reportElement positionType="FixRelativeToBottom" x="0" y="0" width="573" height="1" forecolor="#000000" uuid="64b3a12e-7094-4edb-a91d-1a4ed729e670"> <property name="com.jaspersoft.studio.unit.x" value="cm"/> </reportElement> </line> </frame>
原理说明
- Frame的
stretchType="RelativeToTallestObject"会自动拉伸到内部最高元素的高度,也就是换行后的文本域高度。 - 线条的
positionType="FixRelativeToBottom"使其位置相对于Frame底部固定,无论Frame高度如何变化,线条都会保持在文本域下方。 - 文本域的
FixRelativeToTop确保其始终在Frame顶部,避免拉伸导致位置错乱。
内容的提问来源于stack exchange,提问作者José Eduardo Pereda
相关产品推荐
相关产品推荐

