使用 JasperReports 创建外部 URL 超链接



如何在链接到外部站点的PDF中包含超链接(URL)?

使用像"http://www.stackoverflow.com"这样的简单字符串,将自动生成一个链接。但是,我如何使用像 <a href="http://www.stackoverflow.com">Click here</a> 这样的 URL ?

如果我使用这个 HTML 字符串,Jaspers 会创建一个链接,但也会显示代码。

使用 JasperReports 4.0.4 和 iReport 4.5.1。

若要使 textField 成为指向外部 URL 的超链接,需要将属性hyperlinkType="Reference"添加到元素,并在其中添加 <hyperlinkReferenceExpression> 标记。引用表达式是您放置 URL 的位置。

例如:

<textField hyperlinkType="Reference" hyperlinkTarget="Blank">
    <reportElement x="5" y="5" width="200" height="15"/>
    <textElement/>
    <textFieldExpression class="java.lang.String"><![CDATA["Click Here!"]]></textFieldExpression>
    <hyperlinkReferenceExpression><![CDATA["http://www.google.com"]]></hyperlinkReferenceExpression>
</textField>

hyperlinkTarget属性的行为方式与 HTML 中的 target 属性相同。

请注意,只有文本字段、图像和图表可以以这种方式进行超链接。

由于某些原因,给出的示例不起作用。我使用了wayback机器,并找到了以下有效的代码片段:

<textField hyperlinkType="Reference">
  <reportElement x="5" y="95" width="300" height="15"/>
  <textFieldExpression class="java.lang.String">"  >> Click here to go to www.google.com"</textFieldExpression>
  <hyperlinkReferenceExpression>"http://www.google.com
</hyperlinkReferenceExpression>
    </textField>

最新更新