News Category

Link

Link (3)

Hyperlinks in PDF documents allow users to jump to pages or open documents, making PDF files more interactive and easier to use. However, if the target site of the link has been changed or the link points to the wrong page, it may cause trouble or misunderstanding to the document users. Therefore, it is very important to change or remove wrong or invalid hyperlinks in PDF documents to ensure the accuracy and usability of the hyperlinks, so as to provide a better reading experience for users. This article will introduce how to change or remove hyperlinks in PDF documents through .NET programs using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Change the URL of a Hyperlink in PDF

To change the URL of a hyperlink on a PDF page, it is necessary to get the hyperlink annotation widget and use the PdfUriAnnotationWidget.Uri property to reset the URL. The detailed steps are as follows:

  • Create an object of PdfDocument class.
  • Load a PDF file using PdfDocument.LoadFromFIle() method.
  • Get the first page of the document using PdfDocument.Pages[] property.
  • Get the first hyperlink widget on the page using PdfPageBase.AnnotationsWidget[] property.
  • Reset the URL of the hyperlink using PdfUriAnnotationWidget.Uri property.
  • Save the document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Annotations;
using System;

namespace ChangeHyperlink
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Cretae an object of PdfDocument
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file
            pdf.LoadFromFile("Sample.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Get the first hyperlink
            PdfUriAnnotationWidget url = (PdfUriAnnotationWidget)page.AnnotationsWidget[0];

            //Reset the url of the hyperlink
            url.Uri = "https://en.wikipedia.org/wiki/Climate_change";

            //Save the PDF file
            pdf.SaveToFile("ChangeHyperlink.pdf");
            pdf.Dispose();
        }
    }
}

C#/VB.NET: Change or Delete Hyperlinks in PDF

Remove Hyperlinks from PDF

Spire.PDF for .NET provides the PdfPageBase.AnnotationsWidget.RemoveAt() method to remove a hyperlink on a PDF page by its index. Eliminating all hyperlinks from a PDF document requires iterating through the pages, obtaining the annotation widgets of each page, verifying whether an annotation is an instance of the PdfUriAnnotationWidget class, and deleting the annotation if it is. The following are the detailed steps:

  • Create an object of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFIle() method.
  • To remove a specific hyperlink, get the page containing the hyperlink and remove the hyperlink by its index using PdfPageBase.AnnotationsWidget.RemoveAt() method.
  • To remove all hyperlinks, loop through the pages in the document to get the annotation collection of each page using PdfPageBase.AnnotationsWidget property.
  • Check if an annotation widget is an instance of PdfUriAnnotationWidget class and remove the annotation widget using PdfAnnotationCollection.Remove(PdfUriAnnotationWidget) method if it is.
  • Save the document using PdfDocument.SaveToFIle() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Annotations;
using System;
using System.Dynamic;

namespace DeleteHyperlink
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Cretae an object of PdfDocument
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file
            pdf.LoadFromFile("Sample.pdf");

            //Remove the second hyperlink in the fisrt page
            //PdfPageBase page = pdf.Pages[0];
            //page.AnnotationsWidget.RemoveAt(1);

            //Remove all hyperlinks in the document
            //Loop through pages in the document
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Get the annotation collection of a page
                PdfAnnotationCollection collection = page.AnnotationsWidget;
                for (int i = collection.Count - 1; i >= 0; i--)
                {
                    PdfAnnotation annotation = collection[i];
                    //Check if an annotation is an instance of PdfUriAnnotationWidget
                    if (annotation is PdfUriAnnotationWidget)
                    {
                        PdfUriAnnotationWidget url = (PdfUriAnnotationWidget)annotation;
                        //Remove the hyperlink
                        collection.Remove(url);
                    }
                }
            }

            //Save the document
            pdf.SaveToFile("DeleteHyperlink.pdf");
            pdf.Dispose();
        }
    }
}

C#/VB.NET: Change or Delete Hyperlinks in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Spire.PDF for .NET is a PDF library which enables users to handle the whole PDF document with wide range of tasks in C#, VB.NET. Spire.PDF supports to create PDF links, extract PDF links, update PDF links and remove PDF links from a PDF file. This article mainly shows how to extract and update link from a PDF file in C#.

