我正在使用 JasperReports 6.0.0 创建一个报告,其中显示一个包含 2 列的表格。第一列是文本字段,第二列是子报表,后跟文本字段。我希望文本字段能够拉伸,以便它适合子报表下方的剩余空间。
奇怪的是:如果子报表只有一个元素,文本字段会正确拉伸。
但是,如果子报表有多个元素,则文本字段就会消失(我检查过,它不在子报表下方!)
到目前为止,我的理解是,仅当子报表不超过我为其设置的初始高度时,才会显示文本字段。在上面的示例中,我将子报表的初始高度设置为 20px。子报表中的文本字段的高度为 20px。因此,当我在子报表中仅打印一个文本字段时,子报表的实际高度等于其初始高度,即 20px。但是,当我打印三个文本字段时,实际高度为 60px,大于初始高度 20px。这时子报表下方的文本字段就会消失。对于上下文,如果我将该文本字段设置为不拉伸,则无论它有多少元素,它都会正确显示在子报表下方,但我需要它拉伸。
报告代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0 -->
<!-- 2023-08-23T22:29:24 -->
<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="monitoringBulletinRainSensors" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="dc5d5b62-3121-47f6-bc9c-a0c86a6de260">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="test"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\Users\\<user>\\JaspersoftWorkspace\\MyReports\\"]]></defaultValueExpression>
</parameter>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="text" class="java.lang.String"/>
<detail>
<band height="30" splitType="Prevent">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<textField isStretchWithOverflow="true">
<reportElement key="" stretchType="RelativeToBandHeight" x="0" y="0" width="50" height="30" uuid="17091a86-28eb-4f4b-814e-2049fc13b754">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="local_mesure_unitwidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<box>
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="50" y="0" width="160" height="20" isPrintInFirstWholeBand="true" backcolor="#FFFFFF" uuid="d8d61ef2-ee12-437a-be7d-9514dc16438d">
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="local_mesure_unitwidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("children")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "test_child.jasper"]]></subreportExpression>
</subreport>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" stretchType="RelativeToBandHeight" x="50" y="20" width="160" height="10" uuid="23a8ffa1-34a5-437d-9dc3-1e55a571b2f4"/>
<box>
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA["This text field should stretch"]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
这是子报表的代码:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0 -->
<!-- 2023-08-23T17:48:48 -->
<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="test_child" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6ee7b94c-7f06-43f8-a02a-461787c5be45">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="text" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="20" splitType="Prevent">
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" stretchType="RelativeToBandHeight" x="0" y="0" width="160" height="20" uuid="bcfd27d0-fc93-42bd-82fb-74a8d95ed706">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="local_mesure_unity" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="px"/>
</reportElement>
<box>
<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
最后这是我正在使用的 JSON 数据源(更改子级的数量来模拟这两种情况):
{
"text": "This is a very very very very very very very long text",
"children": [
{
"text": "This is a short text"
},
{
"text": "This is a short text"
},
{
"text": "This is a short text"
}
]
}
非常感谢任何帮助。谢谢!