How to add a document link annotation to PDF in C#

A document link annotation can enable readers to navigate to other place from the same PDF document. This article will demonstrate how to add a document link annotation to PDF in C# with the help of Spire.PDF.

Step 1: Create a new PDF document and add a new page to it.

PdfDocument document = new PdfDocument();
PdfPageBase page = document.Pages.Add();

Step 2: Draw text.

string text = "Document link annotation";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 8f));
RectangleF docLinkAnnotationBounds = new RectangleF(10, 40, 100, 50);
page.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, docLinkAnnotationBounds);

Step 3: Create a new document link annotation and set its color.

PdfDocumentLinkAnnotation documentLinkAnnotation = new PdfDocumentLinkAnnotation(docLinkAnnotationBounds);
documentLinkAnnotation.Color = new PdfRGBColor(Color.Navy);

Step 4: Add another page and set it as destination.

PdfPageBase navigationPage = document.Pages.Add();
documentLinkAnnotation.Destination = new PdfDestination(navigationPage);
documentLinkAnnotation.Destination.Location = new Point(10, 0);
documentLinkAnnotation.Destination.Zoom = 2;

Step 5: Add this annotation to the new page.

page.AnnotationsWidget.Add(documentLinkAnnotation);

Step 6: Save the document to file.

document.SaveToFile("DocumentLinkAnnotation.pdf");   

Effective screenshot of adding a document link annotation to PDF:

How to add a document link annotation to PDF in C#