C#/VB.NET: Insert Hyperlinks into Existing Text in PDF

Hyperlinks in PDF are a valuable feature that enables readers to effortlessly access a given webpage. By including hyperlinks in a PDF, it becomes easier to offer readers supplementary information about the document or direct them to relevant resources. When a reader clicks on a hyperlink, the corresponding page opens immediately in the browser. In this article, we will demonstrate how to add hyperlinks to existing text in a PDF document through .NET programs using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Insert a Hyperlink to Existing Text in PDF with C#/VB.NET

Hyperlinks in PDF documents are inserted to the page as annotation elements. Adding a hyperlink to existing text in a PDF document requires locating the text first. Once the location has been obtained, an object of PdfUriAnnotation class with the link can be created and added to the position. The detailed steps are as follows:

  • Create an object of PdfDocument class and load a PDF file using PdfDocument.LoadFromFile() method.
  • Get the first page using PdfDocument.Pages property.
  • Create an object of PdfTextFinder class and set the finder options using PdfTextFinder.Options.Parameter property.
  • Find the specified text in the page using PdfTextFinder.Find() method and get the third occurrence.
  • Loop through the text bounds of the specified occurrence (because the text being searched may span multiple lines and have more than one bound, the retrieved text bounds are stored in a list to accommodate this variability).
  • Create an object of PdfUriAnnotation class within the text bound and set the URL, border, and border color using properties under PdfUriAnnotation class.
  • Insert the hyperlink to the page annotations using PdfPageBase.AnnotationsWidget.Add(PdfUriAnnotation) method.
  • Save the PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Exporting.XPS.Schema;
using Spire.Pdf.General.Find;
using Spire.Pdf.Texts;
using System;
using System.Collections.Generic;
using System.Drawing;
using TextFindParameter = Spire.Pdf.Texts.TextFindParameter;

namespace ChangeHyperlink
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of PdfDocument class
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file
            pdf.LoadFromFile("Sample.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Create an object of PdfTextFinder and set the finder options
            PdfTextFinder finder = new PdfTextFinder(page);
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            //Find the specified text in the page and get the third occurrence
            List collection = finder.Find("climate change");
            PdfTextFragment fragment = collection[2];

            //Loop through the text bounds of the specified occurrence
            foreach (RectangleF bounds in fragment.Bounds)
            {
                //Create a hyperlink annotation
                PdfUriAnnotation url = new PdfUriAnnotation(bounds);
                //Set the URL of the hyperlink
                url.Uri = "https://en.wikipedia.org/wiki/Climate_change";
                //Set the border of the hyperlink annotation
                url.Border = new PdfAnnotationBorder(1f);
                //Set the color of the border
                url.Color = Color.Blue;
                //Add the hyperlink annotation to the page
                page.AnnotationsWidget.Add(url);
            }

            //Save the PDF file
            pdf.SaveToFile("AddHyperlinks.pdf");
            pdf.Dispose();
        }
    }
}

C#/VB.NET: Insert Hyperlinks into Existing Text in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.