Create, edit, or remove PDF annotations in UWP

Once Nutrient UWP SDK has loaded annotations from a PDF, these annotations will be made available via the Document API.

The API will return a List of IAnnotations. This interface allows you to determine the AnnotationType and several other properties and methods common to all annotations, such as BoundingBox, Id, PageIndex, and ToJson().

The BoundingBox describes the annotation’s location and size on the page. This property is represented by a Rect, with X and Y describing the top-left coordinate of the annotation on the page, and the Width and Height describing the size of the annotation.

Nutrient UWP SDK and Nutrient Web SDK use Instant JSON coordinates. In other words, the origin of the page, “0,0,” is at the top left, with the Y axis value increasing as you move down the page. If you need to convert between these coordinates and PDF coordinates — which have their origin, “0,0,” at the bottom left, with the Y axis value increasing as you move up the page — you can use the conversion utility methods here.

Using the AnnotationType property, you can cast to a specific annotation type to work with its specific APIs:

if (annotation.AnnotationType == AnnotationType.Ink)
{
    var ink = annotation as AnnotationType.Ink;
    return ink.IsSignature;
}

Creating, updating, and deleting annotations

To create an annotation on a page, instantiate the type you want:

var textAnnot = new Text
{
    Contents = "A new text annotation",
    HorizontalAlign = HorizontalAlign.Right,
    VerticalAlign = VerticalAlign.Center,
    BoundingBox = new Rect(10, 10, 300, 300),
    FontSize = 48,
    Bold = true,
    Italic = true,
    FontColor = Colors.Cyan
};

Then you can add it to a page like this:

var createdTextAnnotation = await pdfView.Document.CreateAnnotationAsync(textAnnot);

After it’s added to a page, the new annotations will have a unique Id.

If you wish to update an existing annotation, this can be done like so:

createdTextAnnotation.VerticalAlign = VerticalAlign.Top;
createdTextAnnotation.Opacity = 0.5f;
await pdfView.Document.UpdateAnnotationAsync(createdTextAnnotation);

To delete an annotation, you only need its Id:

await pdfView.Document.DeleteAnnotationAsync(createdTextAnnotation.Id);