Add a link with an internal destination to PDF

Add a link with an internal destination to PDF

This code sample helps with 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.

using (FileStream fileIn = new FileStream(@"..\..\..\inputDocuments\SellingYourHome.pdf", FileMode.Open, FileAccess.Read))
{
    Document document = new Document(fileIn);

    //creates a new internal destination pointing to page 3
    InternalDestination destination = new InternalDestination();
    destination.Page = document.Pages[2];
    destination.PageDisplay = PageDisplay.FitEntire;

    //the action associated with the link is a go-to action
    GoToAction action = new GoToAction(destination);

    //specify link attributes
    Link link = new Link(100, 100, 300, 200);
    link.BorderStyle = BorderStyle.Solid;
    link.BorderWidth = 1;
    link.BorderColor = RgbColor.Red;
    link.MouseUpActions.Add(action);

    document.Pages[0].Links.Add(link);

    using (FileStream fileOut = new FileStream(@"..\..\internaldestination.pdf", FileMode.Create, FileAccess.Write))
    {
        document.Write(fileOut);
    }
}
        Using fileIn As New FileStream("..\..\..\inputDocuments\SellingYourHome.pdf", FileMode.Open, FileAccess.Read)
	Dim document As New Document(fileIn)

	Dim destination As New InternalDestination()
	destination.Page = document.Pages(2)
	destination.PageDisplay = PageDisplay.FitEntire
	Dim action As New GoToAction(destination)
	Dim link As New Link(100, 100, 300, 200)
	link.BorderStyle = BorderStyle.Solid
	link.BorderWidth = 1
	link.BorderColor = RgbColor.Red
	link.MouseUpActions.Add(action)
	document.Pages(0).Links.Add(link)

    Using fileOut As New FileStream("..\..\internaldestination.pdf", FileMode.Create, FileAccess.Write)
    document.Write(fileOut)
    End Using
End Using