Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide textField for specified exporter. For example for not HTML

I have a JasperReports jrxml file which has a hyperlink inside a textField element.

I do not want to show this hyperlink in anything other than the HTML view because the link does not work in excel, PDF, word, etc. and does not make sense to show.

I have read the faq with the properties but it just confused me because it does not talk about hiding a textField at all, just "bands" for headers and footers.

Here is the text field I want to hide when not HTML:

<textField hyperlinkType="ReportExecution">
    <reportElement style="Report_Param_Value_Link" mode="Opaque" x="400" y="0" width="161" height="20"/>
    <textElement/>
    <textFieldExpression class="java.lang.String"><![CDATA[Boolean.TRUE.equals($P{LAST_WEEK}) ? "View WTD" : "View last week"]]></textFieldExpression>
    <hyperlinkParameter name="noMenu">
        <hyperlinkParameterExpression><![CDATA["true"]]></hyperlinkParameterExpression>
    </hyperlinkParameter>
    <hyperlinkParameter name="reportUnit">
    <hyperlinkParameterExpression><![CDATA["repo:/Reports/Operations/Business_Support/Subreports/Business_Support_Performance_Dashboard"]]></hyperlinkParameterExpression>
    </hyperlinkParameter>
    <hyperlinkParameter name="LAST_WEEK">
        <hyperlinkParameterExpression><![CDATA[Boolean.valueOf(!Boolean.TRUE.equals($P{LAST_WEEK})).toString()]]></hyperlinkParameterExpression>
    </hyperlinkParameter>
</textField>
like image 816
Steven Avatar asked Nov 08 '11 01:11

Steven


2 Answers

Use an Element Key Filter.

The quote from JR Ultimate Guide:

This built-in filter implementations excludes from export elements that match a given element key.
Element keys are set at report design time and are propagated into generated reports.
Each element in a filled report has the same key as the element from the report template that generated it.
To trigger an element key filter, the report designer needs to define one or more report properties that start with <exporter_property_prefix>.exclude.key. Each such property matches a single element key which is to be excluded by the filter. The element key is given by the property value, or if no value is set for the property, by the property suffix.
The following example shows how to specify element keys which are to be excluded from specific export outputs:

<jasperReport ...>
    <!-- exclude elements with keys Image1 and Text4 from HTML export-->
    <property name="net.sf.jasperreports.export.html.exclude.key.Image1"/>
    <property name="net.sf.jasperreports.export.html.exclude.key.Text4"/>
    <!-- exclude elements with keys Image5 from PDF export -->
    <property name="net.sf.jasperreports.export.pdf.exclude.key.the.image" value=”Image5”/>
    ...
</jasperReport>

In your case you should add key for text field with hyperlink (for example, textFieldWithHL) and then add one property for each format (pdf, docx, xls, csv, xml, txt, odt) you want to exclude from printing this hyperlink:

<property name="net.sf.jasperreports.export.pdf.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.docx.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.xls.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.csv.exclude.key.textFieldWithHL"/>
<property name="net.sf.jasperreports.export.xml.exclude.key.textFieldWithHL"/>

The expressions from your post:

net.sf.jasperreports.export.{format}.exclude.origin.{suffix}.{arbitrary_name}
net.sf.jasperreports.export.{format}.exclude.origin.keep.first.{suffix}.{arbitrary_name}

allow to exclude the whole bands (also group bands). This filters work with JROrigin objects.


For example, consider a report with a logo that must be included as SVG for PDF output or PNG for HTML output. The JRXML file contains:

    <image scaleImage="RetainShape" onErrorType="Blank">
        <reportElement key="IMAGE_LOGO_PNG" x="1" y="0" width="100" height="60" uuid="a896cade-f6fc-4d8f-b762-29b950309257"/>
        <imageExpression><![CDATA[Transcoder.asPNG($V{V_LOGO_FILE} + ".svg")]]></imageExpression>
    </image>
    <image scaleImage="RetainShape" onErrorType="Blank">
        <reportElement key="IMAGE_LOGO_SVG" x="1" y="0" width="100" height="60" uuid="a896cade-f6fc-4d8f-b762-29b950309257"/>
        <imageExpression><![CDATA[Transcoder.asSVG($V{V_LOGO_FILE} + ".svg")]]></imageExpression>
    </image>

To exclude the SVG from HTML and the PNG from PDF, add the following properties immediately after the <jasperReport...> root element in the JRXML file:

<property name="net.sf.jasperreports.export.html.exclude.key.IMAGE_LOGO_SVG"/>
<property name="net.sf.jasperreports.export.pdf.exclude.key.IMAGE_LOGO_PNG"/>
like image 92
Alex K Avatar answered Nov 07 '22 17:11

Alex K


Looking at your source, it may be possible to create an ExporterFilter that suppresses hyperlinks, and then you have to add that filter to the export process for everything except HTML. However, I don't see why you don't want to show the hyperlink in the other formats. For many years PDF, Word, Excel, etc. will all interpret a hyperlink correctly, and in fact respond to a mouse click on the link just like a browser. That's probably why the process is going to be painful: you are reversing what for most people is expected behavior.

like image 27
Andrew Lazarus Avatar answered Nov 07 '22 16:11

Andrew Lazarus