PDF is a feature-rich document format that supports a range of elements including annotations, media, forms, etc. Hyperlink is an important element that is used to navigate within the PDF, from one PDF to another, to a web page, etc. While generating the PDF documents programmatically, you may often need to insert hyperlinks. Accordingly, in this article, you will learn how to add and update hyperlinks in PDF files in C# .NET.
- .NET API to Add Hyperlinks in PDF
- Add Hyperlink in a PDF
- Add Hyperlink to an External PDF
- Insert Hyperlink to a Particular Page
- Update a Hyperlink in PDF
C# .NET API to Add or Update Hyperlinks in PDF
To manipulate hyperlinks in PDF files, we will use Aspose.PDF for .NET. It is a popular API that allows you to create, process, and convert PDF files from within .NET applications. You can download the API’s binaries or install it using NuGet.
PM> Install-Package Aspose.PDF
Add Hyperlink in a PDF in C#
The following are the steps to add a hyperlink to a PDF file in C#.
- Create a new PDF or load an existing one using Document class.
- Get reference of the page where you want to add hyperlink from Document.Pages collection.
- Create an object of LinkAnnotation class and set its properties.
- Initialize LinkAnnotation.Action property to GoToURIAction object containing URL.
- Add link to Page.Annotations collection.
- Save PDF using Document.Save(String) method.
The following code sample shows how to add a hyperlink in PDF in C#.
Add Hyperlink to an External PDF in C#
You can also navigate to an external PDF file from one PDF using a hyperlink. The following steps demonstrate how to add a hyperlink to an external PDF file in C#.
- Create a new PDF or load an existing one using Document class.
- Get reference of the page where you want to add hyperlink from Document.Pages collection.
- Create an object of LinkAnnotation class and set its properties.
- Initialize LinkAnnotation.Action property to GoToRemoteAction object containing path to external PDF.
- Add link to Page.Annotations collection.
- Save document using Document.Save(String) method.
The following code sample shows how to add hyperlink to an external PDF in C#.
Insert Hyperlink to a Particular PDF Page
In certain cases, you have to navigate from one page to another within a PDF document. The LocalHyperlink class is used to navigate to the particular page in a PDF. The following are the steps to achieve this.
- Create a new PDF or load an existing one using Document class.
- Get reference of the page where you want to add hyperlink from Document.Pages collection.
- Create an instance of TextFragment to set text of the hyperlink.
- Create an instance of LocalHyperlink class and set LocalHyperlink.TargetPageNumber property to desired page number.
- Assign LocalHyperlink object to TextFragment.Hyperlink property.
- Add text to the page using Page.Paragraphs.Add(TextFragment) method.
- Save PDF using Document.Save(String) method.
The following code sample shows how to add hyperlink to navigate to a particular page in PDF.
Update a Hyperlink in PDF using C#
You can also retrieve a hyperlink in a PDF file and update its properties. The following are the steps to perform this operation.
- Load the PDF file using Document class.
- Get reference of the page where the hyperlink is located from Document.Pages collection.
- Extract the desired LinkAnnotation object from Page.Annotations collection by index.
- Update the required properties and save PDF using Document.Save(String) method.
The following code sample shows how to update a hyperlink in PDF using C#.
Get a Free License
You can get a free temporary license in order to use Aspose.PDF for .NET without evaluation limitations.
Conclusion
In this article, you have learned how to add different types of hyperlinks in PDF files using C#. Furthermore, you have seen how to retrieve the hyperlinks from existing PDF files and update their properties programmatically. In addition, you can explore more about .NET PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.