C

Keywords: C# Programming

Hyperlinks can connect different elements, and users can activate these links by clicking on the linked elements. It is efficient, fast and accurate. In this article, we will share the method of inserting hyperlinks into PDF documents through C # programming. The content includes the following points:

  • Insert Web Links
  • Insert external document links
  • Insert Document Page Jump Links

tool

After downloading the installation, be careful to refer Spire.Pdf.dll to the program (the dll file can be obtained in the Bin folder under the installation path)

Sample code (for reference)

[Example 1] Insert web page links

Step 1: Create an instance and add pages

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

Step 2: Define coordinate variables

float x = 10;
float y = 50;

Step 3: Create font 1 and add text to the page

//Create font 1
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true); 
//Add text to page
string text = "Note:\n The main data sources of this paper are referenced from WTO,To view the original text, please click:";
page.Canvas.DrawString(text, font1, PdfBrushes.Black, new PointF(x, y));
PdfStringFormat format = new PdfStringFormat();
format.MeasureTrailingSpaces = true;
x = x + font1.MeasureString(text, format).Width;

Step 4: Create font 2, add hyperlink text, and format it

 //Create font 2
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial Unicode MS",12f, FontStyle.Underline), true);
//Establish PdfTextWebLink object
PdfTextWebLink webLink = new PdfTextWebLink();
//Setting hyperlink address
webLink.Url = "https://www.wto.org/";
//Setting hyperlink text
webLink.Text = "WTO Official Website";
//Setting hyperlink fonts and font colors
webLink.Font = font2;
webLink.Brush = PdfBrushes.Blue;

Step 5: Add hyperlinks to pages and save documents

//Add hyperlinks to pages
webLink.DrawTextWebLink(page.Canvas, new PointF(x, y+15));

//Save document
pdf.SaveToFile("WebLink.pdf");

Page Link Effect:

All code:

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace Weblink
{
    class Program
    {
        static void Main(string[] args)
        {
            //Establish PDF Document and add a page
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            //Define coordinate variables and assign initial values
            float x = 10;
            float y = 50;

            //Create font
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true);
            //Add text to page          
            string text = "Note:\n The main data sources of this paper are referenced from WTO,To view the original text, please click:";
            page.Canvas.DrawString(text, font1, PdfBrushes.Black, new PointF(x, y));

            PdfStringFormat format = new PdfStringFormat();
            format.MeasureTrailingSpaces = true;
            x = x + font1.MeasureString(text, format).Width;

            //Create font
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Underline), true);
            //Establish PdfTextWebLink object
            PdfTextWebLink webLink = new PdfTextWebLink();
            //Setting hyperlink address
            webLink.Url = "https://www.wto.org/";
            //Setting hyperlink text
            webLink.Text = "WTO Official Website";
            //Setting hyperlink fonts and font colors
            webLink.Font = font2;
            webLink.Brush = PdfBrushes.Blue;

            //Add hyperlinks to pages
            webLink.DrawTextWebLink(page.Canvas, new PointF(x, y+15));

            //Save document
            pdf.SaveToFile("WebLink.pdf");
            System.Diagnostics.Process.Start("Weblink.pdf");
        }
    }
}

 

[Example 2] Link to external documents

Step 1: Create an instance and add pages

PdfDocument document = new PdfDocument();
PdfPageBase page = document.Pages.Add();

Step 2: Create fonts and draw hyperlinked text

//Create font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 15f, FontStyle.Regular), true);

//Add hyperlink text 
string text = "Clik and View the Original Document";
//Establish RectangleF Object and add text
RectangleF rectangle = new RectangleF(20, 40, 300,40);
page.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle);

//Establish PdfFileLinkAnnotation object 
PdfFileLinkAnnotation fileLink = new PdfFileLinkAnnotation(rectangle, @"sample.docx");
//Setting the hyperlink border color
fileLink.Color = Color.White;

Step 3: Add hyperlinks to pages and save documents

//Add hyperlinks to pages
page.AnnotationsWidget.Add(fileLink);

