Background color in rectangular box is missing for DOC output format

3.3k views Asked by At

I am using JasperReports API to make reports. I had made a subreport in which I put a rectangle and fill that rectangle by black color and set as background. When I download this report via pdf everything works but when I download this report as doc rectangular background color and textField contents are not shown.

Screenshots:

Pdf:enter image description here

doc:enter image description here

EDIT:

This is my template:

<?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="header" language="groovy" pageWidth="515" pageHeight="842" columnWidth="515" leftMargin="0" rightMargin="0" topMargin="10" bottomMargin="0" uuid="57cd769e-896d-4b65-9ade-a81ddccb4b4d">
<parameter name="fullName" class="java.lang.String"/>
<parameter name="phone1" class="java.lang.String"/>
<parameter name="phone2" class="java.lang.String"/>
<parameter name="email" class="java.lang.String"/>
<parameter name="address" class="java.lang.String"/>
<parameter name="userImagePathInSubReport" class="java.lang.String"/>
<queryString>
    <![CDATA[SELECT
 address.`address1` AS address_address1,
 address.`address2` AS address_address2,
 address.`city` AS address_city,
 address.`country_id` AS address_country_id,
 address.`county` AS address_county,
 address.`house_number` AS address_house_number,
 address.`state` AS address_state,
 address.`street` AS address_street,
 address.`zip` AS address_zip,
 personal_detail.`email` AS personal_detail_email,
 personal_detail.`first_name` AS personal_detail_first_name,
 personal_detail.`last_name` AS personal_detail_last_name,
 personal_detail.`phone1_id` AS personal_detail_phone1_id,
 personal_detail.`phone2_id` AS personal_detail_phone2_id,
 personal_detail.`status` AS personal_detail_status,
 personal_detail.`title` AS personal_detail_title,
 resume.`website` AS resume_website,
 resume.`career_profile` AS resume_career_profile
FROM
 `address` address,
 `personal_detail` personal_detail,
 `resume` resume]]>
</queryString>
<field name="careerProfile" class="java.lang.String"/>
<field name="website" class="java.lang.String"/>
<title>
    <band height="142" splitType="Stretch">
        <rectangle>
            <reportElement uuid="b844ceda-3ba1-40e0-9fe3-bc353c8f593a" x="0" y="0" width="503" height="137" backcolor="#000000"/>
        </rectangle>
        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
            <reportElement uuid="672155ad-36d0-41a2-9495-dd362dc90382" positionType="Float" x="147" y="47" width="121" height="20" forecolor="#FFFFFF"/>
            <textElement/>
            <textFieldExpression><![CDATA[$P{fullName}.toUpperCase()]]></textFieldExpression>
        </textField>
        <textField isBlankWhenNull="true">
            <reportElement uuid="7b0d10ef-7253-48fb-aa31-507cf75a1145" positionType="Float" x="378" y="37" width="110" height="15" forecolor="#FFFFFF" backcolor="#000000"/>
            <textElement>
                <font fontName="SansSerif" size="8"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{phone1}]]></textFieldExpression>
        </textField>
        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
            <reportElement uuid="0ab66ca4-9aa2-416c-b431-27591d5cba4f" positionType="Float" x="268" y="27" width="94" height="20" forecolor="#FFFFFF"/>
            <textElement>
                <font fontName="SansSerif" size="8"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{address}]]></textFieldExpression>
        </textField>
        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
            <reportElement uuid="b1e9d52c-c272-40fd-bf55-42cb023255da" positionType="Float" mode="Transparent" x="378" y="52" width="110" height="15" forecolor="#FFFFFF" backcolor="#FFFFFF"/>
            <textElement>
                <font fontName="SansSerif" size="8"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{email}]]></textFieldExpression>
        </textField>
        <image vAlign="Middle" isUsingCache="true">
            <reportElement uuid="1fedbec8-731c-456b-99ef-7477ca2cd933" x="23" y="12" width="99" height="113"/>
            <imageExpression><![CDATA[$P{userImagePathInSubReport}]]></imageExpression>
        </image>
        <textField isBlankWhenNull="true">
            <reportElement uuid="8a931b28-74c3-4693-ae2b-530d85469a2a" x="378" y="67" width="110" height="15" forecolor="#FFFFFF"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{website}]]></textFieldExpression>
        </textField>
    </band>
