Working with annotations programmatically

13 Sep 202315 minutes to read

Select an annotation

PDF Viewer allows the users to select the annotation programmatically without user interaction. This functionality returns true, if any annotation is found to be selected. Otherwise, it returns false.

Select an annotation in the PDF file

PDF Viewer allows the users to select the annotation programmatically using SelectAnnotation method. The annotation’s name should pass as a parameter that needs to be selected.

The following code snippet explains how to select an ink annotation using the annotation name.

//Selecting ink annotation with annotation’s name 
private void SelectAnnotation()
{ 
    bool isSelected = pdfViewer.SelectAnnotation(inkAnnotationName);
}

NOTE

Similarly, we can implement it for all other annotations.

Select an annotations at specific page

PDF Viewer also allows the users to select the annotation programmatically using overload SelectAnnotation method with specified the page number on which the annotation is located. The annotation’s name and its page number should pass as a parameter that needs to be selected.

NOTE

For better performance, we can use the method SelectAnnotation with page number overload.

The following code snippet explains how to select an ink annotation on the first page of the document using the annotation name.

//Selecting ink annotation from page 1 with the annotation’s name
private void SelectAnnotation()
{ 
    bool isSelected = pdfViewer.SelectAnnotation(inkAnnotationName, 1);
}

NOTE

Similarly, we can implement it for all other annotations.

Select an annotation in the PDF file and bring it into view

PDF Viewer also allows the user to select and bring an annotation to view programmatically using overload SelectAnnotation method with BringIntoView Parameter. The annotation’s name and true value for BringIntoView should be passed as a parameter to select and bring an annotation into view.

The following code snippet explains how to select an ink annotation using the annotation name and bring them into view.

//Selecting ink annotation with the annotation’s name and bring them into view 
private void SelectAnnotation() 
{
    bool isSelected = pdfViewer.SelectAnnotation(inkAnnotationName, true); 
}

NOTE

Similarly, we can implement it for all other annotations.

Select an annotation at specific page and bring it into view

PDF Viewer also allows the user to select the annotation programmatically and bring it into view using overload SelectAnnotation method, specifying the page number on which the annotation is located. The annotation’s name, its page number and its “true” value for BringIntoView should be passed as a parameter that needs to be selected.

The following code snippet explains how to select an ink annotation on the first page of the document using the annotation name and bring it into view.

//Selecting ink annotation from page 1 with the annotation’s name and bringing into view parameter 
private void SelectAnnotation() 
{  
    bool isSelected = pdfViewer.SelectAnnotation(inkAnnotationName, 1, true);
}

NOTE

Similarly, we can implement it for all other annotations.

NOTE

To bring an annotation into view we need to pass a “true” value for the BringIntoView parameter, providing a “false” value will only select the annotation.

How to get and set name of an annotation

Annotation’s name can be obtained either for newly added annotation or the annotation that already exist. Also, you can modify the Name property to give annotations a unique name. In the following sections, the ink annotation was explained brevity and similarly, you can get and set names for all other annotations.

Get and Set Annotations’ Name

The annotations’ name can be obtained from the annotation changed event. The following code sample explains how to get the annotations’ name when adding it to the document.

//Getting annotation’s name while adding the annotation
private void PdfViewer_InkAnnotationChanged(object sender, InkAnnotationChangedEventArgs e)
{
    if (e.Action == AnnotationChangedAction.Add)
    {
        string inkAnnotationName = e.Name;
    }
}

You can also change the name of an annotation. The following code sample demonstrates how to change the name of an ink annotation. Similarly, you can implement it for all other annotations.

//Set the annotation name 
private void PdfViewer_InkAnnotationChanged(object sender, InkAnnotationChangedEventArgs e) 
{  
    e.Name = YOUR OWN NAME;     
}

NOTE

The annotation name must be unique. If there are multiple annotations with the same name in the document, any functions based on the name will only affect the first one identified.

Getting annotation’s name for existing annotation

The existing annotation’s name can be obtained from LoadedDocument. The following code snippet explains how to get the existing annotation’s name.