Before the steps and codes, please check the original PDF file at first:

PDF link

Firstly we need to create a new Document object and load a PDF file which needs to extract and update the links. The links are represented as annotations in a PDF file. Before extract and update the link from a PDF file, we need to extract all the AnnotationsWidget objects.

The following code snippet shows you how to extract links from the PDF file.

//Load an existing PDF file
PdfDocument document = new PdfDocument();
document.LoadFromFile(@"..\..\output.pdf");

//Get the first page
PdfPageBase page = document.Pages[0];

//Get the annotation collection
PdfAnnotationCollection widgetCollection = page.AnnotationsWidget;

//Verify whether widgetCollection is not null or not
if (widgetCollection.Count > 0)
            {
                foreach (var obj in widgetCollection)
                {

//Get the TextWebLink Annotation
if (obj is PdfTextWebLinkAnnotationWidget)
   {
     PdfTextWebLinkAnnotationWidget link = obj as PdfTextWebLinkAnnotationWidget;

The following code snippet shows you how to update the link in PDF file

//Change the url of TextWebLink Annotation
link.Url = "http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html";

//Saves PDF document to file.
document.SaveToFile("sample.pdf");

This picture will show you the link has been updated in the PDF file.

Update link

Hyperlinks in PDF are a valuable feature that enables readers to effortlessly access a given webpage. By including hyperlinks in a PDF, it becomes easier to offer readers supplementary information about the document or direct them to relevant resources. When a reader clicks on a hyperlink, the corresponding page opens immediately in the browser. In this article, we will demonstrate how to add hyperlinks to existing text in a PDF document through .NET programs using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Insert a Hyperlink to Existing Text in PDF with C#/VB.NET

Hyperlinks in PDF documents are inserted to the page as annotation elements. Adding a hyperlink to existing text in a PDF document requires locating the text first. Once the location has been obtained, an object of PdfUriAnnotation class with the link can be created and added to the position. The detailed steps are as follows:

  • Create an object of PdfDocument class and load a PDF file using PdfDocument.LoadFromFile() method.
  • Get the first page using PdfDocument.Pages property.
  • Create an object of PdfTextFinder class and set the finder options using PdfTextFinder.Options.Parameter property.
  • Find the specified text in the page using PdfTextFinder.Find() method and get the third occurrence.
  • Loop through the text bounds of the specified occurrence (because the text being searched may span multiple lines and have more than one bound, the retrieved text bounds are stored in a list to accommodate this variability).
  • Create an object of PdfUriAnnotation class within the text bound and set the URL, border, and border color using properties under PdfUriAnnotation class.
  • Insert the hyperlink to the page annotations using PdfPageBase.AnnotationsWidget.Add(PdfUriAnnotation) method.
  • Save the PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Exporting.XPS.Schema;
using Spire.Pdf.General.Find;
using Spire.Pdf.Texts;
using System;
using System.Collections.Generic;
using System.Drawing;
using TextFindParameter = Spire.Pdf.Texts.TextFindParameter;

namespace ChangeHyperlink
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of PdfDocument class
            PdfDocument pdf = new PdfDocument();

            //Load a PDF file
            pdf.LoadFromFile("Sample.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Create an object of PdfTextFinder and set the finder options
            PdfTextFinder finder = new PdfTextFinder(page);
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            //Find the specified text in the page and get the third occurrence
            List collection = finder.Find("climate change");
            PdfTextFragment fragment = collection[2];

            //Loop through the text bounds of the specified occurrence
            foreach (RectangleF bounds in fragment.Bounds)
            {
                //Create a hyperlink annotation
                PdfUriAnnotation url = new PdfUriAnnotation(bounds);
                //Set the URL of the hyperlink
                url.Uri = "https://en.wikipedia.org/wiki/Climate_change";
                //Set the border of the hyperlink annotation
                url.Border = new PdfAnnotationBorder(1f);
                //Set the color of the border
                url.Color = Color.Blue;
                //Add the hyperlink annotation to the page
                page.AnnotationsWidget.Add(url);
            }

            //Save the PDF file
            pdf.SaveToFile("AddHyperlinks.pdf");
            pdf.Dispose();
        }
    }
}

C#/VB.NET: Insert Hyperlinks into Existing Text in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.