Add a link with an internal destination to PDF

Manipulate PDF
7/17/2015

Code sample for internal links in a PDF.

In this code sample we will look at internal destinations and how you can use them. They are particularly useful in index pages, where you want to "jump" from the overview page to a specific chapter or part.

C# code sample

1 using (FileStream fileIn = new FileStream(@"..\..\..\inputDocuments\SellingYourHome.pdf", FileMode.Open, FileAccess.Read)) 2 { 3 Document document = new Document(fileIn); 4 5 //creates a new internal destination pointing to page 3 6 InternalDestination destination = new InternalDestination(); 7 destination.Page = document.Pages[2]; 8 destination.PageDisplay = PageDisplay.FitEntire; 9 10 //the action associated with the link is a go-to action 11 GoToAction action = new GoToAction(destination); 12 13 //specify link attributes 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 20 document.Pages[0].Links.Add(link); 21 22 using (FileStream fileOut = new FileStream(@"..\..\internaldestination.pdf", FileMode.Create, FileAccess.Write)) 23 { 24 document.Write(fileOut); 25 } 26 }

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 InternalDestination() 5 destination.Page = document.Pages(2) 6 destination.PageDisplay = PageDisplay.FitEntire 7 Dim action As New GoToAction(destination) 8 Dim link As New Link(100, 100, 300, 200) 9 link.BorderStyle = BorderStyle.Solid 10 link.BorderWidth = 1 11 link.BorderColor = RgbColor.Red 12 link.MouseUpActions.Add(action) 13 document.Pages(0).Links.Add(link) 14 15 Using fileOut As New FileStream("..\..\internaldestination.pdf", FileMode.Create, FileAccess.Write) 16 document.Write(fileOut) 17 End Using 18 End Using