This short tutorial guides on how to add hyperlink in PDF using C#. It contains the necessary resource required to establish the environment, a detailed description of the process in the form of steps, and a runnable sample code to insert link in PDF using C#. You will also learn the process to customize the hyperlink and its text as per the requirements.
Steps to Create Link for PDF using C#
- Establish the environment to use Aspose.PDF for .NET to add hyperlinks
- Open the target PDF using the Document class object and get a reference to the desired page
- Create a LinkAnnotation object and set its properties including the URI before adding it to the page
- Create a FreeTextAnnotation object and set text to be displayed over the link
- Add both annotations to the page
- Save the resultant PDF file on the disk having a hyperlink in it
The above steps elaborate the process of how to add link in PDF using C# where first the source PDF file is loaded and its first page is accessed where the hyperlink is to be added. In the next steps, a link annotation is created within a rectangle having an invisible border and a text annotation is also created on the same location within a rectangle of the same size and similar invisible border thus overlapping the link area and text area for creating a hyperlink. In the end, both these annotations are added to the selected PDF page and the resultant PDF file is saved on the disk.
Code to Add Link to PDF using C#
using Aspose.Pdf; | |
using Aspose.Pdf.Annotations; | |
namespace AsposeProjects | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to add hyperlink to a PDF using C# | |
{ | |
// Initialize license | |
License lic = new License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Open the document | |
Document document = new Document("AddHyperlink.pdf"); | |
// Get a reference to the first page | |
Page page = document.Pages[1]; | |
// Create a Link annotation object by setting its rectangular area, border and URI | |
LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(100, 100, 300, 300)); | |
Border border = new Border(link); | |
border.Width = 0; | |
link.Border = border; | |
link.Action = new GoToURIAction("www.aspose.com"); | |
// Add the link annotation to the target page | |
page.Annotations.Add(link); | |
// Create Free Text annotation by setting its rectangular area, appearance, color, contents, and border | |
FreeTextAnnotation textAnnotation = new FreeTextAnnotation(document.Pages[1], | |
new Aspose.Pdf.Rectangle(100, 100, 300, 300), | |
new DefaultAppearance( | |
Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman"), | |
10, System.Drawing.Color.Blue)); | |
textAnnotation.Contents = "Link to Aspose website"; | |
// Set same border as used for link annotation | |
textAnnotation.Border = border; | |
// Add the FreeText annotation also to the annotations collection of the target page of the document | |
page.Annotations.Add(textAnnotation); | |
// Save the updated document | |
document.Save("Output.pdf"); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
The aforementioned sample code demonstrates the process on how to create a link to a PDF using C# where LinkAnnotation object is used to create the actual link by providing the URI, settings its border along with 0 width, and the action is set to open the target page whose URL is set. Similarly, while creating FreeTextAnnotation object for displaying hyperlink text, the rectangle is defined, and appearance is set along with font and color settings. Note that a lot of other properties can also be set for these annotations.
In this topic, we have learned the process to embed link in PDF using C#. If you want to learn the process to add a header and footer in a PDF file, refer to the article on how to add header and footer in PDF using C#.