I'm looking for a way to create a jasper report element that represents a dynamic height and label text box as a textField
that stretch with overflow
The content of the text box should include a dotted line for each line from beginning to end.
Rough sketch of desired result.
Final Decision
public class JRSplitText {
private String text;
private int firstLineCharacterCount = 0;
private AttributedString attributedString;
public JRSplitText(String text, String fontName, int fontStyle, float fontSize) {
this.text = text;
FontUtil fontUtil = FontUtil.getInstance(DefaultJasperReportsContext.getInstance());
Font font = fontUtil.getAwtFontFromBundles(fontName, fontStyle, fontSize, Locale.getDefault(), true);
attributedString = new AttributedString(text);
attributedString.addAttribute(TextAttribute.FONT, font);
}
public String getText(int wrappingWidth) {
if(text == null) {
return null;
}
AffineTransform transform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(transform, true, true);
AttributedCharacterIterator iterator = attributedString.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
TextLayout layout = measurer.nextLayout(wrappingWidth);
firstLineCharacterCount = layout.getCharacterCount();
return text.substring(0, firstLineCharacterCount);
}
public JRDataSource getDataSource(int wrappingWidth) {
if(text == null) {
return null;
}
List<String> list = new ArrayList<>();
AffineTransform transform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(transform, true, true);
AttributedCharacterIterator iterator = attributedString.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
measurer.setPosition(firstLineCharacterCount);
while (measurer.getPosition() < iterator.getEndIndex()) {
TextLayout layout = measurer.nextLayout(wrappingWidth);
String line = text.substring(measurer.getPosition() - layout.getCharacterCount(), measurer.getPosition());
list.add(line);
}
if(list.isEmpty()) {
return null;
}
return new JRBeanCollectionDataSource(list);
}
}
and test jrxml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1 -->
<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="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f9a6be97-9be4-4067-9185-a2a909bd704c">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="Dataset1" uuid="ebf3dce1-fb7a-427e-b156-4053fbbdd833">
<queryString>
<![CDATA[]]>
</queryString>
<field name="_THIS" class="java.lang.String"/>
</subDataset>
<parameter name="lorem ipsum" class="java.lang.String"/>
<parameter name="testParam" class="JRSplitText" isForPrompting="false">
<parameterDescription><![CDATA[]]></parameterDescription>
<defaultValueExpression><![CDATA[new JRSplitText($P{lorem ipsum}, "SansSerif", java.awt.Font.BOLD, 10.0f)]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="16" splitType="Stretch">
<frame>
<reportElement x="0" y="0" width="556" height="0" uuid="422c3774-9917-4908-af5d-d695eb4c0af0">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<staticText>
<reportElement x="0" y="0" width="100" height="18" uuid="258f51ab-e5f3-4c84-a483-87d03ff4adf7"/>
<textElement verticalAlignment="Bottom"/>
<text><![CDATA[This is a Label:]]></text>
</staticText>
<textField>
<reportElement x="100" y="0" width="456" height="18" uuid="aadc75c9-2599-4051-a37d-af3583ec3635"/>
<box>
<bottomPen lineWidth="1.0" lineStyle="Dotted"/>
</box>
<textElement verticalAlignment="Bottom">
<font fontName="SansSerif" size="10" isBold="true"/>
<paragraph leftIndent="2"/>
</textElement>
<textFieldExpression><![CDATA[$P{testParam}.getText(454)]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="100" y="18" width="456" height="8" uuid="059e4872-c8f0-44f7-ac32-4e7b42ce1942">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center">
<font size="6"/>
</textElement>
<text><![CDATA[(hint ...)]]></text>
</staticText>
<componentElement>
<reportElement x="0" y="18" width="556" height="18" isRemoveLineWhenBlank="true" uuid="aaa64777-42be-45ff-8045-9916e75aaf31"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="Dataset1" uuid="5f0160cd-5fef-43e9-970e-fde5226a2aeb">
<dataSourceExpression><![CDATA[$P{testParam}.getDataSource(556)]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="18" width="556">
<textField isBlankWhenNull="false">
<reportElement x="0" y="0" width="556" height="18" isRemoveLineWhenBlank="true" uuid="f0ba1742-9bae-452c-9e98-3518e0951571">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<box>
<bottomPen lineWidth="1.0" lineStyle="Dotted"/>
</box>
<textElement verticalAlignment="Bottom">
<font isBold="true"/>
<paragraph leftIndent="0"/>
</textElement>
<textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</frame>
<staticText>
<reportElement positionType="Float" x="455" y="0" width="100" height="16" forecolor="#857D7D" uuid="8bee13b8-2322-489b-8ab0-2e3c7c6742d1">
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
</reportElement>
<textElement textAlignment="Right"/>
<text><![CDATA[Static Text]]></text>
</staticText>
</band>
</columnHeader>
</jasperReport>