</title>

Edit: This is the code (in Grails) which i am using for generating report in doc format . My application uses JasperReports Plugin 1.6.1

def testSampleForDownloaddoc{
def result = [:]
                result.data = []

                result.data << [data1:"data1",data2:"data2"]



                params._format = "DOCX"
                params._file = "sample"                            //Set your file name this will call to sample.jrxml

                JasperReportDef report = jasperService.buildReportDefinition(params, request.getLocale(), result)
                generateReport(report)
}
def generateReport = { reportDef ->
    if (!reportDef.fileFormat.inline && !reportDef.parameters._inline) {
        response.setHeader("Content-Disposition", "inline;    filename=\"${reportDef.parameters._name ?: reportDef.name}.${reportDef.fileFormat.extension}\"");
        response.setHeader("Content-Type", "${reportDef.fileFormat.mimeTyp}");
        response.contentType = reportDef.fileFormat.mimeTyp
        response.characterEncoding = "UTF-8"
        response.outputStream << reportDef.contentStream.toByteArray()
    } else {
        render(text: reportDef.contentStream, contentType:   reportDef.fileFormat.mimeTyp, encoding: reportDef.parameters.encoding ? reportDef.parameters.encoding : 'UTF-8');
    }
}

Edit: This is the Java code which I am using:

