Working with PDF annotations using C#: Creating markup annotation

Andriy Andruhovski
asposepdf
Published in
3 min readMay 14, 2018
“A woman taking notes in a large notebook next to a laptop” by J. Kelly Brito on Unsplash

The PDF standard describes various tools that allow you to create an additional content on the page. In this topic we will talk about markup annotation tools. Markup annotations points in specific way to some places on the page and contains additional text info.

Most types of the markup annotations support two states: open and closed. Closed state means that annotation will appear on the page in some distinctive form such as an icon, box, stamp, etc. When the user activates the annotation by clicking it, it exhibits its associated object (annotation goes to the open state).

Markup annotation (highlighted text) in open state

Markup annotations can be divided into the following groups:

  • free text annotations display text directly on the page;
  • other text annotation with pop-up window;
  • sound annotation without pop-up window but it can be holds a text information.

Obviously, annotations are applied to some objects (or places) on the page. Let us assume that these are text fragments. In this demo, we will use a sample PDF file and search the “PDF” acronym to annotate it in different ways.

So, our task can be divided into two sub-tasks:

  1. Find the text fragments containing the “PDF” string and get their coordinates;
  2. Apply appropriate annotation.

To create all types of annotation we will use a PdfContentEditor from Aspose.PDF for .NET.

Create free text annotation

A free text annotation displays text directly on the page. The PdfContentEditor.CreateFreeText method allows creating this type of annotation. In the following snippet, we add free text annotation above the first occurrence of the string.

Document with Free Text Annotation

Create text annotation

A text annotation is a “sticky note” attached to the point of the PDF document. In the closed state, the annotation will be displayed as an icon. Aspose.PDF supports 7 standard icon types: “Note”, “Comment”, “Key”, “Help”, “Insert”, “NewParagraph”, “Paragraph”, and 4 extra (“Check”, “Cross”, “Circle”, “Star”). We can create text annotation using PdfContentEditor.CreateText method.

Following example shows adding text annotations for all occurrences of a searched string.

Document with Text Annotations

Create text markup annotation

Text markup annotation will appear as highlights, underlines, strikeouts or squiggles. The PdfContentEditor.CreateMarkup method allows creating text markups.

Create Line annotation

The purpose of line annotation is to display a single straight line on the page.

For this type of annotation we will use PdfContentEditor.CreateLine method.

--

--