使用 JasperReports 创建外部 URL 超链接

问题描述 投票:0回答:3

如何在 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。

jasper-reports
3个回答
31
投票

要使文本字段成为指向外部 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
属性相同。

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


3
投票

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

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

0
投票

如果您想在常规文本使用超链接,请将

markup
设置为
html
并使用 html 标签:

<textField>
    <textElement markup="html"/>
    <textFieldExpression>
        <![CDATA["Text before hyperlink <font color='blue'><u><a href=\"http://www.stackoverflow.com\">Click here</a></u></font> Text after hyperlink"]]>
    </textFieldExpression>
</textField>
© www.soinside.com 2019 - 2024. All rights reserved.