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);
annotationToolbarItems.RemoveAt(0);
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);