A link annotation enables users to open a web link, an email link or an external file from PDF document by clicking the annotation itself. In this article, I’ll take file link annotation as an example to introduce how to create a link annotation in PDF using Spire.PDF.
Code Snippet:
Step 1: Create a new object of PdfDocument, add a page to it.
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
Step 2: Declare two parameters that will be passed to the constructor of PdfFileLinkAnnotation class.
RectangleF rect = new RectangleF(0, 40, 150, 35); string filePath = @"C:\Users\Administrator\Desktop\Convert SQL Data base to PDF.docx";
Step 3: Create a file link annotation based on the two parameters and add the annotation to the new page.
PdfFileLinkAnnotation link = new PdfFileLinkAnnotation(rect, filePath); page.Annotations.Add(link);
Step 4: Create a free text annotation based on the same RectangleF, specifying the content.
PdfFreeTextAnnotation text= new PdfFreeTextAnnotation(rect); text.Text = "Click here! This is a link annotation."; PdfFont font = new PdfFont(PdfFontFamily.Helvetica,10); text.Font = font; page.Annotations.Add(text);
Step 5: Save the file.
doc.SaveToFile("PdfLinkAnnotation.pdf", FileFormat.PDF);
Output:
Click the annotation, a dialog box appears to check if you want to launch a file from PDF document.
Full Code:
using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using System.Drawing; namespace LinkAnnotation { class Program { static void Main(string[] args) { PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); RectangleF rect = new RectangleF(0, 40, 120, 35); string filePath = @"C:\Users\Administrator\Desktop\Convert SQL Data base to PDF.docx"; PdfFileLinkAnnotation link = new PdfFileLinkAnnotation(rect, filePath); page.Annotations.Add(link); PdfFreeTextAnnotation text = new PdfFreeTextAnnotation(rect); text.Text = "Click here! This is a link annotation."; PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10); text.Font = font; page.Annotations.Add(text); doc.SaveToFile("PdfLinkAnnotation.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("PdfLinkAnnotation.pdf"); } } }
Imports Spire.Pdf Imports Spire.Pdf.Annotations Imports Spire.Pdf.Graphics Imports System.Drawing Namespace LinkAnnotation Class Program Private Shared Sub Main(args As String()) Dim doc As New PdfDocument() Dim page As PdfPageBase = doc.Pages.Add() Dim rect As New RectangleF(0, 40, 120, 35) Dim filePath As String = "C:\Users\Administrator\Desktop\Convert SQL Data base to PDF.docx" Dim link As New PdfFileLinkAnnotation(rect, filePath) page.Annotations.Add(link) Dim text As New PdfFreeTextAnnotation(rect) text.Text = "Click here! This is a link annotation." Dim font As New PdfFont(PdfFontFamily.Helvetica, 10) text.Font = font page.AnnotationsW.Add(text) doc.SaveToFile("PdfLinkAnnotation.pdf", FileFormat.PDF) System.Diagnostics.Process.Start("PdfLinkAnnotation.pdf") End Sub End Class End Namespace