Lock and Unlock Annotations in .NET MAUI PDF Viewer (SfPdfViewer)

13 Jun 20243 minutes to read

You can lock an annotation to prevent it from being edited. The annotation that has been locked cannot be removed or edited further until it is unlocked. This section will go through the process of locking and unlocking annotations in a PDF document.

Lock all annotations in a document

To lock all annotations in a document, set the IsLocked property of the AnnotationSettings to true. The following example explains how to lock all annotations in a document.

void LockAllAnnotations()
{
    // Lock all the annotations in a PDF document using the [AnnotationSettings](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.PdfViewer.AnnotationSettings.html) property of the [SfPdfViewer](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.PdfViewer.SfPdfViewer.html) instance.
    PdfViewer.AnnotationSettings.IsLocked = true;
}
  • Similarly, to unlock all the annotations, set the IsLocked property value to false.

Lock specific annotation

To lock a specific annotation in a document, access the annotation instance and set the IsLocked property of the annotation to true. The following example explains how to lock the first annotation in a PDF document.

void LockFirstAnnotation()
{
    // Obtain the annotation collection using the [SfPdfViewer](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.PdfViewer.SfPdfViewer.html) instance.
    ReadOnlyObservableCollection<Annotation> annotations = PdfViewer.Annotations;

    // Obtain the first annotation in the annotation collection.
    Annotation firstAnnotation = annotations[0];

    // Lock the annotation.
    firstAnnotation.IsLocked = true;
}
  • Similarly, to unlock the annotation, set theIsLocked property value to false.

Lock specific annotation types

You can also use the AnnotationSettings property to lock a specific annotation type in a document. The following example explains how to lock all the circle annotations in a document by accessing the circle annotation settings. Similarly, you can lock other types of annotation.

void LockCircleAnnotations()
{
    // Obtain the default circle annotation settings using [SfPdfViewer](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.PdfViewer.SfPdfViewer.html) instance.
    ShapeAnnotationSettings circleAnnotationSettings = PdfViewer.AnnotationSettings.Circle;
    
    // Lock all the circle annotations.
    circleAnnotationSettings.IsLocked = true;
}
  • Similarly, to unlock the specific annotation types, set the IsLocked property value to false.

Lock the selected annotation

To lock the selected annotation, access the selected annotation instance and set the IsLocked property of the annotation to true. The selected annotation instance may be obtained from the AnnotationSelected event. The following example explains how to lock the selected annotation in a PDF document.

/// <summary>
/// Locks the selected annotation.
/// </summary>
/// <param name="selectedAnnotation">The selected annotation instance that may be obtained from the annotation selected event<</param>
void LockSelectedAnnotation(Annotation selectedAnnotation)
{
    // Lock the annotation.
    selectedAnnotation.IsLocked = true;
}
  • Similarly, to unlock the selected annotation, set the IsLocked property value to false.