Hyperlink Integration in PDFs with C# and .NET

In this article, we will look at how to integrate hyperlinks into PDF documents using C# and the SautinSoft library. PDF documents are widely used for information exchange due to their portability and consistent formatting across devices. Link annotations represent either hypertext links to other locations in the document or actions to be performed. It is represented by the PdfLinkAnnotation class. Adding hyperlinks to PDF documents increases their interactivity, makes them more user-friendly and functional.

Hyperlinks in PDF documents serve several purposes:

  • Improved navigation. Allows users to navigate to specific sections of the document or external resources.
  • Improved interactivity. Makes documents more attractive by linking to websites, email addresses, or other files.
  • Professional presentation. Hyperlinks give documents a sophisticated and modern look, making them more attractive.

The hyperlink integration process consists of several steps:

  1. Add SautinSoft.PDF from NuGet.
  2. Load a PDF Document.
  3. Create two pages.
  4. Add the text "First page" to the first page.
  5. Add an image with a hyperlink.
  6. Add the text "Open file" with a link to a local file.
  7. Add the text "Go to second page" with a transition to the second page.
  8. Add the text "Second page" to the second page.
  9. Save the document.

Complete code

using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Add hyperlinks to PDF files.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/hyperlinks.php
        /// </remarks>
        static void Main(string[] args)
        {
            // Before starting this example, please get a free trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

            string pdfFile = Path.GetFullPath(@"..\..\..\simple text.pdf");

            using (var document = new PdfDocument())
            {
                var page = document.Pages.Add();

                var secondPage = document.Pages.Add();

                var pageWidth = page.Size.Width;

                using (var formattedText = new PdfFormattedText())
                {
                    formattedText.FontSize = 24;
                    formattedText.Append("First page");
                    double y = 700;
                    var origin = new PdfPoint((pageWidth - formattedText.Width) / 2, y);
                    page.Content.DrawText(formattedText, origin);

                    var image = PdfImage.Load("..\\..\\..\\SautinSoft.png");
                    y -= image.Size.Height + 100;
                    origin = new PdfPoint((pageWidth - image.Size.Width) / 2, y);
                    page.Content.DrawImage(image, origin);

                    // Add a link annotation over the drawn image that opens a website.
                    var link = page.Annotations.AddLink(origin.X, origin.Y, image.Size.Width, image.Size.Height);
                    link.Actions.AddOpenWebLink("https://sautinsoft.com/");

                    formattedText.Clear();
                    formattedText.Append("Open file");
                    y -= formattedText.Height + 100;
                    origin = new PdfPoint((pageWidth - formattedText.Width) / 2, y);
                    page.Content.DrawText(formattedText, origin);

                    // Add a link annotation over the drawn text that opens a file.
                    link = page.Annotations.AddLink(origin.X, origin.Y, formattedText.Width, formattedText.Height);
                    link.Actions.AddOpenFile(pdfFile);

                    formattedText.Clear();
                    formattedText.Append("Go to second page");
                    y -= formattedText.Height + 100;
                    origin = new PdfPoint((pageWidth - formattedText.Width) / 2, y);
                    page.Content.DrawText(formattedText, origin);

                    // Add a link annotation over the drawn text that goes to the second page.
                    link = page.Annotations.AddLink(origin.X, origin.Y, formattedText.Width, formattedText.Height);
                    link.Actions.AddGoToPageView(secondPage, PdfDestinationViewType.FitPage);

                    formattedText.Clear();
                    formattedText.Append("Second page");
                    origin = new PdfPoint((pageWidth - formattedText.Width) / 2, 700);
                    secondPage.Content.DrawText(formattedText, origin);
                }

                document.Save("Hyperlinks.pdf");
            }

            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Hyperlinks.pdf") { UseShellExecute = true });
        }
    }
}

Download

Option Infer On

Imports System
Imports System.IO
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content

Namespace Sample
	Friend Class Sample
		''' <summary>
		''' Add hyperlinks to PDF files.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/hyperlinks.php
		''' </remarks>
		Shared Sub Main(ByVal args() As String)
			' Before starting this example, please get a free trial key:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' PdfDocument.SetLicense("...");

			Dim pdfFile As String = Path.GetFullPath("..\..\..\simple text.pdf")

			Using document = New PdfDocument()
				Dim page = document.Pages.Add()

				Dim secondPage = document.Pages.Add()

				Dim pageWidth = page.Size.Width

				Using formattedText = New PdfFormattedText()
					formattedText.FontSize = 24
					formattedText.Append("First page")
					Dim y As Double = 700
					Dim origin = New PdfPoint((pageWidth - formattedText.Width) \ 2, y)
					page.Content.DrawText(formattedText, origin)

					Dim image = PdfImage.Load("..\..\..\SautinSoft.png")
					y -= image.Size.Height + 100
					origin = New PdfPoint((pageWidth - image.Size.Width) \ 2, y)
					page.Content.DrawImage(image, origin)

					' Add a link annotation over the drawn image that opens a website.
					Dim link = page.Annotations.AddLink(origin.X, origin.Y, image.Size.Width, image.Size.Height)
					link.Actions.AddOpenWebLink("https://sautinsoft.com/")

					formattedText.Clear()
					formattedText.Append("Open file")
					y -= formattedText.Height + 100
					origin = New PdfPoint((pageWidth - formattedText.Width) \ 2, y)
					page.Content.DrawText(formattedText, origin)

					' Add a link annotation over the drawn text that opens a file.
					link = page.Annotations.AddLink(origin.X, origin.Y, formattedText.Width, formattedText.Height)
					link.Actions.AddOpenFile(pdfFile)

					formattedText.Clear()
					formattedText.Append("Go to second page")
					y -= formattedText.Height + 100
					origin = New PdfPoint((pageWidth - formattedText.Width) \ 2, y)
					page.Content.DrawText(formattedText, origin)

					' Add a link annotation over the drawn text that goes to the second page.
					link = page.Annotations.AddLink(origin.X, origin.Y, formattedText.Width, formattedText.Height)
					link.Actions.AddGoToPageView(secondPage, PdfDestinationViewType.FitPage)

					formattedText.Clear()
					formattedText.Append("Second page")
					origin = New PdfPoint((pageWidth - formattedText.Width) \ 2, 700)
					secondPage.Content.DrawText(formattedText, origin)
				End Using

				document.Save("Hyperlinks.pdf")
			End Using

			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Hyperlinks.pdf") With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.