How to Insert Hyperlink in PDF – C#

Sometimes, hyperlink becomes an essential element in a document, for example, when we need to show references or related contents. There are two ways to insert hyperlink. One is to insert website url directly in document. The other is to add hyperlink for text. We can link key words to website by adding hyperlink. In this post, I will show how to insert hyperlink in a PDF document with C#.

In this example, I will show three hyperlinks in document, url directly, text hyperlink and annotation hyperlink. Also, I use Spire.PDF in my method. Therefore, I use its dll file as reference.

The following shows details about how to insert hyperlink in PDF.

  1. Create a PDF document and set margin. Then, add one page to write text.
  2. Insert hyperlink. Firstly, we insert website url directly. Set hyperlink location and format. Then, write a cue word to explain which this url is about. Secondly, add hyperlink for text. Also, add cue word and draw it with format. Set link text and url and then draw. Thirdly, insert url annotation. Add cue word and draw as previous. However, I need to set this link type as PdfUrlAnnotation. Then, draw hyperlink.
  3. Save and launch file.

Main Coding:

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

using Spire.Pdf.Annotations;

 

namespace InsertHyperlink

{

    class link

    {

        static void Main(string[] args)

        {

            //Create

            PdfDocument doc = new PdfDocument();

 

            //Margin

            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;

 

            //Add Page

            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

 

            //Insert Link

            float y = 20;

            float x = 0;

            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(“Impact”, 12));

            String label = “Google:   “;

            PdfStringFormat format = new PdfStringFormat();

            format.MeasureTrailingSpaces = true;

            page.Canvas.DrawString(label, font, PdfBrushes.DarkOrange, 0, y, format);

            x = font.MeasureString(label, format).Width;

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font(“Calibri”, 16, FontStyle.Underline));

            String url1 = http://www.google.com”;

            page.Canvas.DrawString(url1, font1, PdfBrushes.DeepSkyBlue, x, y);

            y = y + font1.MeasureString(url1).Height;

 

            label = “My Blog:   “;

            page.Canvas.DrawString(label, font, PdfBrushes.DarkOrange, 0, y, format);

            x = font.MeasureString(label, format).Width;

            String text = “Janewdaisy”;

            PdfTextWebLink link2 = new PdfTextWebLink();

            link2.Text = text;

            link2.Url = https://janewdaisy.wordpress.com”;

            link2.Font = font1;

            link2.Brush = PdfBrushes.DeepSkyBlue;

            link2.DrawTextWebLink(page.Canvas, new PointF(x, y));

            y = y + font1.MeasureString(text).Height;

 

            label = “URI Annotation:   “;

            page.Canvas.DrawString(label, font, PdfBrushes.DarkOrange, 0, y, format);

            x = font.MeasureString(label, format).Width;

            text = “Yahoo”;

            PointF location = new PointF(x, y);

            SizeF size = font1.MeasureString(text);

            RectangleF linkBounds = new RectangleF(location, size);

            PdfUriAnnotation link3 = new PdfUriAnnotation(linkBounds);

            link3.Border = new PdfAnnotationBorder(0);

            link3.Uri = http://www.yahoo.com”;

            (page as PdfNewPage).Annotations.Add(link3);

            page.Canvas.DrawString(text, font1, PdfBrushes.DeepSkyBlue, x, y);

            y = y + size.Height;

 

            //Save

            doc.SaveToFile(“Link.pdf”);

            doc.Close();

 

            //Launch

            System.Diagnostics.Process.Start(“Link.pdf”);

        }

    }

}

Result Shown by Following:

__________________________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Advertisements

One thought on “How to Insert Hyperlink in PDF – C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s