Working with Annotations in UWP PDF Viewer

13 Jul 20265 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.

  1. Text markup annotations
  2. Shape annotations
  3. Ink annotations
  4. Popup annotations
  5. Free text annotations.
  6. Free text callout annotations.
  7. Stamp annotations.

The individual annotation types are discussed in detail under their own sections.

Annotation events.

PDF viewer provides the following events.

  1. AnnotationAdded
  2. AnnotationTapped
  3. AnnotationSelected
  4. AnnotationDeselected
  5. AnnotationRemoved
  6. AnnotationMovedOrResized

The properties of the annotation involved in an event can be obtained from the AnnotationProperties property of the event args parameter. This pattern is shared by all events except AnnotationMovedOrResized, so only the AnnotationAdded event is illustrated below.

To obtain properties for a specific annotation type, cast AnnotationProperties to the corresponding type (for example, TextMarkupProperties).

  • C#
  • 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.

  • C#
  • 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, the SfPdfViewer control allows users to select annotations by tapping on them, and a selector appears around the selected annotation. The selection of annotations can be disabled by setting the IsReadOnly property of the AnnotationSettings class to true. The default value of this property is false.

  • C#
  • //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.

  • C#
  • //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 removes 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 removes all annotations from the PDF.

  • C#
  • 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.

  • C#
  • //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 annotations is Visibility.Visible.

  • C#
  • //Gets or sets the visibility of the annotations.
    pdfViewer.AnnotationVisibility = Visibility.Collapsed;

    See Also