//Getting existing annotation’s name
private void PdfViewer_DocumentLoaded(object sender, EventArgs args)
{
    PdfLoadedDocument loadedDocument = pdfViewer.LoadedDocument;
    PdfPageBase page = loadedDocument.Pages[0];
    string annotationName = page.Annotations[0].Name;
}

Modify an annotation

Annotation’s properties can be modified programmatically through Settings in respective annotation changed event.

The following code snippet explains how to modify the selected ink annotation’s properties. Similarly, we can implement for all other annotations.

//Modifying the selected annotation’s properties.
private void PdfViewer_InkAnnotationChanged(object sender, InkAnnotationChangedEventArgs e)
{
    if (e.Action == AnnotationChangedAction.Select)
    {
		PdfViewerInkSettings pdfViewerInkSettings = (e.Settings as PdfViewerInkSettings);
		pdfViewerInkSettings.InkColor = System.Windows.Media.Color.FromArgb(255, 0, 0, 255);
		pdfViewerInkSettings.Opacity = 1f;
		pdfViewerInkSettings.Thickness = 5;
    }
}

Hide an annotation

PDF Viewer allows the user to hide the annotation programmatically without user interaction. This functionality returns true if any annotation is found and hidden, otherwise it returns false. The annotation Name property is used to identify the annotation. Refer this UG link to get and set an annotation Name property.

NOTE

If there are multiple annotations with the same name in the document, it will hide the first one.

Hide an annotation in the PDF file

PDF Viewer allows the user to hide an annotation programmatically using the HideAnnotation method. The annotation name should be passed as a parameter to hide the annotation.

The following code snippet explains how to hide an ink annotation using the annotation name.

//Hide ink annotation using annotation’s name 
private void HideAnnotation() 
{ 
   bool isHidden =  pdfViewer.HideAnnotation(inkAnnotationName); 
}

NOTE

Similarly, we can implement it for all other annotations.

Hide an annotation at specific page

PDF Viewer also allows the user to hide the annotation from a specific page programmatically using the overload HideAnnotation method with a specified page number on which the annotation is located. The annotation name and its page number should be passed as a parameter that needs to be hidden.

NOTE

For better performance, we can use the method HideAnnotation with page number overload.

The following code snippet explains how to hide an ink annotation on the first page of the document using the annotation name.

//Hide ink annotation from page 1 with the annotation’s name 
private void HideAnnotation() { bool isHidden = pdfViewer.HideAnnotation(inkAnnotationName, 1); }

NOTE

Similarly, we can implement it for all other annotations.

Show an annotation

PDF Viewer allows the user to show the hidden annotation programmatically without user interaction. This functionality returns true if any annotation is found and shown, otherwise it returns false. The annotation Name property is used to identify the annotation. Refer this UG link to get and set an annotation Name property.

NOTE

If there are multiple annotations with the same name in the document, it will show the first one.

Show an annotation in the PDF file

PDF Viewer allows the user to show an annotation programmatically that was hidden using the ShowAnnotation method. The annotation name should be passed as a parameter to show the annotation.

The following code snippet explains how to show a hidden ink annotation using the annotation name.

//Show ink annotation using annotation’s name 
private void ShowAnnotation() 
{ 
   bool isShown =  pdfViewer.ShowAnnotation(inkAnnotationName); 
}

NOTE

Similarly, we can implement it for all other annotations.

Show an annotation at specific page

PDF Viewer also allows the user to show the annotation from a specific page programmatically using the overload ShowAnnotation method with a specified page number on which the annotation is located. The annotation name and its page number should be passed as a parameter that needs to be shown.

NOTE

For better performance, we can use the method ShowAnnotation with page number overload.

The following code snippet explains how to show an ink annotation on the first page of the document using the annotation name.

//Show ink annotation from page 1 with the annotation’s name 
private void ShowAnnotation() { bool isShown = pdfViewer.ShowAnnotation(inkAnnotationName, 1); }

NOTE

Similarly, we can implement it for all other annotations.

Delete an annotation

PDF Viewer allows the user to delete an annotation programmatically without user interaction. This functionality returns true if any annotation is found and deleted, otherwise it returns false. The annotation Name property is used to identify the annotation. Refer this UG link to get and set an annotation Name property.

NOTE

If there are multiple annotations with the same name in the document, it will delete the first one.

Delete an annotation in the PDF file

PDF Viewer allows the user to delete an annotation programmatically using the DeleteAnnotation method. The annotation name should be passed as a parameter to delete the annotation.

The following code snippet explains how to delete an ink annotation using the annotation name.

//delete ink annotation using annotation’s name 
private void DeleteAnnotation()
{ 
   bool isDeleted =  pdfViewer.DeleteAnnotation(inkAnnotationName); 
}

NOTE

Similarly, we can implement it for all other annotations.

Delete an annotation at specific page

PDF Viewer also allows the user to delete the annotation from a specific page programmatically using the overload DeleteAnnotation method with a specified page number on which the annotation is located. The annotation name and its page number should be passed as a parameter that needs to be deleted.

NOTE

For better performance, we can use the method DeleteAnnotation with page number overload.

The following code snippet explains how to delete an ink annotation on the first page of the document using the annotation name.

//Delete ink annotation from page 1 with the annotation’s name
private void DeleteAnnotation() { bool isDeleted = pdfViewer.DeleteAnnotation(inkAnnotationName, 1); }

NOTE

Similarly, we can implement it for all other annotations.

Delete all annotations

The PDF Viewer enables users to programmatically remove all annotations from a document without requiring any user interaction. This option helps users to remove unwanted annotations from the document.

Delete all annotations in the PDF file

PDF Viewer allows the user to delete all annotations programmatically using the ClearAllAnnotations method.

The following code snippet explains how to delete all annotations.

//delete all annotations
private void DeleteAnnotations()
{ 
   pdfViewer.ClearAllAnnotations(); 
}
'delete all annotations
private void DeleteAnnotations()
{ 
   pdfViewer.ClearAllAnnotations()
}

Delete all annotations at specific page

PDF Viewer also allows the user to delete all annotations from a specific page programmatically using the overload ClearAllAnnotations method with a specified page number on which the annotations are located. The page number which we pass as a parameter, its annotations get deleted.

The following code snippet explains how to delete all annotations on the first page of the PDF document.

//Delete all annotations from page 1
private void DeleteAnnotation() 
{ 
    pdfViewer.ClearAllAnnotations(1); 
}
'delete all annotations from page 1
private void DeleteAnnotations()
{ 
   pdfViewer.ClearAllAnnotations(1)
}

Update the annotation’s modified date

PDF Viewer allows the user to update the ModifiedDate of an annotation programmatically without user interaction. With this feature, users can alter the modified date and time according to their preferences.

How to update the modified date of an annotation using default annotation settings?

User can include a ModifiedDate field when adding an annotation to a PDF document using default annotation settings.

The following code snippet explains how to set the modified date field of an ink annotation to be included. Similarly, we can implement it for all other annotations.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    pdfviewer.Load("Input.pdf");
    pdfviewer.InkAnnotationSettings.ModifiedDate = new DateTime(2020, 12, 1, 1, 1, 1);
}
private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
{
    pdfviewer.Load("Input.pdf")
    pdfviewer.InkAnnotationSettings.ModifiedDate = new DateTime(2020, 12, 1, 1, 1, 1)
}

How to update the modified date of an annotation using annotation changed event settings?

User can include a ModifiedDate field of an annotation to a PDF document using annotation changed event settings.

The following code snippet explains how to set the modified date field of an ink annotation using ink annotation changed event settings. Similarly, we can implement it for all other annotations.

private void PdfViewer_InkAnnotationChanged(object sender, InkAnnotationChangedEventArgs e)
{
    e.Settings.ModifiedDate = new DateTime(2023, 12, 1, 1, 1, 1);
}
private Sub PdfViewer_InkAnnotationChanged(sender As Object, e As InkAnnotationChangedEventArgs)
{
    e.Settings.ModifiedDate = new DateTime(2023, 12, 1, 1, 1, 1)
}

NOTE

You can refer to our WPF PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our WPF PDF Viewer example to know how to render and configure the pdfviewer.