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:
The hyperlink integration process consists of several steps:
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 });
}
}
}
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
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: