Add a link to PDF with an external destination

Manipulate PDF
7/17/2015

Code sample that adds an external link to your PDF

If your PDF refers to a webpage, or to another external document it is important to be able to add an external links to your PDF. This article shows how you can add a link with an external destination; this can be any document or an URL.

C# code sample

1 using ( FileStream fileIn = new FileStream( 2 @"..\..\..\inputDocuments\SellingYourHome.pdf", 3 FileMode.Open, FileAccess.Read ) ) 4 { 5 Document document = new Document( fileIn ); 6 7 RemoteDestination destination = new RemoteDestination(); 8 destination.Path = @"..\inputDocuments\PackingLightBrochure.pdf"; 9 destination.PageIndex = 2; // third page 10 destination.PageDisplay = PageDisplay.FitEntire; 11 destination.WindowBehavior = WindowBehavior.NewWindow; 12 13 GoToAction action = new GoToAction( destination ); 14 Link link = new Link( 100, 100, 300, 200 ); 15 link.BorderStyle = BorderStyle.Solid; 16 link.BorderWidth = 1; 17 link.BorderColor = RgbColor.Red; 18 link.MouseUpActions.Add( action ); 19 document.Pages[0].Links.Add( link ); 20 21 using ( FileStream fileOut = new FileStream( 22 @"..\..\remotedestination.pdf", 23 FileMode.Create, FileAccess.Write ) ) 24 { 25 document.Write( fileOut ); 26 } 27 }

VB.NET code sample

1 Using fileIn As New FileStream("..\..\..\inputDocuments\SellingYourHome.pdf", FileMode.Open, FileAccess.Read) 2 Dim document As New Document(fileIn) 3 4 Dim destination As New RemoteDestination() 5 destination.Path = "..\inputDocuments\PackingLightBrochure.pdf" 6 destination.PageIndex = 2 7 ' third page 8 destination.PageDisplay = PageDisplay.FitEntire 9 destination.WindowBehavior = WindowBehavior.NewWindow 10 11 Dim action As New GoToAction(destination) 12 Dim link As New Link(100, 100, 300, 200) 13 link.BorderStyle = BorderStyle.Solid 14 link.BorderWidth = 1 15 link.BorderColor = RgbColor.Red 16 link.MouseUpActions.Add(action) 17 document.Pages(0).Links.Add(link) 18 19 Using fileOut As New FileStream("..\..\remotedestination.pdf", FileMode.Create, FileAccess.Write) 20 document.Write(fileOut) 21 End Using 22 End Using