Reputation: 909
I'm using Jasper report and iReport and exporting the report in PDF format.
In my report I want to add dynamic few text fields along with their labels aligned horizontally (left to right & top to bottom) like the given 3 Examples.
Example : All the 8 labels are available
Label1: $F{value1} Label2: $F{value2}
Label3: $F{value3} Label4: $F{value4}
Label5: $F{value5} Label6: $F{value6}
Label7: $F{value7} Label8: $F{value8}
Example : Only 3 labels (Label 1, Label 4, Label 6) are available
Label1: $F{value1} Label4: $F{value4}
Label6: $F{value6}
Example : Only 5 labels (Label 1, Label 3, Label 5, Label 6, Label 8) are available
Label1: $F{value1} Label3: $F{value3}
Label5: $F{value5} Label6: $F{value6}
Label8: $F{value8}
As far as I checked I see the positiontype argument of ReportElement just tries to conserve the "Y" offset measured from top/bottom of the parent report section.
But in my case it is different as it has to consider the "X" offset as well.
Could anyone please suggest the way to dynamically align this?
Upvotes: 1
Views: 3136
Reputation: 21710
You are right you can only move/set elements in y position with Position Type. The "quickest" way I can come up with to achieve your layout is using a sub report with 2 columns and horizontal print order, but this means that you need to create a datasource for the subreport ("label1",$F{value1},"label2",$F{value2}...
) I will show you some quick code
Create a simple class to keep the label and the values.
public class LabelValue {
private final String label;
private final String value;
public LabelValue(String label, String value) {
this.label = label;
this.value = value;
}
public String getLabel() {
return label;
}
public String getValue() {
return value;
}
}
Create a method call that generates the JRDatasource
public static JRDataSource getDatasource(String... fieldsValues) {
List<LabelValue> vList = new ArrayList<>();
for (int i = 0; i < fieldsValues.length-1; i=i+2) {
//add to datasource only if value (2nd param is != null)
if (fieldsValues[i+1]!=null) {
vList.add(new LabelValue(fieldsValues[i],fieldsValues[i+1]));
}
}
return new JRBeanCollectionDataSource(vList);
}
Create a subreport with 2 columns and printOrder="Horizontal"
<?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="subReportColonne" columnCount="2" printOrder="Horizontal" pageWidth="555" pageHeight="842" whenNoDataType="BlankPage" columnWidth="277" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="597c0716-df6b-42ec-a7c8-863eb1b7174a">
<field name="label" class="java.lang.String"/>
<field name="value" class="java.lang.String"/>
<detail>
<band height="20" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<textField>
<reportElement x="0" y="0" width="130" height="20" uuid="34bf2bee-16f1-49b6-bfe5-a82ac94e6086">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box leftPadding="3"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{label}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="130" y="0" width="140" height="20" uuid="58d18846-5a61-4a56-b9d8-6ac0ac499510">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box leftPadding="3"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Call the subreport with your labels and fields
<subreport>
<reportElement x="0" y="0" width="530" height="58" uuid="4e3cde2d-02a3-4929-8dda-0cf9d4c20dc1"/>
<dataSourceExpression><![CDATA[my.package.DatasourceProvider.getDatasource(new String[]{"label1",$F{value1},"label2",$F{value2},"label3", $F{value3}, "label4",$F{value4},"label5",$F{value5},"label6", $F{value6}})]]></dataSourceExpression>
<subreportExpression><![CDATA["C:\\...\\the_subreport.jasper"]]></subreportExpression>
</subreport>
Result if value2, value3, value5 is null
Upvotes: 1