TL;DR: Learn to manage hyperlinks in PDF documents programmatically with C#. This guide covers adding, updating, and removing links in PDFs using Syncfusion’s .NET PDF Library to streamline your document navigation and interactivity.
Hyperlinks are a vital component of PDFs, allowing users to quickly navigate to other parts of the document, external websites, or other files. Whether you’re creating reports, eBooks, or documentation, embedding hyperlinks enhances your PDF files’ interactivity and overall user experience. However, as documents evolve, there may come a need to manage these hyperlinks programmatically—whether by adding new ones, updating outdated links, or removing them entirely.
This blog will explore how to effortlessly add, update, and remove hyperlinks in PDFs using the Syncfusion .NET PDF Library. This powerful tool allows developers to manipulate PDFs in C#, providing the ability to easily manage hyperlinks programmatically. Whether you’re looking to add new links, modify existing ones, or remove outdated references, this guide will walk you through the steps to streamline your PDF workflow while keeping your documents dynamic and up to date.
Agenda:
- Adding hyperlinks (external URLs) to the PDF
- Inserting hyperlinks into an existing PDF file
- Creating internal navigation links within a PDF
- Navigating external documents from a PDF
- Navigating specific pages in an external PDF file
- Modifying the existing hyperlinks in a PDF
- Removing hyperlinks from a PDF document
Let’s get started!
Getting started with app creation
Step 1: Create a .NET Core Console app
Open Visual Studio, select Create a New Project from the Start Page or File Menu, choose Console App (.NET Core), and follow the setup instructions.
Step 2: Open the NuGet Package Manager Console
Now, navigate to Visual Studio’s Tools menu, select NuGet Package Manager, and open the Package Manager Console.
Step 3: Install the Syncfusion PDF NuGet Package
Run the following command to install the Syncfusion.Pdf.Net.Core package in the Package Manager Console.
Install-Package Syncfusion.Pdf.Net.Core
Adding hyperlinks (external URLs) to the PDF
Adding hyperlinks to external URLs in a PDF enhances the user experience by providing quick access to external resources, such as websites or files.
With the .NET PDF Library, you can programmatically add these external links to your PDF documents. This process allows for seamless navigation to external content, ensuring your PDF stays dynamic and informative.
The PdfTextWebLink/ PdfUriAnnotation class helps us to insert hyperlinks to external URLs in a PDF document.
Step 1: Create a new PDF document
Begin by creating a new instance of the PdfDocument class, which will serve as the PDF file where the hyperlink will be added.
//Create a new PDF document
using (PdfDocument document = new PdfDocument())
Step 2: Add a page to the PDF
We should create a page in the PDF document to add a hyperlink. This page will host the hyperlink.
//Add a page to the document
PdfPage page = document.Pages.Add();
Step 3: Create the hyperlink
Create a PdfTextWebLink or PdfUriAnnotation class to represent the hyperlink visually. This will allow users to click on a particular area or text within the document to navigate to the external URL.
// Create text web link
PdfTextWebLink textWebLink = new PdfTextWebLink()
{
// Set the text to display
Text = "Click here to access Syncfusion .NET components and controls",
// Set the hyperlink
Url = "https://www.syncfusion.com/",
// Set brush
Brush = PdfBrushes.Blue,
// Create font and set to the text web link
Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12)
};
// Draw the text web link
textWebLink.DrawTextWebLink(page, new PointF(10, 50));
Step 4: Save the PDF with hyperlinks
Once the link is added to the document, save the PDF to a FileStream or MemoryStream.
// Save the PDF document
using (FileStream outputStream = new FileStream("hyperlink-pdf.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream);
}
Here’s the complete code example for adding an external URL hyperlink to a PDF document.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
// Create a new PDF document
using (PdfDocument document = new PdfDocument())
{
// Add a page to the document
PdfPage page = document.Pages.Add();
// Create text web link
PdfTextWebLink textWebLink = new PdfTextWebLink()
{
// Set the text to display
Text = "Click here to access Syncfusion .NET components and controls",
// Set the hyperlink
Url = "https://www.syncfusion.com/",
// Set brush
Brush = PdfBrushes.Blue,
// Create font and set to the text web link
Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12)
};
// Draw the text web link
textWebLink.DrawTextWebLink(page, new PointF(10, 50));
// Save the PDF document
using (FileStream outputStream = new FileStream("hyperlink-pdf.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream);
}
}
After executing the above code, a PDF file will be generated with a clickable text link that navigates to an external URL.
Inserting hyperlinks into an existing PDF file
Inserting hyperlinks into an existing PDF file enhances its interactivity by providing users with direct access to external websites or specific sections within the document.
Using the .NET PDF Library, you can programmatically add hyperlinks to an existing PDF, ensuring your content remains up-to-date and functional.
The PdfLoadedDocument class allows you to open an existing PDF document, and you can use the PdfUriAnnotation class to create and add hyperlinks as an annotation.
Step 1: Load the existing PDF document
Begin by creating an instance of the PdfLoadedDocument class to open the existing PDF file you want to modify.
// Read the PDF file as stream
using (FileStream inputPdfFileStream = new FileStream("data/input.pdf", FileMode.Open, FileAccess.Read))
{
// Load the PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPdfFileStream))
Step 2: Access the page where you want to insert the hyperlink
Identify the page where you wish to insert the hyperlink in the PDF document.
// Access the desired page (e.g., the second page)
PdfPageBase page = loadedDocument.Pages[1];
Step 3: Create the hyperlink annotation
Use the PdfUriAnnotation class to create a hyperlink annotation on the specified page, defining the location where the link will be placed.
In this example, we will locate existing text within the PDF and add a hyperlink at the exact position of that text.
// Find the word and add link based on the word bounds
List<RectangleF> matchedItems;
loadedDocument.FindText("PDF Succinctly", 1, out matchedItems);
// Create a PdfUriAnnotation object to place the URI link in the first image bounds
PdfUriAnnotation pdfUriAnnotation = new PdfUriAnnotation(matchedItems[0], "https://www.syncfusion.com/succinctly-free-ebooks/pdf")
{
// Set the border (Empty border)
Border = new PdfAnnotationBorder(1),
Color = new PdfColor(Color.Blue)
};
// Add the annotation to the page
page.Annotations.Add(pdfUriAnnotation);
Step 4: Save the modified PDF document
After adding the hyperlink, save the changes to the PDF document.
// Save the PDF document
using (FileStream outputStream = new FileStream("insert-link-into-existing-pdf.pdf", FileMode.Create, FileAccess.Write))
{
loadedDocument.Save(outputStream);
}
Here’s the complete code example for inserting hyperlinks into an existing PDF document.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
// Read the PDF file as stream
using (FileStream inputPdfFileStream = new FileStream("data/input.pdf", FileMode.Open, FileAccess.Read))
{
// Load the PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPdfFileStream))
{
// Access the desired page (e.g., the second page)
PdfPageBase page = loadedDocument.Pages[1];
// Find the word and add link based on the word bounds
List<RectangleF> matchedItems;
loadedDocument.FindText("PDF Succinctly", 1, out matchedItems);
//Create a PdfUriAnnotation object to place the URI link in the first image bounds
PdfUriAnnotation pdfUriAnnotation = new PdfUriAnnotation(matchedItems[0], "https://www.syncfusion.com/succinctly-free-ebooks/pdf")
{
// Set the border (Empty border)
Border = new PdfAnnotationBorder(1),
Color = new PdfColor(Color.Blue)
};
// Add the annotation to the page
page.Annotations.Add(pdfUriAnnotation);
// Save the PDF document
using (FileStream outputStream = new FileStream("insert-link-into-existing-pdf.pdf", FileMode.Create, FileAccess.Write))
{
loadedDocument.Save(outputStream);
}
}
}
After executing this code, the specified PDF file will be updated to include a hyperlink, allowing users to click on it and navigate to the designated external URL. This feature enhances the document’s functionality and user engagement, making it more interactive.
Creating internal navigation links within a PDF
Internal navigation links within a PDF document enable users to quickly jump to specific sections or pages, enhancing the overall readability and user experience.
The PdfDestination class creates destinations for internal links, while the PdfDocumentLinkAnnotation class defines the link annotations.
Step 1: Create a new PDF document
Create a new instance of the PdfDocument class, which will be the PDF file containing the internal navigation links.
//Create a PDF document
using (PdfDocument document = new PdfDocument())
Step 2: Add pages to the PDF
Create multiple pages in the PDF document where you want to link from and to. For the demonstration, we will create two pages.
// Add the first page to the document
PdfPage firstPage = document.Pages.Add();
// Create font to draw text on the page
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
//Draw text on the page.
firstPage.Graphics.DrawString("This is the first page.", font, PdfBrushes.Black, new PointF(10, 10));
// Add the second page to the document
PdfPage secondPage = document.Pages.Add();
secondPage.Graphics.DrawString("This is the second page.", font, PdfBrushes.Black, new PointF(10, 10));
Step 3: Define the destination for the internal link
Set up a destination for the internal link. This tells the PDF where to navigate when the link is clicked.
// Define a destination for the second page
PdfDestination destination = new PdfDestination(secondPage, new PointF(0, 0))
{
Mode = PdfDestinationMode.FitH, // Fit the page horizontally
};
Step 4: Create the internal link annotation
Let’s use the PdfDocumentLinkAnnotation class to create the link annotation on the first page, linking it to our defined destination.
// Create a link annotation on the first page
PdfDocumentLinkAnnotation linkAnnotation = new PdfDocumentLinkAnnotation(new RectangleF(125, 10, 200, 20));
//Set destination for the link annotation
linkAnnotation.Destination = destination;
//Set the border for the link annotation
linkAnnotation.Border = new PdfAnnotationBorder(0, 0, 0); // No border
// Add the link annotation to the first page
firstPage.Annotations.Add(linkAnnotation);
Step 5: Save the PDF with internal navigation links
Once the link is created, save the PDF document to a FileStream or MemoryStream.
// Save the PDF document
using (FileStream outputStream = new FileStream("internal-navigation.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream);
}
Here’s the complete code for creating internal navigation links within a PDF document.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
//Create a PDF document
using (PdfDocument document = new PdfDocument())
{
// Add the first page to the document
PdfPage firstPage = document.Pages.Add();
// Create font to draw text on the page
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
//Draw text on the page.
firstPage.Graphics.DrawString("This is the first page.", font, PdfBrushes.Black, new PointF(10, 10));
// Add the second page to the document
PdfPage secondPage = document.Pages.Add();
secondPage.Graphics.DrawString("This is the second page.", font, PdfBrushes.Black, new PointF(10, 10));
// Define a destination for the second page
PdfDestination destination = new PdfDestination(secondPage, new PointF(0, 0))
{
Mode = PdfDestinationMode.FitH, // Fit the page horizontally
};
// Create a link annotation on the first page
PdfDocumentLinkAnnotation linkAnnotation = new PdfDocumentLinkAnnotation(new RectangleF(125, 10, 200, 20));
//Set destination for the link annotation
linkAnnotation.Destination = destination;
//Set the border for the link annotation
linkAnnotation.Border = new PdfAnnotationBorder(0, 0, 0); // No border
// Add the link annotation to the first page
firstPage.Annotations.Add(linkAnnotation);
//Draw the text for the link annotation for better visualization
firstPage.Graphics.DrawString("Click here to go to the second page.", font, PdfBrushes.Blue, new PointF(125, 10));
// Save the PDF document
using (FileStream outputStream = new FileStream("internal-navigation.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream);
}
}
After running this code, a PDF file with an internal navigation link on the first page will be created, allowing users to click and navigate directly to the second page. This enhances navigation and makes your PDF documents more user-friendly.
Navigating to external documents from a PDF
Sometimes, you can provide links within a PDF that direct users to external resources like images, text files, other PDFs, or multimedia files. This is especially helpful for linking to additional documentation, supporting materials, or supplementary resources outside the current document.
Using the .NET PDF Library, you can create file links in your PDF with the PdfFileLinkAnnotation class, allowing seamless access to these external resources.
Step 1: Create a new PDF document
Start by creating an instance of the PdfDocument class to create a new PDF document.
// Create a new PDF document
using (PdfDocument document = new PdfDocument())
Step 2: Add a page to the PDF document
Add a new page where the link to the external document will be placed.
// Create a new page
PdfPage page = document.Pages.Add();
Step 3: Define the link area on the page
Create a rectangle that defines the clickable area for the link annotation.
// Define the bounds for the link annotation
RectangleF bounds = new RectangleF(10, 40, 180, 14);
Step 4: Create a file link annotation
Use the PdfFileLinkAnnotation class to specify the file path of the external document you wish to link. For instance, in this example, we’ll add a Word document.
// Specify the file path for the external document
string filePath = "data/Adventure Works Cycles.docx";
// Create a link annotation to the external file
PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(bounds, filePath);
Step 5: Add the annotation to the PDF page
Now, add the file link annotation to the page’s annotation collection.
// Add the annotation to the page
page.Annotations.Add(fileLinkAnnotation);
Step 6: Save the PDF document
Save the document to a stream or file.
// Save the PDF document
using (FileStream outputStream = new FileStream("external-document-navigation.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream);
}
Here’s the complete code to add a link to an external document within a PDF file.
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
using (PdfDocument document = new PdfDocument())
{
// Create a new page
PdfPage page = document.Pages.Add();
// Draw string
page.Graphics.DrawString("Click here to open an external file", new PdfStandardFont(PdfFontFamily.Helvetica, 12), PdfBrushes.Black, new PointF(10, 40));
// Define the bounds for the link annotation
RectangleF bounds = new RectangleF(10, 40, 180, 14);
// Specify the file path for the external document
string filePath = "data/Adventure Works Cycles.docx";
// Create a link annotation to the external file
PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(bounds, filePath);
// Add the annotation to the page
page.Annotations.Add(fileLinkAnnotation);
// Save the PDF document
using (FileStream outputStream = new FileStream("external-document-navigation.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream);
}
}
Adding links to external documents within a PDF enhances the document’s usability by integrating direct access to additional resources or supporting information. This feature benefits educational or technical documents, making supplementary files readily accessible to readers.
Navigating to specific pages in an external PDF file
Navigating specific pages in an external PDF file enables users to jump directly to a desired page, improving the usability and efficiency a PDF document. With the .NET PDF Library, you can easily create links that direct users to specific pages in another PDF file, making your content more interactive and user-friendly.
The PdfRemoteGoToAction class creates actions that navigate to a specific page in an external PDF file.
Step 1: Create a new PDF document
Begin by creating an instance of the PdfDocument class, the PDF file containing the remote navigation links.
//Create a new PDF document
using (PdfDocument document = new PdfDocument())
Step 2: Add a new page
Add a page in your PDF document where you wish to insert the navigation link.
//Create a new page
PdfPage page = document.Pages.Add();
Step 3: Define the action to navigate to the external PDF page
Set up the action for navigating to the remote file, specifying the external PDF file and the page number you want to navigate to.
//Create a new remote destination
PdfRemoteDestination remoteDestination = new PdfRemoteDestination();
//Set the remote PDF page number
remoteDestination.RemotePageNumber = 3;
//Set the destination mode
remoteDestination.Mode = PdfDestinationMode.FitToPage;
//Create a new PdfRemoteGoToAction and mention the remote PDF file name and remote destination
PdfRemoteGoToAction goToAction = new PdfRemoteGoToAction("input.pdf", remoteDestination);
//Set the PDF to open in new window
goToAction.IsNewWindow = true;
Step 4: Create the button field to add the action
Create a new button field on the page using the PdfButtonField class and add it to the remote action.
//Create a new PdfButtonField
PdfButtonField submitButton = new PdfButtonField(page, "gotoActionButton");
submitButton.Bounds = new RectangleF(25, 160, 250, 20);
submitButton.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
submitButton.Text = "Click here to open the external PDF file";
submitButton.BackColor = new PdfColor(181, 191, 203);
//Add the action to the button.
submitButton.Actions.GotFocus = goToAction;
//Add the submit button to a new document
document.Form.Fields.Add(submitButton);
Step 5: Save the modified PDF document
After adding the remote goto action, save the changes to the PDF document.
using (FileStream outputStream = new FileStream("navigate-pages-to-external-pdf.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream);
}
Here’s the complete code for navigating to a specific page in an external PDF document.
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
//Create a new PDF document
using (PdfDocument document = new PdfDocument())
{
//Create a new page
PdfPage page = document.Pages.Add();
//Create a new remote destination
PdfRemoteDestination remoteDestination = new PdfRemoteDestination();
//Set the remote PDF page number
remoteDestination.RemotePageNumber = 3;
//Set the destination mode
remoteDestination.Mode = PdfDestinationMode.FitToPage;
//Create a new PdfRemoteGoToAction and mention the remote PDF file name and remote destination
PdfRemoteGoToAction goToAction = new PdfRemoteGoToAction("input.pdf", remoteDestination);
//Set the to open in new window
goToAction.IsNewWindow = true;
//Create a new PdfButtonField
PdfButtonField submitButton = new PdfButtonField(page, "gotoActionButton");
submitButton.Bounds = new RectangleF(25, 160, 250, 20);
submitButton.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
submitButton.Text = "Click here to open the external PDF file";
submitButton.BackColor = new PdfColor(181, 191, 203);
//Add the action to the button.
submitButton.Actions.GotFocus = goToAction;
//Add the submit button to a new document
document.Form.Fields.Add(submitButton);
using (FileStream outputStream = new FileStream("navigate-pages-to-external-pdf.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream);
}
}
After running this code, the PDF file will be created to include a navigation link that directs users to a specific page in the designated external PDF. This functionality enhances user experience by allowing quick access to relevant content, making your documents more interactive and easier to navigate.
Modifying existing hyperlinks in a PDF
Modifying existing hyperlinks in a PDF allows you to update URLs, change destinations, or alter the appearance of the links within your document. The Syncfusion .NET PDF Library will enable you to easily access and update hyperlink annotations, ensuring your PDF content remains accurate and functional.
To modify a hyperlink, you’ll typically retrieve the existing annotation, change its properties, and then save the updated document. This straightforward process lets you keep your documents current without recreating them from scratch.
Step 1: Load the existing PDF document
Begin by creating the PdfLoadedDocument class to open the PDF file containing the hyperlinks you want to modify.
using (FileStream fileStream = new FileStream("data/document-with-hyperlinks.pdf", FileMode.Open, FileAccess.ReadWrite))
{
//Load the existing PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream))
Step 2: Access the page with the hyperlink
Access the specific page in the PDF document where the hyperlink is located.
// Access the desired page (e.g., the third page)
PdfLoadedPage page = loadedDocument.Pages[2] as PdfLoadedPage;
Step 3: Retrieve the existing hyperlink annotation
Loop through the annotations on the page to find the hyperlink you want to modify.
// Loop through the annotations on the page
foreach (PdfLoadedAnnotation annotation in page.Annotations)
{
// Check if the annotation is a hyperlink
if (annotation is PdfLoadedUriAnnotation)
{
PdfLoadedUriAnnotation uriAnnotation = annotation as PdfLoadedUriAnnotation;
// Modify the existing URL
uriAnnotation.Uri = "https://www.syncfusion.com/succinctly-free-ebooks/pdf";
}
else if (annotation is PdfLoadedTextWebLinkAnnotation)
{
PdfLoadedTextWebLinkAnnotation linkAnnotation = annotation as PdfLoadedTextWebLinkAnnotation;
// Modify existing URL
linkAnnotation.Url = "https://www.syncfusion.com/succinctly-free-ebooks/pdf";
}
}
Step 4: Save the modified PDF document
Save the updated PDF document after making the necessary changes to the hyperlinks.
using (FileStream outputStream = new FileStream("modify-existing-hyperlinks.pdf", FileMode.Create, FileAccess.ReadWrite))
{
// Save the modified document
loadedDocument.Save(outputStream);
}
Here’s the complete code for modifying existing hyperlinks in a PDF document.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using (FileStream fileStream = new FileStream("data/document-with-hyperlinks.pdf", FileMode.Open, FileAccess.ReadWrite))
{
//Load the existing PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream))
{
// Access the desired page (e.g., the third page)
PdfLoadedPage page = loadedDocument.Pages[2] as PdfLoadedPage;
// Loop through the annotations on the page
foreach (PdfLoadedAnnotation annotation in page.Annotations)
{
// Check if the annotation is a hyperlink
if (annotation is PdfLoadedUriAnnotation)
{
PdfLoadedUriAnnotation uriAnnotation = annotation as PdfLoadedUriAnnotation;
// Modify the existing URL
uriAnnotation.Uri = "https://www.syncfusion.com/succinctly-free-ebooks/pdf";
}
else if (annotation is PdfLoadedTextWebLinkAnnotation)
{
PdfLoadedTextWebLinkAnnotation linkAnnotation = annotation as PdfLoadedTextWebLinkAnnotation;
// Modify existing URL
linkAnnotation.Url = "https://www.syncfusion.com/succinctly-free-ebooks/pdf";
}
}
using (FileStream outputStream = new FileStream("modify-existing-hyperlinks.pdf", FileMode.Create, FileAccess.ReadWrite))
{
// Save the modified document
loadedDocument.Save(outputStream);
}
}
}
After executing this code, the specified PDF file will be updated to reflect the modified hyperlinks. Users will benefit from the updated links, ensuring the document provides accurate and relevant information, thus enhancing its functionality and user engagement.
Removing hyperlinks from a PDF document
Removing hyperlinks from a PDF document can help streamline the content, eliminate outdated or incorrect links, or enhance the document’s appearance. With the Syncfusion .NET PDF Library, you can easily access and remove hyperlink annotations from your PDF, allowing you to control user content.
The process involves loading the existing PDF, identifying the hyperlink annotations, and removing them as needed.
Step 1: Load the existing PDF document
Start by creating an instance of the PdfLoadedDocument class to open the PDF file containing the hyperlinks you want to remove.
using (FileStream fileStream = new FileStream("data/document-with-hyperlinks.pdf", FileMode.Open, FileAccess.ReadWrite))
{
//Load the existing PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream))
Step 2: Access the page with the hyperlinks
Access the specific page in the PDF document where the hyperlinks are located.
// Access the desired page (e.g., the third page)
PdfLoadedPage page = loadedDocument.Pages[2] as PdfLoadedPage;
Step 3: Identify and remove hyperlink annotations
Loop through the annotations on the page and remove any hyperlink annotations found.
// Loop through the annotations on the page
for (int i = page.Annotations.Count - 1; i >= 0; i--)
{
PdfAnnotation annotation = page.Annotations[i];
// Check if the annotation is a hyperlink
if (annotation is PdfLoadedUriAnnotation || annotation is PdfLoadedTextWebLinkAnnotation)
{
// Remove the hyperlink
page.Annotations.Remove(annotation);
}
}
Step 4: Save the modified PDF document
After removing the hyperlinks, save the updated PDF document.
using (FileStream outputStream = new FileStream("remove-hyperlinks.pdf", FileMode.Create, FileAccess.ReadWrite))
{
// Save the modified document
loadedDocument.Save(outputStream);
}
Here’s the complete code for removing hyperlinks from a PDF document.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using (FileStream fileStream = new FileStream("data/document-with-hyperlinks.pdf", FileMode.Open, FileAccess.ReadWrite))
{
//Load the existing PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream))
{
// Access the desired page (e.g., the third page)
PdfLoadedPage page = loadedDocument.Pages[2] as PdfLoadedPage;
// Loop through the annotations on the page
for (int i = page.Annotations.Count - 1; i >= 0; i--)
{
PdfAnnotation annotation = page.Annotations[i];
// Check if the annotation is a hyperlink
if (annotation is PdfLoadedUriAnnotation || annotation is PdfLoadedTextWebLinkAnnotation)
{
// Remove the hyperlink
page.Annotations.Remove(annotation);
}
}
using (FileStream outputStream = new FileStream("remove-hyperlinks.pdf", FileMode.Create, FileAccess.ReadWrite))
{
// Save the modified document
loadedDocument.Save(outputStream);
}
}
}
After executing this code, the specified PDF file will no longer contain hyperlinks. This can be particularly useful for cleaning up documents or preparing them for distribution without external links, ensuring the content remains focused and relevant for the audience.
GitHub reference
For more details, refer to the add, update, or remove hyperlinks in PDF using the C# GitHub demo.
Conclusion
Thank you for reading! This blog has covered adding, updating, and removing hyperlinks in PDF documents using the Syncfusion .NET PDF Library in C#.
We encourage you to explore the PDF Library’s documentation to discover additional functionalities and features to help you manipulate PDFs effectively.
If you have any questions or need assistance, feel free to comment below or contact us through our support forum, support portal, or feedback portal. We are always here to assist you!
Related blogs
- Easily Create, Update, and Remove Bookmarks in PDFs Using C#
- Easily Add and Remove Attachments in Your PDF Documents Using C#
- Step-by-Step Guide to Adding and Removing Watermarks in PDF using C#
- Add, Remove, Extract, and Replace Images in PDF using C#
- 9 Types of Useful Data You Can Extract from a PDF Using C#
Top comments (0)