C#: Add Various Types of Annotations to PDF

PDF annotation tools allow you to highlight text, add sticky notes, draw shapes, and insert comments directly on PDF documents. This can be useful for providing feedback, taking notes, marking up designs, and collaborating on documents. Mastering PDF annotation features can streamline workflows and improve productivity when working with PDF files.

In this article, you will learn how to programmatically add various types of annotations to a PDF document using Spire.PDF for .NET in C#.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Add a Markup Annotation to PDF in C#

Markup annotation in PDF enables users to select and apply a colored background to emphasize or draw attention to specific text within the document.

Spire.PDF provides the PdfTextMarkupAnnotation class to work with this type of annotation. To add a markup annotation to PDF using Spire.PDF in C#, follow these steps.

  • Create a PdfDocument object.
  • Load a PDF document from the specified file path.
  • Get a specific page from the document.
  • Find the desired text from the page using the methods provided by the PdfTextFinder class.
  • Create a PdfTextMarkupAnnotation object based on the text found.
  • Add the annotation to the page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf.Annotations;
using Spire.Pdf;
using Spire.Pdf.Texts;
using System.Drawing;

namespace AddMarkupAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextFinder object based on the page
            PdfTextFinder finder = new PdfTextFinder(page);

            // Set the find options
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // Find the instances of the specified text
            List<PdfTextFragment> fragments = finder.Find("In order to help make the Site a secure environment" +
                " for the purchase and sale of Marketplace Offerings, all users are required to accept and " +
                "comply with these Terms of Service.");

            // Get the first instance
            PdfTextFragment textFragment = fragments[0];

            // Specify annotation text
            String text = "There is a markup annotation added by Spire.PDF for .NET.";

            // Iterate through the bounds of the found text
            for (int i = 0; i < textFragment.Bounds.Length; i++)
            {
                // Get a specific bound
                RectangleF rect = textFragment.Bounds[i];

                // Create a text markup annotation
                PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Administrator", text, rect);

                // Set the markup color
                annotation.TextMarkupColor = Color.Green;

                // Add the annotation to the collection of the annotations
                page.Annotations.Add(annotation);
            }

            // Save result to file
            doc.SaveToFile("AddMarkups.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add Various Types of Annotations to PDF

Add a Free Text Annotation to PDF in C#

Free Text Annotation in PDF files enables users to add handwritten-like or typed text directly onto the document, similar to taking notes on a printed document.

Spire.PDF provides the PdfFreeTextAnnotation to work with the free text annotations in PDF. Here is how you can use to create one in a PDF document.

  • Create a PdfDocument object.
  • Load a PDF document from the specified file path.
  • Get a specific page from the document.
  • Find the desired text from the page using the methods provided by the PdfTextFinder class.
  • Specify the x and y coordinates to add annotation.
  • Create a PdfFreeTextAnnotation object, and set its properties like text, font, border and color.
  • Add the annotation to the page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddFreeTextAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextFinder object based on the page
            PdfTextFinder finder = new PdfTextFinder(page);

            // Set the find options
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // Find the instances of the specified text
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // Get the first instance
            PdfTextFragment textFragment = fragments[0];

            // Get the text bound
            RectangleF rect = textFragment.Bounds[0];

            // Get the x and y coordinates to add annotation
            float x = rect.Right;
            float y = rect.Bottom;

            // Create a free text annotation
            RectangleF rectangle = new RectangleF(x, y, 130, 30);
            PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rectangle);

            // Set the content of the annotation
            textAnnotation.Text = "There is a free text annotation\radded by Spire.PDF for .NET.";

            // Set other properties of annotation
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10f, PdfFontStyle.Bold);
            PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
            textAnnotation.Font = font;
            textAnnotation.Border = border;
            textAnnotation.BorderColor = Color.Purple;
            textAnnotation.Color = Color.Green;
            textAnnotation.Opacity = 1.0f;

            // Add the annotation to the collection of the annotations
            page.Annotations.Add(textAnnotation);

            // Save result to file
            doc.SaveToFile("FreeTextAnnotation.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add Various Types of Annotations to PDF

Add a Popup Annotation to PDF in C#

Popup Annotation in PDF files allows users to attach a small label or comment that pops up when clicked, revealing additional information or a short message.

Spire.PDF offers the PdfPopupAnnotation class to work with the popup annotation in PDF. The following are the steps to add a popup annotation to PDF using it.

  • Create a PdfDocument object.
  • Load a PDF document from the specified file path.
  • Get a specific page from the document.
  • Find the desired text from the page using the methods provided by the PdfTextFinder class.
  • Specify the x and y coordinates to add annotation.
  • Create a PdfPopupAnnotation object, and set its properties like text, icon and color.
  • Add the annotation to the page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using System.Drawing;

namespace AddPopupAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextFinder object based on the page
            PdfTextFinder finder = new PdfTextFinder(page);

            // Set the find options
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // Find the instances of the specified text
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // Get the first instance
            PdfTextFragment textFragment = fragments[0];

            // Get the text bound
            RectangleF textBound = textFragment.Bounds[0];

            // Get the x and y coordinates to add annotation
            float x = textBound.Right + 5;
            float y = textBound.Top - 15;

            // Create a popup annotation
            RectangleF rectangle = new RectangleF(x, y, 30, 30);
            PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rectangle);
            
            // Set the annotation text
            popupAnnotation.Text = "There is a popup annotation\radded by Spire.PDF for .NET.";

            // Set the icon and color of the annotation
            popupAnnotation.Icon = PdfPopupIcon.Comment;
            popupAnnotation.Color = Color.Red;

            // Add the annotation to the collection of the annotations
            page.Annotations.Add(popupAnnotation);

            // Save result to file
            doc.SaveToFile("PopupAnnotation.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add Various Types of Annotations to PDF

Add a Shape Annotation to PDF in C#

Shape Annotation in PDF refers to the ability to add graphical shapes such as rectangles, circles, lines, or arrows onto a PDF document to highlight or provide additional information.

Spire.PDF offers the PdfPolyLineAnnotation class that allows the user to create a custom shape annotation in a PDF document. Here are the detailed steps.

  • Create a PdfDocument object.
  • Load a PDF document from the specified file path.
  • Get a specific page from the document.
  • Find the desired text from the page using the methods provided by the PdfTextFinder class.
  • Specify the coordinates to add annotation.
  • Create a PdfPloyLineAnnotation object, and set the text of the annotation.
  • Add the annotation to the page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using System.Drawing;

namespace AddShapeAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextFinder object based on the page
            PdfTextFinder finder = new PdfTextFinder(page);

            // Set the find options
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // Find the instances of the specified text
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // Get the first instance
            PdfTextFragment textFragment = fragments[0];

            // Get the text bound
            RectangleF textBound = textFragment.Bounds[0];

            // Get the coordinates to add annotation
            float left = textBound.Left;
            float top = textBound.Top;
            float right = textBound.Right;
            float bottom = textBound.Bottom;

            // Create a shape nnotation
            PdfPolyLineAnnotation polyLineAnnotation = new PdfPolyLineAnnotation(page, new PointF[] { 
                new PointF(left, top), new PointF(right, top), new PointF(right - 5, bottom), new PointF(left - 5, bottom), new PointF(left, top) });

            // Set the annotation text
            polyLineAnnotation.Text = "There is a shape annotation\radded by Spire.PDF for .NET.";

            // Add the annotation to the collection of the annotations
            page.Annotations.Add(polyLineAnnotation);

            // Save result to file
            doc.SaveToFile("ShapeAnnotation.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add Various Types of Annotations to PDF

Add a Web Link Annotation to PDF in C#

Web Link Annotation in PDF documents allows users to embed hyperlinks that direct readers to websites when clicked.

Spire.PDF provides the PdfUrlAnnotation class to represent a web link annotation. The following are the steps to add a web link annotation using it.

  • Create a PdfDocument object.
  • Load a PDF document from the specified file path.
  • Get a specific page from the document.
  • Find the desired text from the page using the methods provided by the PdfTextFinder class.
  • Create a PdfUrlAnnotation object based on the text found.
  • Add the annotation to the page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using System.Drawing;

namespace AddUrlAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextFinder object based on the page
            PdfTextFinder finder = new PdfTextFinder(page);

            // Set the find options
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // Find the instances of the specified text
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // Get the first instance
            PdfTextFragment textFragment = fragments[0];

            // Get the text bound
            RectangleF textBound = textFragment.Bounds[0];

            // Create a Url annotation
            PdfUriAnnotation urlAnnotation = new PdfUriAnnotation(textBound, "https:\\\\www.e-iceblue.com\\");

            // Add the annotation to the collection of the annotations
            page.Annotations.Add(urlAnnotation);

            // Save result to file
            doc.SaveToFile("UrlAnnotation.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add Various Types of Annotations to PDF

Add a File Link Annotation to PDF in C#

File link annotation in PDF documents refers to interactive links that allow users to navigate to external files directly from the PDF.

Spire.PDF offers the PdfFileLinkAnnotation class to work with the file link annotation. Here are the steps to add a file link annotation to a PDF document using it.

  • Create a PdfDocument object.
  • Load a PDF document from the specified file path.
  • Get a specific page from the document.
  • Find the desired text from the page using the methods provided by the PdfTextFinder class.
  • Create a PdfFileLinkAnnotation object based on the text found.
  • Add the annotation to the page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using System.Drawing;

namespace AddFileLinkAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextFinder object based on the page
            PdfTextFinder finder = new PdfTextFinder(page);

            // Set the find options
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // Find the instances of the specified text
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // Get the first instance
            PdfTextFragment textFragment = fragments[0];

            // Get the text bound
            RectangleF textBound = textFragment.Bounds[0];

            // Create a file link annotation
            PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(textBound, "C:\\Users\\Administrator\\Desktop\\Report.docx");

            // Add the annotation to the collection of the annotations
            page.Annotations.Add(fileLinkAnnotation);

            // Save result to file
            doc.SaveToFile("FileLinkAnnotation.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add Various Types of Annotations to PDF

Add a Document Link Annotation to PDF in C#

Document link annotation in PDF files refers to hyperlinks that allow users to navigate between different pages or sections within the same PDF document.

Spire.PDF offers the PdfDocumentLinkAnnotation class to work with the document link annotation. Here are the steps to add a document link annotation to a PDF document using it.

  • Create a PdfDocument object.
  • Load a PDF document from the specified file path.
  • Get a specific page from the document.
  • Find the desired text from the page using the methods provided by the PdfTextFinder class.
  • Create a PdfDocumentLinkAnnotation object based on the text found.
  • Add the annotation to the page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using Spire.Pdf.General;
using System.Drawing;

namespace AddDocumentLinkAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            // Load a PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

            // Get a specific page
            PdfPageBase page = doc.Pages[0];

            // Create a PdfTextFinder object based on the page
            PdfTextFinder finder = new PdfTextFinder(page);

            // Set the find options
            finder.Options.Parameter = TextFindParameter.WholeWord;
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // Find the instances of the specified text
            List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings");

            // Get the first instance
            PdfTextFragment textFragment = fragments[0];

            // Get the text bound
            RectangleF textBound = textFragment.Bounds[0];

            // Create a document link annotation
            PdfDocumentLinkAnnotation documentLinkAnnotation = new PdfDocumentLinkAnnotation(textBound);
     
            // Set the destination of the annotation
            documentLinkAnnotation.Destination = new PdfDestination(doc.Pages[1]);

            // Add the annotation to the collection of the annotations
            page.Annotations.Add(documentLinkAnnotation);

            // Save result to file
            doc.SaveToFile("DocumentLinkAnnotation.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add Various Types of Annotations to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.