How to Add Hyperlink in PDF using C#

This short tutorial guides on how to add hyperlink in PDF using C#. It contains the necessary resource required to establish the environment, a detailed description of the process in the form of steps, and a runnable sample code to insert link in PDF using C#. You will also learn the process to customize the hyperlink and its text as per the requirements.

  1. Establish the environment to use Aspose.PDF for .NET to add hyperlinks
  2. Open the target PDF using the Document class object and get a reference to the desired page
  3. Create a LinkAnnotation object and set its properties including the URI before adding it to the page
  4. Create a FreeTextAnnotation object and set text to be displayed over the link
  5. Add both annotations to the page
  6. Save the resultant PDF file on the disk having a hyperlink in it

The above steps elaborate the process of how to add link in PDF using C# where first the source PDF file is loaded and its first page is accessed where the hyperlink is to be added. In the next steps, a link annotation is created within a rectangle having an invisible border and a text annotation is also created on the same location within a rectangle of the same size and similar invisible border thus overlapping the link area and text area for creating a hyperlink. In the end, both these annotations are added to the selected PDF page and the resultant PDF file is saved on the disk.

The aforementioned sample code demonstrates the process on how to create a link to a PDF using C# where LinkAnnotation object is used to create the actual link by providing the URI, settings its border along with 0 width, and the action is set to open the target page whose URL is set. Similarly, while creating FreeTextAnnotation object for displaying hyperlink text, the rectangle is defined, and appearance is set along with font and color settings. Note that a lot of other properties can also be set for these annotations.

In this topic, we have learned the process to embed link in PDF using C#. If you want to learn the process to add a header and footer in a PDF file, refer to the article on how to add header and footer in PDF using C#.

 English