Text Based Annotations using Java

Text-based annotations enable reviewers and developers to add interactive notes, highlighting, and markup to PDF documents without altering core content. This section covers five practical annotation types used in document review workflows, compliance scenarios, and collaborative feedback cycles.

Quick Reference: Annotation Types

This article covers the following text-based annotation types:

  • Free Text: Editable text boxes for adding notes and comments
  • Highlight: Visual emphasis on important text passages
  • Strikeout: Mark text for deletion or revision during review
  • Squiggly: Wavy underline for indicating errors or concerns
  • Underline: Traditional underline emphasis with optional quad-point precision

Add, get, and delete free text annotations

Free text annotations act as floating text boxes that can be edited without affecting the document structure. Use these examples to add comment boxes, inspect their properties, or remove them.

Add free text annotations

  1. Open the source PDF Document.
  2. Create a FreeTextAnnotation with a rectangle and appearance settings.
  3. Add the annotation to the page and save the document.
public static void freeTextAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        FreeTextAnnotation freeTextAnnotation = new FreeTextAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299, 713, 308, 720, true),
                new DefaultAppearance());
        freeTextAnnotation.setTitle("Aspose User");
        freeTextAnnotation.setColor(Color.getLightGreen());

        document.getPages().get_Item(1).getAnnotations().add(freeTextAnnotation);
        document.save(outputFile.toString());
    }
}

Get free text annotations

  1. Open the source PDF Document.
  2. Iterate through annotations on the page and filter by AnnotationType.FreeText.
  3. Retrieve the annotation properties or bounds.
public static void freeTextAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.FreeText) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Delete free text annotations

  1. Open the source PDF Document.
  2. Find free text annotations by iterating through the page annotations and filtering by type.
  3. Add matching annotations to a delete list and remove them from the page.
  4. Save the updated document.
public static void freeTextAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.FreeText) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Add, get, and delete highlight annotations

Highlight annotations mark important passages with a semi-transparent overlay. Use these examples to create highlights for document review, locate existing highlights, and clean up markup.

Add highlight annotations

  1. Open the source PDF Document.
  2. Create a HighlightAnnotation with a rectangle defining the highlight area.
  3. Add the annotation to the page and save the document.
public static void textHighlightAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        HighlightAnnotation highlightAnnotation = new HighlightAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(300, 750, 320, 770, true));

        document.getPages().get_Item(1).getAnnotations().add(highlightAnnotation);
        document.save(outputFile.toString());
    }
}

Get highlight annotations

  1. Open the source PDF Document.
  2. Iterate through annotations and filter by AnnotationType.Highlight.
  3. Read the annotation properties such as bounds or color.
public static void textHighlightAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Highlight) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Delete highlight annotations

  1. Open the source PDF Document.
  2. Collect highlight annotations by filtering annotations by type.
  3. Remove each annotation from the page.
  4. Save the updated document.
public static void textHighlightAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Highlight) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Add, get, and delete strikeout annotations

Strikeout annotations cross out text to indicate deletion, rejection, or revision. Use these examples to apply strikethrough markup during document review, find marked text, and remove strikeout annotations.

Add strikeout annotations

  1. Open the source PDF Document.
  2. Create a StrikeOutAnnotation with a rectangle, title, and color.
  3. Add the annotation to the page and save the document.
public static void textStrikeoutAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        StrikeOutAnnotation strikeoutAnnotation = new StrikeOutAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        strikeoutAnnotation.setTitle("Aspose User");
        strikeoutAnnotation.setSubject("Inserted text 1");
        strikeoutAnnotation.setFlags(AnnotationFlags.Print);
        strikeoutAnnotation.setColor(Color.getBlue());

        document.getPages().get_Item(1).getAnnotations().add(strikeoutAnnotation);
        document.save(outputFile.toString());
    }
}

Get strikeout annotations

  1. Open the source PDF Document.
  2. Iterate through annotations and filter by AnnotationType.StrikeOut.
  3. Read annotation metadata or bounds.
public static void textStrikeoutAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.StrikeOut) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Delete strikeout annotations

  1. Open the source PDF Document.
  2. Collect strikeout annotations by filtering by type.
  3. Remove each annotation from the page.
  4. Save the updated document.
public static void textStrikeoutAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.StrikeOut) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Add, get, and delete squiggly annotations

Squiggly annotations (wavy underlines) highlight potential errors, concerns, or items requiring attention. Use these examples to mark problematic text, inspect squiggly annotations, and remove them from documents.

Add squiggly annotations

  1. Open the source PDF Document.
  2. Create a SquigglyAnnotation with a rectangle and title.
  3. Add the annotation to the page and save the document.
