2012-06-28 20 views
16

如何在鏈接到外部網站的PDF中包含超鏈接(URL)?使用JasperReports創建外部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。

回答

25

要使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屬性。

請注意,只有textFields,圖像和圖表可以用這種方式超鏈接。

有關更深入的解釋和更多示例,請參見this jasperforge page

1

由於某些原因,給出的示例不起作用。我用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>