Annotate documents with GroupDocs.Annotation

GroupDocs.Annotation adds review markup to documents — areas, arrows, highlights, strikeouts, text fields, watermarks and threaded replies — and reads back the annotations already on a file. It is the API behind a document review or collaboration feature.

Add an area annotation

An area annotation boxes a region of a page and attaches a comment to it. setBox takes the rectangle in points, and setPageNumber is zero-based.

import com.groupdocs.annotation.Annotator;
import com.groupdocs.annotation.models.Rectangle;
import com.groupdocs.annotation.models.annotationmodels.AreaAnnotation;

try (Annotator annotator = new Annotator("contract.docx")) {
    AreaAnnotation area = new AreaAnnotation();
    area.setBox(new Rectangle(100, 100, 200, 100));
    area.setBackgroundColor(65535);
    area.setMessage("Please confirm these figures");
    area.setPageNumber(0);
    area.setOpacity(0.7);

    annotator.add(area);
    annotator.save("annotation-add-area.pdf");
}

contract.docx is the sample file used in this example. Click here to download it.

Binary file (PDF, 16 KB)

Download full output

Note
Colours are 32-bit integers rather than Color objects — 65535 is yellow. setPageNumber counts from zero, so the first page is 0.

Highlight text

A highlight annotation is bounded by four corner points rather than a rectangle, because a highlight can follow text that wraps across lines and is not always a neat box.

import com.groupdocs.annotation.Annotator;
import com.groupdocs.annotation.models.Point;
import com.groupdocs.annotation.models.annotationmodels.HighlightAnnotation;
import java.util.Arrays;
import java.util.List;

try (Annotator annotator = new Annotator("contract.docx")) {
    // Corner points of the region to highlight
    List<Point> points = Arrays.asList(
        new Point(80, 730),
        new Point(240, 730),
        new Point(80, 710),
        new Point(240, 710));

    HighlightAnnotation highlight = new HighlightAnnotation();
    highlight.setPoints(points);
    highlight.setBackgroundColor(65535);
    highlight.setMessage("Check this clause against the contract");
    highlight.setPageNumber(0);
    highlight.setOpacity(0.5);

    annotator.add(highlight);
    annotator.save("annotation-highlight.pdf");
}

contract.docx is the sample file used in this example. Click here to download it.

Binary file (PDF, 15 KB)

Download full output

Read annotations back

get returns the annotations already on a document — how you load an existing review into your own UI.

import com.groupdocs.annotation.Annotator;
import com.groupdocs.annotation.models.Rectangle;
import com.groupdocs.annotation.models.annotationmodels.AnnotationBase;
import com.groupdocs.annotation.models.annotationmodels.AreaAnnotation;
import java.util.List;

// Annotate first, so there is something to read back
try (Annotator annotator = new Annotator("contract.docx")) {
    AreaAnnotation area = new AreaAnnotation();
    area.setBox(new Rectangle(100, 100, 200, 100));
    area.setMessage("Please confirm these figures");
    area.setPageNumber(0);

    annotator.add(area);
    annotator.save("annotation-extract.pdf");
}

try (Annotator annotator = new Annotator("annotation-extract.pdf")) {
    List<AnnotationBase> annotations = annotator.get();

    System.out.println("Annotations found: " + annotations.size());

    for (AnnotationBase annotation : annotations) {
        System.out.println(annotation.getClass().getSimpleName()
            + " on page " + annotation.getPageNumber()
            + ": " + annotation.getMessage());
    }
}

contract.docx is the sample file used in this example. Click here to download it.

Binary file (PDF, 16 KB)

Download full output

Learn more

GroupDocs.Annotation supports eighteen annotation types, threaded replies for multi-round review, updating and removing existing annotations, importing and exporting annotations as XML, page previews, and document version history.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.