//Save and open documents
document.SaveToFile("ExternalFileLink.pdf");

External Document Connection Effect:

All code:

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace Filelink
{
    class Program
    {
        static void Main(string[] args)
        {
            //Establish PDF Document and add a page
            PdfDocument document = new PdfDocument();
            PdfPageBase page = document.Pages.Add();

            //Create font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 15f, FontStyle.Regular), true);

            //Add hyperlink text 
            string text = "Clik and View the Original Document";
            //Establish RectangleF Object and add text
            RectangleF rectangle = new RectangleF(20, 40, 300,40);
            page.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle);

            //Establish PdfFileLinkAnnotation object 
            PdfFileLinkAnnotation fileLink = new PdfFileLinkAnnotation(rectangle, @"sample.docx");
            //Setting the hyperlink border color
            fileLink.Color = Color.White;

            //Add hyperlinks to pages
            page.AnnotationsWidget.Add(fileLink);

            //Save and open documents
            document.SaveToFile("ExternalFileLink.pdf");
            System.Diagnostics.Process.Start("ExternalFileLink.pdf");
        }
    }
}

 

[Example 3] Insert jump links to document pages

Step 1: Create the document and add 3 pages

PdfDocument pdf = new PdfDocument();
PdfPageBase page1 = pdf.Pages.Add();
PdfPageBase page2 = pdf.Pages.Add();
PdfPageBase page3 = pdf.Pages.Add();

Step 2: Create fonts and add text to pages

//Create font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true);

//Add text to page
page1.Canvas.DrawString("(Home page)", font, PdfBrushes.Black, new PointF(20, 20));
page2.Canvas.DrawString("(Second pages)", font, PdfBrushes.Black, new PointF(20, 20));
page3.Canvas.DrawString("(Third pages)", font, PdfBrushes.Black, new PointF(20, 20));

//Create hyperlink text
string text = "Click to jump to the last page";

//Establish RectangleF Object and add text          
RectangleF rectangle = new RectangleF(40, 50, 900, 20);
page1.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle);

//Establish PdfDocumentLinkAnnotation object
PdfDocumentLinkAnnotation documentLink = new PdfDocumentLinkAnnotation(rectangle, new PdfDestination(page3));

//border-color            
documentLink.Color = Color.White;

Step 3: Add hyperlinks to pages and save documents

//Add hyperlinks to page 1
page1.AnnotationsWidget.Add(documentLink);

//Save document
pdf.SaveToFile("InternalFileLink.pdf");

Page jump link effect:

All code:

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace Documentlink
{
    class Program
    {
        static void Main(string[] args)
        {
            //Establish PDF Document and add 3 pages
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page1 = pdf.Pages.Add();
            PdfPageBase page2 = pdf.Pages.Add();
            PdfPageBase page3 = pdf.Pages.Add();

            //Create font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true);

            //Add text to page
            page1.Canvas.DrawString("(Home page)", font, PdfBrushes.Black, new PointF(20, 20));
            page2.Canvas.DrawString("(Second pages)", font, PdfBrushes.Black, new PointF(20, 20));
            page3.Canvas.DrawString("(Third pages)", font, PdfBrushes.Black, new PointF(20, 20));

            //Create hyperlink text
            string text = "Click to jump to the last page";

            //Establish RectangleF Object and add text          
            RectangleF rectangle = new RectangleF(40, 50, 900, 20);
            page1.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle);

            //Establish PdfDocumentLinkAnnotation object
            PdfDocumentLinkAnnotation documentLink = new PdfDocumentLinkAnnotation(rectangle, new PdfDestination(page3));

            //border-color            
            documentLink.Color = Color.White;

            //Add hyperlinks to page 1
            page1.AnnotationsWidget.Add(documentLink);

            //Save the document and open it
            pdf.SaveToFile("InternalFileLink.pdf");
            System.Diagnostics.Process.Start("InternalFileLink.pdf");
        }
    }
}

(End of this article)

Please indicate the source for reprinting.

Posted by irish21 on Fri, 25 Jan 2019 15:42:14 -0800