如何在列中水平、从左到右和从上到下对齐文本字段及其标签?

How to align text fields and their labels horizontally, left to right & top to bottom in columns?

我正在使用 Jasper 报告和 iReport 并以 PDF 格式导出报告。

在我的报告中,我想添加动态的几个文本字段及其水平对齐的标签(从左到右和从上到下),就像给定的 3 个示例。

示例:所有 8 个标签都可用

Label1:   $F{value1}              Label2:    $F{value2}
Label3:   $F{value3}              Label4:    $F{value4}
Label5:   $F{value5}              Label6:    $F{value6}
Label7:   $F{value7}              Label8:    $F{value8}

示例:只有 3 个标签(标签 1、标签 4、标签 6)可用

Label1:   $F{value1}              Label4:    $F{value4}
Label6:   $F{value6}              

示例:只有 5 个标签(标签 1、标签 3、标签 5、标签 6、标签 8)可用

Label1:   $F{value1}              Label3:    $F{value3}
Label5:   $F{value5}              Label6:    $F{value6}
Label8:   $F{value8}

据我检查,我看到 ReportElement 的 positiontype 参数只是试图保存从父报告部分的 top/bottom 测量的 "Y" 偏移量。

但在我的情况下它是不同的,因为它还必须考虑 "X" 偏移量。

有人可以建议动态对齐的方法吗?

你是对的,你只能 move/set y 位置的元素与位置类型。 "quickest" 我想出的实现布局的方法是使用具有 2 列和水平打印顺序的子报表,但这意味着您需要为子报表创建数据源 ("label1",$F{value1},"label2",$F{value2}...) 我将向您展示一些快速代码

  1. 创建一个简单的 class 来保留标签和值。

    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;
      }
    }
    
  2. 创建生成 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);
    }
    
  3. 创建一个包含 2 列的子报表 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>
    
  4. 使用您的标签和字段调用子报表

    <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>
    

如果 value2、value3、value5 为 null

的结果