Customizing the Annotation Toolbar in the UWP PDF Viewer

It’s possible to add, remove, and reorder all the built-in annotation buttons on the toolbar.

Adding an Annotation Toolbar Item

The next section covers how to add an annotation toolbar item.

Built-In Annotation Toolbar Items

The following list contains all the built-in annotation toolbar items:

  • StrokeColorAnnotationToolbarItem

  • FillColorAnnotationToolbarItem

  • BackgroundColorAnnotationToolbarItem

  • OpacityAnnotationToolbarItem

  • LineWidthAnnotationToolbarItem

  • BlendModeAnnotationToolbarItem

  • SpacerAnnotationToolbarItem

  • DeleteAnnotationToolbarItem

  • AnnotationNoteAnnotationToolbarItem

  • BorderColorAnnotationToolbarItem

  • BorderWidthAnnotationToolbarItem

  • BorderStyleAnnotationToolbarItem

  • ColorAnnotationToolbarItem

  • LinecapsDasharrayAnnotationToolbarItem

  • LineStyleAnnotationToolbarItem

  • FontAnnotationToolbarItem

  • OverlayTextAnnotationToolbarItem

  • OutlineColorAnnotationToolbarItem

  • ApplyRedactionsAnnotationToolbarItem

  • NoteIconAnnotationToolbarItem

  • ClockwiseRotationAnnotationToolbarItem

  • CounterClockwiseRotationAnnotationToolbarItem

The same list can be found in the API reference.

Before modifying the annotation toolbar, specify which annotation toolbar to modify with the AnnotationType enumeration.

The following code shows how to add a built-in annotation item to the Text annotation toolbar:

AnnotationType at = AnnotationType.Text;
var annotationToolbarItems = PdfView.GetAnnotationToolbarItems(at);
annotationToolbarItems.Add(new ColorAnnotationToolbarItem());
await PdfView.SetAnnotationToolbarItemsAsync(at, annotationToolbarItems);

In some instances, you might want to configure how the chosen annotation toolbar looks or set it up in a particular order. To do this, create an IList of IAnnotationToolbarItem items and pass it to PdfView.SetAnnotationToolbarItemsAsync. The ordering of the buttons is determined by the order of the container:

AnnotationType at = AnnotationType.Text;
var defaultAnnotationToolbarItems = new List<IAnnotationToolbarItem>
    new FontAnnotationToolbarItem(),
    new SpacerAnnotationToolbarItem(),
    new OpacityAnnotationToolbarItem(),
    new SpacerAnnotationToolbarItem(),
    new BackgroundColorAnnotationToolbarItem(),
    new SpacerAnnotationToolbarItem(),
    new DeleteAnnotationToolbarItem()
await PdfView.SetAnnotationToolbarItemsAsync(at, defaultAnnotationToolbarItems);

Removing an Annotation Toolbar Item

To remove an item from the annotation toolbar, use this code:

AnnotationType at = AnnotationType.Ink;
var annotationToolbarItems = PdfView.GetAnnotationToolbarItems(at);
await PdfView.SetAnnotationToolbarItemsAsync(at, annotationToolbarItems);

The code above removes the first item of the annotation toolbar (the item at the 0 position of the annotationToolbarItems list).

Extra logic can be applied to find the required item and remove it from the list before SetAnnotationToolbarItemsAsync.

Reordering the Annotation Toolbar Items

The order of the toolbar items is determined by the order said item is inserted into the annotationToolbarItems list. This means that to reorder the annotation toolbar items, you have to reorder the items of the annotationToolbarItems list.

The following example shows how to randomly shuffle the items on the Stamp annotation toolbar:

AnnotationType at = AnnotationType.Stamp;
var annotationToolbarItems = PDFView.GetAnnotationToolbarItems(atype);
var shuffledList = annotationToolbarItems.OrderBy(a => Guid.NewGuid()).ToList();
await PDFView.SetAnnotationToolbarItemsAsync(at, shuffledList);