Tom Sebastian
Tom Sebastian

Reputation: 3433

How to set image in jasper title programmatically

I am creating JR report without using jrxml template. I need to know how can we set how image in the report title with Java code?

  
   ...
  //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);

Here instead of text field I need to add an image. What should I do?

Upvotes: 0

Views: 2472

Answers (2)

Alex K
Alex K

Reputation: 22867

You can use JRDesignImage class for setting image.

The sample:

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

The result will be (the generated pdf file):

Resulted pdf file

Upvotes: 3

Mike Noland
Mike Noland

Reputation: 505

I something like this with a List Component and a dataset

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

Upvotes: -1

Related Questions