In the previous tutorial, you learnt how to add navigation page to a PDF document to make it easy for the end user to move to the first, previous, next, and last pages in a PDF document. In this tutorial, I am going to show how to create links in a PDF document to jump to a location in the current PDF document and to an external URL. You can accomplish these tasks by using Anchor and PdfAction classes.
To link to a location in the document, you will create two instances of the Anchor class. One instance is a destination location and another instance is a link to the destination. For the destination anchor, you have to set name of the anchor by using the Name property. To jump to this location, the link anchor must use the Reference property to specify the destination location. You simply assign the name of the destination anchor preceded by pound sign ( # ) to this property.
//bottom destination anchor
Anchor dest= new Anchor("destination location");
dest.Name = "dest";
//link to destbottom anchor
Anchor gotodest= new Anchor(new Chunk("Go to the destination location"));
gotodest.Reference = "#dest";
Linking to an external URL is similar to linking to a page in the PDF document. You can use the PdfAction class to specify the link and use the SetAction method to associate the link to a Chunk object. When the user clicks the link text of the Chunk object, the external URL will be displayed on the address bar of your browser.
//add a link to external url
Chunk link = new Chunk("http://www.worldbestlearningcenter.com/");
You can download the complete C# code of this tutorial from here.
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: