Introduction to Annotations

Annotations

PSPDFKit for Windows supports most common annotation types:

These are standard annotations (as defined in the PDF Reference) that can be read and written by many apps, including Adobe Acrobat and Apple’s Preview app.

Working with Annotations in Code

Once PSPDFKit for Windows 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. PSPDFKit for Windows and PSPDFKit for Web 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:

1
2
3
4
5
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, simply instantiate the type you want:

Copy
1
2
3
4
5
6
7
8
9
10
11
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:

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

After is it 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:

Copy
1
2
3
createdTextAnnotation.VerticalAlign = VerticalAlign.Top;
createdTextAnnotation.Opacity = 0.5f;
await pdfView.Document.UpdateAnnotationAsync(createdTextAnnotation);

To delete an annotation, you only need its Id:

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

Annotation Events

There are several events you can subscribe to in order to be informed of changes to annotations:

Here’s how you can add a handler to be informed of deleted annotations:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
{
    // After the PDF has been loaded.
    pdfView.Document.AnnotationsDeleted += OnAnnotationsDeleted;
}

private void OnAnnotationsDeleted(object sender, IList<IAnnotation> annotations)
{
    foreach (var annotation in annotations)
    {
        Debug.WriteLine($"OnAnnotationsDeleted: {annotation.Id}");
    }
}