2012-05-21 24 views
3

我正在使用ReportLab使用Python製作pdf。我想在畫布上添加一個形狀,並將該形狀用作超鏈接。以下示例中的矩形鏈接到google.com的最簡單方法是什麼?在ReportLab中將超鏈接添加到畫布元素的最簡單方法是什麼?

from reportlab.pdfgen import canvas 
from reportlab.lib.units import inch 

c = canvas.Canvas("hello.pdf") 

# move the origin up and to the left, draw square 
c.translate(inch,9*inch) 
# How do I make this rectangle link to google.com? 
c.rect(inch,inch,1*inch,1*inch, fill=1) 

c.showPage() 
c.save() 

回答

9

呼叫linkURL畫布上:

c.linkURL('http://google.com', (inch, inch, 2*inch, 2*inch), relative=1) 

的矩形是可點擊區域,所以你必須匹配到繪製的矩形。參數是兩個座標,左下角和右上角的兩個x, y

查看更多的例子在這篇博客文章:http://www.hoboes.com/Mimsy/hacks/adding-links-to-pdf/

+0

最有可能@MartjinPieters不知道其他兩個答案突出了問題。請參閱Crebits對修正方法的回答。 –

+1

@fatih_dur:謝謝你的提醒,我確實沒有意識到其他帖子。 –

相關問題