public JasperDesign template(List<SectionOrder> SectionOrderList, List<ExtraSection> extraSectionList, Integer selectedTemplate, String applicationPath, Boolean isUserImageAvailable) throws Exception {
    //Parameters
    JRDesignParameter SUBREPORT_DIR = new JRDesignParameter();
    SUBREPORT_DIR.setName("imagePathInTemplate5");
    SUBREPORT_DIR.setValueClass(java.lang.String.class);
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("\"" + applicationPath + "/images/templateImages/template5\"");
    SUBREPORT_DIR.setDefaultValueExpression(expression);
    jasperDesign.addParameter(SUBREPORT_DIR);

    JRDesignField field;
    field = createField("personalDetail.fullName", java.lang.String.class);
    jasperDesign.addField(field);

    field = createField("personalDetail.phone1.number", java.lang.String.class);
    jasperDesign.addField(field);

    field = createField("personalDetail.phone2.number", java.lang.String.class);
    jasperDesign.addField(field);

    field = createField("personalDetail.email", java.lang.String.class);
    jasperDesign.addField(field);

    field = createField("userImagePath", String.class);
    jasperDesign.addField(field);

    field = createField("headerImage", String.class);
    jasperDesign.addField(field);

    field = createField("phoneImage", String.class);
    jasperDesign.addField(field);

    field = createField("messageImage", String.class);
    jasperDesign.addField(field);

    for (ExtraSection extraSection : extraSectionList) {
        String name = replaceDollarSign(extraSection.getTitle()).replace(" ", "_");
        System.out.println("name-------------" + name);
        field = createField(name, List.class);
        jasperDesign.addField(field);
    }

    JRDesignBand band = new JRDesignBand();
    band.setHeight(20);

    jasperDesign.setTitle(band);

    //Detail
    band = new JRDesignBand();
    int totalNumberOfSubReport = resumeSectionOrderList.size();
    int heightOfOneSubReport = 200 / totalNumberOfSubReport;
    band.setHeight(300);
    band.setSplitType(SplitTypeEnum.IMMEDIATE);
    //Header Data

    JRDesignSubreport headerInfoSubReport;
    if (isUserImageAvailable) {
        headerInfoSubReport = createSubReport(jasperDesign, 0, 0, heightOfOneSubReport, jasperDesign.getColumnWidth(), "resumeList", net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.class, applicationPath + "/report/template5/headerWithUserImage.jasper");
    }

    //User image
    JRDesignSubreportParameter userImagePathParameter = new JRDesignSubreportParameter();
    userImagePathParameter.setName("userImagePathInSubReport");
    expression = new JRDesignExpression();
    expression.setText("$F{userImagePath}");
    userImagePathParameter.setExpression(expression);
    headerInfoSubReport.addParameter(userImagePathParameter);
    //End user image

    //Full name
    JRDesignSubreportParameter fullName = new JRDesignSubreportParameter();
    fullName.setName("fullName");

    expression = new JRDesignExpression();
    expression.setValueClass(String.class);
    expression.setText("$F{personalDetail.fullName}");
    fullName.setExpression(expression);

    headerInfoSubReport.addParameter(fullName);
    band.addElement(headerInfoSubReport);
    //End of full name

    //Phone1
    JRDesignSubreportParameter phone1 = new JRDesignSubreportParameter();
    phone1.setName("phone1");

    expression = new JRDesignExpression();
    expression.setValueClass(String.class);
    expression.setText("$F{personalDetail.phone1.number}");
    phone1.setExpression(expression);

    headerInfoSubReport.addParameter(phone1);
    band.addElement(headerInfoSubReport);
    //End of phone1

    //Phone2
    JRDesignSubreportParameter phone2 = new JRDesignSubreportParameter();
    phone2.setName("phone2");

    expression = new JRDesignExpression();
    expression.setValueClass(String.class);
    expression.setText("$F{personalDetail.phone2.number}");
    phone2.setExpression(expression);

    headerInfoSubReport.addParameter(phone2);
    band.addElement(headerInfoSubReport);
    //End of phone2

    //Email
    JRDesignSubreportParameter email = new JRDesignSubreportParameter();
    email.setName("email");

    expression = new JRDesignExpression();
    expression.setValueClass(String.class);
    expression.setText("$F{personalDetail.email}");
    email.setExpression(expression);

    headerInfoSubReport.addParameter(email);
    band.addElement(headerInfoSubReport);
    //End of email

    //Address
    JRDesignSubreportParameter address = new JRDesignSubreportParameter();
    address.setName("address");

    expression = new JRDesignExpression();
    expression.setValueClass(String.class);
    expression.setText("$F{newLineAddress}");
    address.setExpression(expression);

    headerInfoSubReport.addParameter(address);
    band.addElement(headerInfoSubReport);
    //End of address

    Map<String, String> pathMap = new HashMap<String, String>();
    Map<String, String> valueMap = new HashMap<String, String>();
    for (ResumeSectionOrder resumeSectionOrder : resumeSectionOrderList) {

        if (resumeSectionOrder.getSectionName().equals("Career Objective")) {
            pathMap.put("Career Objective", applicationPath + "/report/template5 /careerObjective.jasper");
            valueMap.put("Career Objective", "resumeList");
        }
        if (resumeSectionOrder.getSectionName().equals("Personal Profile")) {
            pathMap.put("Personal Profile", applicationPath + "/report/template5 /personalStatement.jasper");
            valueMap.put("Personal Profile", "someList");
        }
    }

    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);

    //Column footer

    band = new JRDesignBand();
    jasperDesign.setColumnFooter(band);

    //Page footer

    band = new JRDesignBand();
    jasperDesign.setPageFooter(band);

    //Summary

    return jasperDesign;
}
1

There are 1 answers

18
Alex K On

I've tried, but nothing:

  • Frame element, rectangle element, textField and staticText elements with Black backcolor as container;
  • image element with black background as image;
  • Background band with black background.

The result is: nothing works. The backcolor of container is missing.

