Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

jasper reports - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...