public static void textSquigglyAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);
        SquigglyAnnotation squigglyAnnotation = new SquigglyAnnotation(
                page,
                new Rectangle(67, 317, 261, 459, true));
        squigglyAnnotation.setTitle("John Smith");
        squigglyAnnotation.setColor(Color.getBlue());

        page.getAnnotations().add(squigglyAnnotation);
        document.save(outputFile.toString());
    }
}

Get squiggly annotations

  1. Open the source PDF Document.
  2. Iterate through annotations and filter by AnnotationType.Squiggly.
  3. Read annotation bounds or metadata.
public static void textSquigglyAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Squiggly) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Delete squiggly annotations

  1. Open the source PDF Document.
  2. Collect squiggly annotations by filtering by type.
  3. Remove each annotation from the page.
  4. Save the updated document.
public static void textSquigglyAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Squiggly) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Add, get, and delete underline annotations

Underline annotations emphasize important passages with a traditional underline. Use these examples to create underlines, read marked text content, and delete underline annotations from pages.

Add underline annotations

  1. Open the source PDF Document.
  2. Create an UnderlineAnnotation with a rectangle and color.
  3. Add the annotation to the page and save the document.
public static void textUnderlineAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        underlineAnnotation.setTitle("Aspose User");
        underlineAnnotation.setSubject("Inserted Underline 1");
        underlineAnnotation.setFlags(AnnotationFlags.Print);
        underlineAnnotation.setColor(Color.getBlue());

        document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
        document.save(outputFile.toString());
    }
}

Get underline annotations

  1. Open the source PDF Document.
  2. Iterate through annotations and filter by AnnotationType.Underline.
  3. Read annotation properties or bounds.
public static void textUnderlineAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Delete underline annotations

  1. Open the source PDF Document.
  2. Collect underline annotations by filtering by type.
  3. Remove each annotation from the page.
  4. Save the updated document.
public static void textUnderlineAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Add an underline annotation with quad points

This example defines the underline area explicitly through quad points derived from a rectangle.

  1. Open the source PDF Document.
  2. Create an UnderlineAnnotation and calculate its quad points.
  3. Add the annotation to the page and save the document.
public static void textUnderlineWithQuadPointsAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Rectangle rect = new Rectangle(299.988, 713.664, 308.708, 720.769, true);
        UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
                document.getPages().get_Item(1), rect);
        underlineAnnotation.setTitle("Aspose User");
        underlineAnnotation.setSubject("Inserted Underline with Quad Points");
        underlineAnnotation.setFlags(AnnotationFlags.Print);
        underlineAnnotation.setColor(Color.getBlue());
        underlineAnnotation.setQuadPoints(new com.aspose.pdf.Point[]{
                new com.aspose.pdf.Point(rect.getLLX(), rect.getLLY()),
                new com.aspose.pdf.Point(rect.getURX(), rect.getLLY()),
                new com.aspose.pdf.Point(rect.getURX(), rect.getURY()),
                new com.aspose.pdf.Point(rect.getLLX(), rect.getURY())
        });

        document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
        document.save(outputFile.toString());
    }
}

Get marked text from underline annotations

Retrieve the actual text content covered by underline annotations. These examples show two approaches: reading the complete marked text as a single string, or processing text fragments individually for detailed analysis.

  1. Open the source PDF Document.
  2. Iterate through underline annotations on the page.
  3. Read either getMarkedText() or getMarkedTextFragments() and print the results.
public static void textUnderlineMarkedTextGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
                System.out.println("Marked text: " + ua.getMarkedText());
            }
        }
    }
}
public static void textUnderlineMarkedFragmentsGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
                for (TextFragment fragment : ua.getMarkedTextFragments()) {
                    System.out.println("Fragment text: " + fragment.getText());
                }
            }
        }
    }
}

Delete underline annotations by title

Remove annotations selectively by filtering on metadata properties like title. This approach enables targeted cleanup of annotations by author or purpose.

  1. Open the source PDF Document.
  2. Filter underline annotations by title.
  3. Delete the matching annotations and save the updated document.
public static void textUnderlineByTitleDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<UnderlineAnnotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
                if ("Aspose User".equals(ua.getTitle())) {
                    toDelete.add(ua);
                }
            }
        }
        for (UnderlineAnnotation ua : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(ua);
        }
        document.save(outputFile.toString());
    }
}

Add and flatten an underline annotation

Convert an interactive underline annotation into permanent page content by flattening it. This prevents further editing while preserving the underline appearance in any PDF viewer.

  1. Open the source PDF Document.
  2. Add an UnderlineAnnotation to the page.
  3. Call flatten() on the annotation and save the output file.
public static void textUnderlineFlattenAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        underlineAnnotation.setTitle("Aspose User");
        underlineAnnotation.setSubject("Inserted Underline to Flatten");
        underlineAnnotation.setFlags(AnnotationFlags.Print);
        underlineAnnotation.setColor(Color.getBlue());

        document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
        underlineAnnotation.flatten();

        document.save(outputFile.toString());
    }
}