0

我在不使用jrxml模板的情况下创建JR报告。我需要知道如何使用Java代码设置报告标题中的图像?

  
   ...
  //Title
    band = new JRDesignBand();
    band.setHeight(50);
    textField = new JRDesignTextField();
    textField.setBlankWhenNull(true);
    textField.setX(0);
    textField.setY(10);
    textField.setWidth(515);
    textField.setHeight(30);
    textField.setHorizontalAlignment(HorizontalAlignEnum.CENTER);
    textField.setStyle(normalStyle);
    textField.setFontSize(22);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$P{ReportTitle}");
    textField.setExpression(expression);
    band.addElement(textField);
    jasperDesign.setTitle(band);

在这里,我需要添加图像而不是文本字段。我应该怎么办?

4

2 回答 2

3

您可以使用JRDesignImage类来设置图像。

样本:

public JasperDesign getJasperDesign() throws JRException {
    JasperDesign jasperDesign = new JasperDesign();
    jasperDesign.setName(DESIGN_NAME);
    jasperDesign.setPageWidth(595);
    jasperDesign.setPageHeight(842);
    jasperDesign.setColumnWidth(515);
    jasperDesign.setColumnSpacing(0);
    jasperDesign.setLeftMargin(40);
    jasperDesign.setRightMargin(40);
    jasperDesign.setTopMargin(50);
    jasperDesign.setBottomMargin(50);

    JRDesignParameter parameter = new JRDesignParameter();
    parameter.setName("ReportTitle");
    parameter.setValueClass(java.lang.String.class);
    jasperDesign.addParameter(parameter);

    //Title band
    JRDesignBand band = new JRDesignBand();
    band.setHeight(250);

    JRDesignTextField textField = new JRDesignTextField();
    textField.setBlankWhenNull(true);
    textField.setX(0);
    textField.setY(10);
    textField.setWidth(515);
    textField.setHeight(30);
    textField.setHorizontalAlignment(HorizontalAlignEnum.CENTER);
    textField.setFontSize(22);
    JRDesignExpression expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$P{ReportTitle}");
    textField.setExpression(expression);
    band.addElement(textField);

    // Image
    String imgPath = "\"dukesign.jpg\"";

    expression = new JRDesignExpression();
    expression.setText(imgPath);

    JRDesignImage image = new JRDesignImage(jasperDesign);
    image.setX(45);
    image.setY(55);
    image.setWidth(130);
    image.setHeight(104);
    image.setScaleImage(ScaleImageEnum.FILL_FRAME);
    image.setExpression(expression);

    // Add image to Detail band
    band.addElement(image);

    jasperDesign.setTitle(band);

    return jasperDesign;
}

结果将是(生成的pdf文件):

生成的pdf文件

于 2013-07-22T20:48:21.117 回答
-1

我用一个列表组件和一个数据集来做这样的事情

<title>
    <band height="70" splitType="Stretch">
        <staticText>
            <reportElement uuid="30b7421d-058a-478d-9179-901273499bc4" x="74" y="0" width="420" height="55"/>
            <textElement textAlignment="Center" markup="html">
                <font size="18"/>
            </textElement>
            <text><![CDATA[Report Header ]]></text>
        </staticText>
        <componentElement>
            <reportElement uuid="f1f3b8ab-5dbf-4139-8033-d048a7e9896d" x="0" y="0" width="74" height="70"/>
            <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="Agency_Image" uuid="6c1c4672-d9c2-4968-9265-0918ce1b8e91">
                    <datasetParameter name="USER_AGENCY_CODE">
                        <datasetParameterExpression><![CDATA[$P{USER_AGENCY_CODE}]]></datasetParameterExpression>
                    </datasetParameter>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                </datasetRun>
                <jr:listContents height="70" width="74">
                    <image onErrorType="Blank">
                        <reportElement uuid="1c56b92b-ea0c-4f16-8402-df13d3f01317" x="0" y="0" width="46" height="46"/>
                        <imageExpression><![CDATA[$F{AGENCY_IMAGE}]]></imageExpression>
                    </image>
                </jr:listContents>
            </jr:list>
        </componentElement>
        <textField>
            <reportElement uuid="9d151874-7f50-4987-a89f-a183dfe744cd" x="74" y="55" width="420" height="15"/>
            <textElement textAlignment="Center">
                <font isBold="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{AGENCY_NAME}]]></textFieldExpression>
        </textField>
    </band>
</title>
于 2013-07-22T19:21:59.943 回答