Insert Hyperlink in PDF in C#/VB.NET

PDF Hyperlink replaces huge information with a simple link which satisfies people the need of rendering external information while not occupying much space. Now, it is time to introduce a solution to insert hyperlink in PDF with C#, VB.NET.

Using this .NET PDF component Spire.PDF for .NET, you can not only to insert hyperlink in your PDF file but also set hyperlink format and position to perfectly match the PDF page. First please see below picture as the target PDF file in which there are two hyperlinks are added. Both hyperlinks can automatically take you to a new place:

Insert Hyperlink in PDF

There are two main steps to insert PDF hyperlink. One is to set the position of hyperlink. The other is to insert hyperlinks. Let us see them one by one.

Step 1: Set PDF hyperlink position

When we set PDF hyperlink position, we should make full use of the class of Spire.Pdf: PdfPageBase.Canvas.ClientSize. You can see detail information as below code:

            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Verdana", 17));
            float space = font.Height * 0.75f;
            float y = page.Canvas.ClientSize.Height * 0.13f - margin.Bottom + space;
Dim unitCvtr As New PdfUnitConvertor()
Dim margin As New PdfMargins()
margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Bottom = margin.Top
margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Right = margin.Left
Dim font As New PdfTrueTypeFont(New Font("Verdana", 17))
Dim space As Single = font.Height * 0.75F
Dim y As Single = page.Canvas.ClientSize.Height * 0.13F - margin.Bottom + space

Step 2: Insert PDF hyperlink and set hyperlink format

There are two kinds of hyperlinks will be added in this step: url link and text link. When we insert url link, we need to assign value for url string.While when we want to insert a text hyperlink we should call DrawTextWebLink () method. If necessary, we also can add label. After adding hyperlinks, PdfPageBase.Canvas.DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y); method can help us set hyperlink format with six parameters passed.

            String label = "File Source: ";
            PdfStringFormat format = new PdfStringFormat();
            format.MeasureTrailingSpaces = true;
            float x1 = font.MeasureString(label, format).Width;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Verdana", 17, System.Drawing.FontStyle.Bold));
            String url1 = "";
            float x2 = font2.MeasureString(url1, format).Width;
            float x = (page.Canvas.ClientSize.Width - x1 - x2) / 2;
            page.Canvas.DrawString(label, font, PdfBrushes.Firebrick, x, y, format);
            x += x1;
            page.Canvas.DrawString(url1, font2, PdfBrushes.DarkSeaGreen, x, y);
            y = y + font2.MeasureString(url1).Height;

            label = "Simple Link: ";
            x -= x1;
            page.Canvas.DrawString(label, font, PdfBrushes.Firebrick, x, y, format);
            float xoffset2 = font.MeasureString(label, format).Width;
            x += xoffset2;
            String text = "microsoft";
            PdfTextWebLink link2 = new PdfTextWebLink();
            link2.Text = text;
            link2.Url = url1;
            link2.Font = font2;
            link2.Brush = PdfBrushes.DarkSeaGreen;
            link2.DrawTextWebLink(page.Canvas, new PointF(x, y));
Dim label As [String] = "File Source: "
Dim format As New PdfStringFormat()
format.MeasureTrailingSpaces = True
Dim x1 As Single = font.MeasureString(label, format).Width
Dim font2 As New PdfTrueTypeFont(New Font("Verdana", 17, System.Drawing.FontStyle.Bold))
Dim url1 As [String] = ""
Dim x2 As Single = font2.MeasureString(url1, format).Width
Dim x As Single = (page.Canvas.ClientSize.Width - x1 - x2) / 2
page.Canvas.DrawString(label, font, PdfBrushes.Firebrick, x, y, format)
x += x1
page.Canvas.DrawString(url1, font2, PdfBrushes.DarkSeaGreen, x, y)
y = y + font2.MeasureString(url1).Height

label = "Simple Link: "
x -= x1
page.Canvas.DrawString(label, font, PdfBrushes.Firebrick, x, y, format)
Dim xoffset2 As Single = font.MeasureString(label, format).Width
x += xoffset2
Dim text As [String] = "microsoft"
Dim link2 As New PdfTextWebLink()
link2.Text = text
link2.Url = url1
link2.Font = font2
link2.Brush = PdfBrushes.DarkSeaGreen
link2.DrawTextWebLink(page.Canvas, New PointF(x, y))

Spire.PDF for .NET is a PDF application which enables users to handle the whole PDF document with wide range of tasks in C#, VB.NET.