Skip to main content
A newer version of this page is available. .

Text Markup Annotations

  • 5 minutes to read

The PDF Document API component allows you to create, modify or remove text markup annotations.

You can apply underline, highlight, strikeout, and squiggly underline markup annotation types to a text.

The document consists of the following sections:

Create Markup Annotations

The PdfDocumentProcessor.AddTextMarkupAnnotation method allows you to add a markup annotation to the specified page area, rectangle or position on the page. This method returns the PdfTextMarkupAnnotationData object that represents the markup annotation data.

Note

If a specified page area does not correspond to the text on the page, the annotation is not created and the PdfDocumentProcessor.AddTextMarkupAnnotation methods return null.

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T545395.

This example shows how to create a text markup annotation that highlights text in a document and specify the annotation properties.

image


using System;
using DevExpress.Pdf;

namespace CreateMarkupAnnotation {
    class Program {
        static void Main(string[] args) {

            // Create a PDF Document Processor.
            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
            {

                // Load a document.
                processor.LoadDocument("..\\..\\Document.pdf");

                // Add a text markup annotation to the first page for text corresponding to the specified page area.
                PdfTextMarkupAnnotationData annotation = processor.AddTextMarkupAnnotation(1, new PdfRectangle(90, 100, 240, 230),
                    PdfTextMarkupAnnotationType.Highlight);
                if (annotation != null) {

                    // Specify the annotation properties
                    annotation.Author = "Bill Smith";
                    annotation.Contents = "Note";
                    annotation.Color = new PdfRGBColor(0.8, 0.2, 0.1);

                    // Save the resulting document.
                    processor.SaveDocument("..\\..\\Result.pdf");
                }
                else
                    Console.WriteLine("The annotation cannot be added to a page. Make sure the specified page area corresponds to text on the page.");
            }
        }
    }
}

Edit Markup Annotations

The following API allows you to change the markup annotation’s settings.

API

Description

PdfDocumentProcessor.GetMarkupAnnotationData

Returns all markup annotations from the specified page.

PdfMarkupAnnotationData.Author

Gets or sets the annotation’s author.

PdfMarkupAnnotationData.Contents

Specifies the annotation’s tooltip text (appears when the mouse hovers over an annotation)

PdfMarkupAnnotationData.Color

Defines the annotation’s color

PdfMarkupAnnotationData.Opacity

Gets or sets the annotation’s opacity.

This example shows how to change an existing markup annotation’s settings in a PDF document.

image

using DevExpress.Pdf;
using System;
using System.Collections.Generic;

namespace ModifyExistingMarkupAnnotation
{

    class Program {
        static void Main(string[] args)
        {

            // Create a PDF Document Processor.
            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
            {

                // Load a document.
                processor.LoadDocument("..\\..\\Document.pdf");

                // Get a list of annotations in the first document page.
                IList<PdfMarkupAnnotationData> annotations = processor.GetMarkupAnnotationData(1);

                if (annotations.Count > 0) {

                    // Change the properties of the first annotation.
                    annotations[0].Author = "Bill Smith";
                    annotations[0].Contents = "Important!";
                    annotations[0].Color = new PdfRGBColor(0.6, 0.7, 0.3);
                    annotations[0].Opacity = 0.2;

                    // Save the result document.
                    processor.SaveDocument("..\\..\\Result.pdf");
                }
                else
                    Console.WriteLine("The specified document page does not contain markup annotations!");
            }
        }
    }
}

Delete Markup Annotations

Call the PdfDocumentProcessor.DeleteMarkupAnnotations method to remove all markup annotations from the specified page.

To delete a specific markup annotation from a page, call the PdfDocumentProcessor.DeleteMarkupAnnotation method. Pass the target PdfMarkupAnnotationData object as an parameter to this method.

This example shows how to delete text markup annotations created by a particular author.

using DevExpress.Pdf;
using System.Linq;

namespace RemoveSpecificMarkupAnnotations
{

    class Program {
        static void Main(string[] args)
        {

            // Create a PDF Document Processor.
            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
            {

                // Load a document.
                processor.LoadDocument("..\\..\\Document.pdf");

                for (int i = 0; i <= processor.Document.Pages.Count; i++)
                {

                    // Remove Bill Smith's markup annotations from a page.
                    processor.DeleteMarkupAnnotations(processor.GetMarkupAnnotationData(i)
                    .Where(annotation => annotation.Author.Contains("Bill Smith")));

                    // Save the result document.
                    processor.SaveDocument("..\\..\\Result.pdf");
                }
            }
        }
    }
}