C# tutorial: link in PDF


Link in PDF

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.

Link to a location in the current document

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";

Link to an external URL

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/");
link.SetAction(new PdfAction("http://www.worldbestlearningcenter.com/"));

links in pdf

You can download the complete C# code of this tutorial from here.


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.