I get as result the Table in Word document without background contains texts and image like in your sample.

I think it can a bug of implementation or it can be a "feature" of implementation based on Apache POI library.

Solution with help of Html component

I've found "ugly" solution, but it works.

You can construct html page (any design you want) and use Html element to show this page.

The sample of jrxml:

<?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="background_word" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5c50ffd4-6429-4c13-83d1-86221e9aa4bc">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="fullName" class="java.lang.String">
        <defaultValueExpression><![CDATA["Bill"]]></defaultValueExpression>
    </parameter>
    <parameter name="phone1" class="java.lang.String">
        <defaultValueExpression><![CDATA["234-56-78"]]></defaultValueExpression>
    </parameter>
    <parameter name="phone2" class="java.lang.String">
        <defaultValueExpression><![CDATA["234-56-77"]]></defaultValueExpression>
    </parameter>
    <parameter name="email" class="java.lang.String">
        <defaultValueExpression><![CDATA["[email protected]"]]></defaultValueExpression>
    </parameter>
    <parameter name="address" class="java.lang.String">
        <defaultValueExpression><![CDATA["City"]]></defaultValueExpression>
    </parameter>
    <parameter name="userImagePathInSubReport" class="java.lang.String">
        <defaultValueExpression><![CDATA["image.jpg"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="careerProfile" class="java.lang.String"/>
    <field name="website" class="java.lang.String"/>
    <title>
        <band height="241" splitType="Stretch">
            <componentElement>
                <reportElement uuid="cbd45ce6-f8bf-411c-a453-24a12abe76d8" x="48" y="21" width="327" height="200"/>
                <hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="FillFrame" horizontalAlign="Left" verticalAlign="Middle">
                    <hc:htmlContentExpression><![CDATA["<table\n" +
                "style=\"text-align: left; height: 94px; background-color: black; width: 99%;\"\n" +
                "border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n" +
                "<tbody>\n" +
                "<tr>\n" +
                "<td colspan=\"1\" rowspan=\"3\"\n" +
                "style=\"vertical-align: top; background-color: black; width: 25%;\"><img\n" +
                "style=\"width: 25px; height: 31px;\" alt=\"\" src=\"" + $P{userImagePathInSubReport} + "><br>\n" +
                "</td>\n" +
                "<td style=\"vertical-align: top; background-color: black;\"><span\n" +
                "style=\"background-color: white;\">" + $P{fullName} + "</span><br>\n" +
                "</td>\n" +
                "<td style=\"vertical-align: top;\"><br>\n" +
                "</td>\n" +
                "<td style=\"vertical-align: top;\"><span\n" +
                "style=\"background-color: white;\">" + $P{phone1} + "</span></td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "<td style=\"vertical-align: top;\"><br>\n" +
                "</td>\n" +
                "<td style=\"vertical-align: top;\"><span\n" +
                "style=\"background-color: white;\">" + $P{address} + "</span></td>\n" +
                "<td style=\"vertical-align: top;\"><span\n" +
                "style=\"background-color: white;\">" + $P{email} + "</span></td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "<td style=\"vertical-align: top;\"><br>\n" +
                "</td>\n" +
                "<td style=\"vertical-align: top;\"><br>\n" +
                "</td>\n" +
                "<td style=\"vertical-align: top;\"><span\n" +
                "style=\"background-color: white;\">" + $F{website} + "</span></td>\n" +
                "</tr>\n" +
                "</tbody>\n" +
                "</table>"]]></hc:htmlContentExpression>
                </hc:html>
            </componentElement>
        </band>
    </title>
</jasperReport>

The result will be (in MS Word):

enter image description here

Note:
I've used iReport 5.2.0


You can find sample of using Html component in demo\samples\htmlcomponent\ folder of JasperReports library distribution package.


I saw that you have posted this issue on http://community.jaspersoft.com. Please, let us know if you get the help.