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

2023-07-11 00:39:08 Written by  support iceblue
Rate this item
(0 votes)

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.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 11 July 2023 00:43