Add hyperlink to PDF

Manipulate PDF
6/3/2015

This code samples draws a hyperlink on an existing PDF page. The link is added as a link annotation. A URI action is associated with the mouse up event.

C# code sample

1 using (FileStream fileIn = new FileStream("in.pdf", FileMode.Open, FileAccess.Read)) 2 { 3 Document pdf = new Document(fileIn); 4 5 Page page = pdf.Pages[0]; 6 7 Link link = new Link(); 8 page.Links.Add(link); 9 10 // position link at the top-right corner 11 link.Height = 40; 12 link.Width = 100; 13 link.Left = page.Width - link.Width; 14 link.Bottom = page.Height - link.Height; 15 16 // add a UriAction to the mouse up event 17 link.MouseUpActions.Add(new UriAction("http://www.tallcomponents.com")); 18 19 using (FileStream fileOut = new FileStream("out.pdf", FileMode.Create, FileAccess.Write)) 20 { 21 pdf.Write(fileOut); 22 } 23 }

VB.NET code sample

1 Using fileIn As New FileStream("..\..\..\inputDocuments\BlueWater.pdf", FileMode.Open, FileAccess.Read) 2 Dim pdf As New Document(fileIn) 3 4 Dim page As Page = pdf.Pages(0) 5 6 Dim link As New Link() 7 page.Links.Add(link) 8 9 ' position link at the top-right corner 10 link.Height = 40 11 link.Width = 100 12 link.Left = page.Width - link.Width 13 link.Bottom = page.Height - link.Height 14 15 ' add a UriAction to the mouse up event 16 link.MouseUpActions.Add(New UriAction("http://www.tallcomponents.com")) 17 18 Using fileOut As New FileStream("..\..\out.pdf", FileMode.Create, FileAccess.Write) 19 pdf.Write(fileOut) 20 End Using 21 End Using