Skip to main content

Text Markup Annotations

  • 2 minutes to read

Use text markup annotations to identify or comment on document content. You can highlight text, underline important information, strike out obsolete content, add sticky notes, or place comments directly on a page.

The PDF Document API supports the following text markup annotation types:

Annotation Description
Highlight Highlights text.
Underline Draws a line below text.
Strikeout Draws a line through text.
Squiggly Draws a wavy underline below text.

Highlight Text

Create a TextMarkupAnnotation, specify text bounds, set the annotation type to HIGHLIGHT, and add the annotation to the page.

The following code snippet highlights all occurrences of “external PDF Viewer“:

import com.devexpress.docs.pdf.*;

import java.nio.file.*;
import java.nio.channels.*;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        try (FileChannel channel = FileChannel.open(Path.of("Document.pdf"));
                PdfDocument pdfDocument = new PdfDocument(channel)) {

            // Search for text.
            for (TextSearchInfo result : pdfDocument.findText("external PDF Viewer")) {
                for (TextMatchInfo match : result.getMatches()) {

                    List<OrientedRectangle> bounds =
                        match.getMatchFragments().stream()
                                .map(fragment -> fragment.getRectangle())
                                .toList();

                    TextMarkupAnnotation annotation = new TextMarkupAnnotation(bounds);

                    annotation.setType(TextMarkupAnnotationType.HIGHLIGHT);
                    annotation.setColor(PdfColor.getYellow());
                    annotation.setTitle("John Smith");
                    annotation.setContent("Review this section.");

                    pdfDocument.getPages()
                            .get(result.getPageIndex())
                            .getAnnotations()
                            .add(annotation);
                }
            }

            // Save the document.
            try (WritableByteChannel summaryChannel =
                    FileChannel.open(
                        Path.of("Result.pdf"),
                        StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(summaryChannel);
            }
        }
    }
}

Underline Text

Set the annotation type to UNDERLINE:

annotation.setType(TextMarkupAnnotationType.UNDERLINE);

Strike Out Text

Set the annotation type to STRIKE_OUT:

annotation.setType(TextMarkupAnnotationType.STRIKE_OUT);

Squiggly Underline

Set the annotation type to SQUIGGLY:

annotation.setType(TextMarkupAnnotationType.SQUIGGLY);
See Also