Dra*_*gon 3 java jasper-reports line-breaks
我有一个长串(150个字符).我在报告中添加了text_1字符串参数,我希望在第50个字符处使用自动jasper breakline.
<textFieldExpression><![CDATA[$F{TEXT_1}]]></textFieldExpression>
Run Code Online (Sandbox Code Playgroud)
例:
我的字符串: 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
我想用JasperReports打印:
01234567890123456789012345678901234567890123456789
01234567890123456789012345678901234567890123456789
01234567890123456789012345678901234567890123456789
Run Code Online (Sandbox Code Playgroud)
我想在Jasper中执行,而不是在Java中执行请建议我一个解决方案.
有几种方法:
设置textField的宽度并将isStretchWithOverflow属性设置为true.此解决方案取决于字体指标.
使用Java表达式.例如,您可以使用Guava库.在这种情况下,您应该将import语句添加到模板中.
在此示例中,仅使用标题带来演示两种解决方案.使用空数据源.
演示解决任务的两种变体.
第一个textField的宽度仅足以显示50个符号.借助isStretchWithOverflow属性,文本将被拆分为多行(textField的高度将动态增加).
第二个textField的表达式(查看textFieldExpression)使用Guava库.
Joiner.on("\n").join(Splitter.fixedLength(50).split(value))- Splitter.fixedLength(int)方法允许我们每50个字符拆分字符串,Joiner.on(String)方法帮助我们使用换行符连接字符串.
<?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="break_lines" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<import value="com.google.common.base.*"/>
<parameter name="LONG_TEXT" class="java.lang.String">
<defaultValueExpression><![CDATA["012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"]]></defaultValueExpression>
</parameter>
<title>
<band height="347" splitType="Stretch">
<textField isStretchWithOverflow="true">
<reportElement x="110" y="10" width="280" height="30"/>
<textFieldExpression><![CDATA[$P{LONG_TEXT}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="20" y="100" width="525" height="30"/>
<textFieldExpression><![CDATA[Joiner.on("\n").join(Splitter.fixedLength(50).split($P{LONG_TEXT}))]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
Run Code Online (Sandbox Code Playgroud)
Jaspersoft Studio中的输出结果将是:
阿150个字符数的字符串分割成3行,每行包含50个字符.