Working with annotations in UWP PDF Viewer
14 Dec 20235 minutes to read
PDF Viewer allows user to include annotations in PDF files and provides options to modify or remove the existing annotations. The supported annotations are below.
- Text markup annotations
- Shape annotations
- Ink annotations
- Popup annotations
- Free text annotations.
- Free text callout annotations.
- Stamp annotations.
The individual annotation types are discussed in detail under their own sections.
Annotation events.
PDF viewer provides the following events.
- AnnotationAdded
- AnnotationTapped
- AnnotationSelected
- AnnotationDeselected
- AnnotationRemoved
- AnnotationMovedOrResized
The properties of the annotation involved in the event can be obtained from the AnnotationProperties
property of the event args parameter of each event except AnnotationMovedOrResized. Since the annotation properties are obtained from the same AnnotationProperties
instance for all events except AnnotationMovedOrResized, only the AnnotationAdded event is illustrated below.
The properties of the included annotation can be obtained by casting the AnnotationAddedEventArgs
parameter’s AnnotationProperties property to the property of corresponding annotation type.
SfPdfViewerControl pdfViewer = new SfPdfViewerControl();
pdfViewer.AnnotationAdded += PdfViewer_AnnotationAdded;
private void PdfViewer_AnnotationAdded(object sender, AnnotationAddedEventArgs e)
{
//Check whether the annotation is text markup
if (e.AnnotationProperties is TextMarkupProperties)
{
TextMarkupProperties properties = e.AnnotationProperties as TextMarkupProperties;
//Obtain the annotation type such as Highlight, Underline, Strikethrough
AnnotationMode annotationMode = properties.AnnotationType;
//Obtain color of the annotation
Color color = properties.Color;
//Obtain the opacity value of the annotation
double opacity = properties.Opacity;
//Obtain the page number in which the annotation is added
int pageNumber = properties.PageNumber;
}
}
The properties of the annotation involved in the AnnotationMovedOrResized
event can be obtained from the AnnotationMovedOrResizedEventArgs
parameter of the event handler.
SfPdfViewerControl pdfViewer = new SfPdfViewerControl();
pdfViewer.AnnotationMovedOrResized += PdfViewer_AnnotationMovedOrResized;
private void PdfViewer_AnnotationAdded(object sender, AnnotationMovedOrResizedEventArgs e)
{
//Obtain the new position and size
int newX = e.NewX;
int newY = e.NewY;
int newHeight = e.NewHeight;
int newWidth = e.NewWidth;
//Obtain the old position and size
int oldX = e.OldX;
int oldY = e.OldY;
int oldHeight = e.OldHeight;
int oldWidth = e.OldWidth;
//Obtain the page number in which the annotation is added
int pageNumber = e.PageNumber;
}
Enable and disable selection of annotations
By default, PdfViewer allows the user to select the annotations by tapping on it, this action is followed by the appearance of the selector around the selected annotation. The selection of annotations can be disabled by setting IsReadOnly
property of AnnotationSettings
class to true. The default value of this API will be false.
//Disable the selection of annotations
pdfViewer.AnnotationSettings.IsReadOnly = true;
Setting the author for the annotations.
The Author
property of the IAnnotation
is used to set the name of the author for all supported annotations.
Refer to the following code example.
//Initialize the SfPdfViewer
SfPdfViewerControl pdfViewer = new SfPdfViewerControl();
pdfViewer.AnnotationAdded += PdfViewer_AnnotationAdded;
private void PdfViewer_AnnotationAdded(object sender, Syncfusion.Windows.PdfViewer.AnnotationAddedEventArgs e)
{
if (e.Annotation is IAnnotation)
{
//sets the author name for the added annotation.
e.Annotation.Author = "Syncfusion";
}
}
Deleting Annotations
The PDF viewer supports removing a single annotation and all the annotations in the PDF document.
Remove a selected annotation
The following code snippet illustrates removing a selected annotation from the PDF document.
<Grid> <syncfusion:SfPdfViewerControl Name="pdfViewer"></syncfusion:SfPdfViewerControl>
<Button Content="deleteAnnotationButton" Click="deleteAnnotationButton_clicked” ></Button> </Grid>
pdfViewer.AnnotationSelected += PdfViewer_AnnotationSelected;
IAnnotation annotation;
private void PdfViewer_AnnotationSelected(object sender, AnnotationSelectedEventArgs e)
{
// Get the selected annotation
annotation = e.Annotation;
}
private void deleteAnnotationButton_clicked(object sender, RoutedEventArgs e)
{
//Delete the selected shape annotation
pdfViewer.RemoveAnnotation(annotation);
}
Remove all annotations
The following code snippet illustrates removing all annotations from the PDF.
pdfViewer.ClearAllAnnotations();
How to get the list of annotations present in the PDF?
Use the AnnotationCollection
property to get the list of annotations present in the PDF document. Refer to the following code sample.
//Gets the list annotations present in a PDF.
var annotations = pdfViewer.AnnotationCollection;
How to show or hide the annotations present in the PDF?
Use the AnnotationVisibility
property to change the visibility of the annotations present in a PDF document. By default, the visibility of the annotation is Visibility.Visible
.
//Gets or sets the visibility of the annotations.
pdfViewer.AnnotationVisibility = Visibility.Collapsed;