Free text annotations in Xamarin Pdf Viewer (SfPdfViewer)

23 Jun 202112 minutes to read

Xamarin PDF Viewer allows you to include free text annotations in a PDF document and provides options to modify or remove the existing ones.

Adding free text annotations using toolbar

Enabling free text annotation mode

Set the AnnotationMode property of the PDF viewer to FreeText. After setting the annotation mode, the zooming, panning, and scrolling will be disabled. Tap anywhere on the displayed PDF page, a popup will appear. Type the text in the popup and click "Ok" to add text to the page. Once the free text annotation is added, the zooming, panning, and scrolling will be enabled again.

<syncfusion:SfPdfViewer x:Name="pdfViewer"/>
<Button x:Name="freeTextAnnotationButton" Command="{Binding AnnotationModeCommand, Source={x:Reference Name=pdfViewer}}" CommandParameter="FreeText" />
SfPdfViewer pdfViewer = new SfPdfViewer();
pdfViewer.AnnotationMode = AnnotationMode.FreeText;

Disabling free text annotation mode

Setting the annotation mode to None disables the free text mode.

<syncfusion:SfPdfViewer x:Name="pdfViewer"/>
<Button x:Name="resetAnnotationButton" Command="{Binding AnnotationModeCommand, Source={x:Reference Name=pdfViewer}}" CommandParameter="None" />
pdfViewer.AnnotationMode = AnnotationMode.None;

Detecting the inclusion of free text annotations

The event FreeTextAnnotationAdded will be raised when a free text annotation is added to the PDF.

<syncfusion:SfPdfViewer x:Name="pdfViewer" FreeTextAnnotationAdded="PdfViewer_FreeTextAnnotationAdded"/>
pdfViewer.FreeTextAnnotationAdded += PdfViewer_FreeTextAnnotationAdded;

Adding free text annotations programmatically

By AddAnnotation method , You can add the free text annotations programmatically. The created free text annotation object passed as a parameter. The FreeTextAnnotation instance acquires the text, page number and bounds as the parameters.

The following code sample illustrates the same.

string text = "Syncfusion";
           
//Creates the free text annotation
FreeTextAnnotation freeTextAnnotation = new FreeTextAnnotation(text, 1, new Rectangle(100, 100, 100, 50));           

//Add the free text annotation to the specified page 
pdfViewer.AddAnnotation(freeTextAnnotation);

Detecting tap on free text annotations

Tapping a free text annotation selects it or deselects if it is already selected. The event FreeTextAnnotationTapped is raised when a free text is tapped.

<syncfusion:SfPdfViewer x:Name="pdfViewer" FreeTextAnnotationTapped="PdfViewer_FreeTextAnnotationTapped"/>
pdfViewer.FreeTextAnnotationTapped += PdfViewer_FreeTextAnnotationTapped;

Selecting free text annotations

You can select a free text annotation by tapping on it. When a free text is selected, the FreeTextAnnotationSelected event will be raised. The properties of the selected free text can be retrieved using the args parameter of the event handler.

private void PdfViewer_FreeTextAnnotationSelected(object sender, FreeTextAnnotationSelectedEventArgs args)
{
	//Get the bounds
	Rectangle bounds = args.Bounds;

	//Get the page number on which the deselected free text is
	int pageNumber = args.PageNumber;

	//Get the text of the free text annotation
	string text = args.Text;

	//Get the text color
	Color textColor = args.TextColor;

	//Get the text size
	float textSize = args.TextSize;
}

Selecting free text annotation programmatically

By SelectAnnotation method, You can select the free text annotation programmatically. The specified free text annotation object passed as a parameter.

The following code sample illustrates the same.

//Selects the specified free text annotation
pdfViewer.SelectAnnotation(freetextAnnotation);

NOTE

Once SelectAnnotation method is called and as long as the annotation stays selected, the SelectedAnnotation property will return the same instance as the parameter of this method.

Deselecting free text annotations

You can deselect a selected free text annotation by tapping on it or somewhere on the PDF page. Deselection can be detected using the FreeTextAnnotationDeselected event.

<syncfusion:SfPdfViewer x:Name="pdfViewer" FreeTextAnnotationDeselected="PdfViewer_FreeTextAnnotationDeselected"/>
pdfViewer.FreeTextAnnotationDeselected += PdfViewer_FreeTextAnnotationDeselected;

Deselecting free text annotation programmatically?

By DeselectAnnotation method, You can deselect the free text annotation. The specified free text annotation object passed as a parameter.

The following code sample illustrates the same.

//Deselects the specified free text annotation 
pdfViewer.DeselectAnnotation(freetextAnnotation);

NOTE

There is no effect in calling DeselectAnnotation method, if the given annotation is not selected. Once this method is called, the SelectedAnnotation property will return null until any other annotation gets selected.

Customizing the appearance of free text annotations

You can customize the default text color and text size of free text annotations to be added. This will not affect the already added free text annotations. The appearance of a selected free text can also be modified.

Setting the default text color

You can set the default text color of the free text annotations by using the SfPdfViewer.AnnotationSettings.FreeTextAnnotationSettings.TextColor property. Refer to the following code.

pdfViewer.AnnotationSettings.FreeTextAnnotationSettings.TextColor = Color.Red;

Setting the default text size

You can set the default text size of the free text annotations by using the SfPdfViewer.AnnotationSettings.FreeTextAnnotationSettings.TextSize property. Refer to the following code example.

pdfViewer.AnnotationSettings.FreeTextAnnotationSettings.TextSize = 4;

Setting the default background-color

You can set the background color for the free text annotations using the SfPdfViewer.AnnotationSettings.FreeText.FillColor property. The default value is set to Color.Transparent.

//Setting the background color of the free text annotation

pdfViewerControl.AnnotationSettings.FreeText.FillColor = Color.LightBlue;

Setting the default minimum size

By the SfPdfViewer.AnnotationSettings.FreeText.MinimumSize property, You can set the minimum size to which the free text annotations could be resized.

Refer the following code example:

//Sets the minimum size for the free text annotations
pdfViewerControl.AnnotationSettings.FreeText.MinimumSize = new Size(10, 10);

Changing the properties of a selected free text

You can change the properties of the selected annotation by casting the sender parameter of the FreeTextAnnotationSelected event handler to FreeTextAnnotation and modify its properties. The following code shows how to change the properties.

Button changeFreeTextPropertiesButton = new Button();
changeFreeTextPropertiesButton.Clicked += changeFreeTextPropertiesButton_Clicked;

FreeTextAnnotation selectedFreeTextAnnotation;

private void PdfViewer_FreeTextAnnotationSelected(object sender, FreeTextAnnotationSelectedEventArgs args)
{
	//Cast the sender object to FreeTextAnnotation
    selectedFreeTextAnnotation = sender as FreeTextAnnotation;
}

private void changeFreeTextPropertiesButton_Clicked(object sender, EventArgs e)
{
	//Change the color of the text
    selectedFreeTextAnnotation.Settings.TextColor = Color.Blue;

	//Change the size of the text
	selectedFreeTextAnnotation.Settings.TextSize = 7;
}

Moving or resizing free text annotations

To move or resize the annotation, it should be selected. After the selector appears, tapping and dragging anywhere inside the selector will move the annotation. Tapping on the bubbles around the selector and dragging will resize the annotation.

Detecting the move or resize of a free text annotation

The event FreeTextAnnotationMovedOrResized will be raised when you move or resize the free text annotation.

<syncfusion:SfPdfViewer x:Name="pdfViewer" FreeTextAnnotationMovedOrResized="PdfViewer_FreeTextAnnotationMovedOrResized"/>
pdfViewer.FreeTextAnnotationMovedOrResized += PdfViewer_FreeTextAnnotationMovedOrResized;

The properties of the moved or resized free text can be obtained from the args parameter of the event handler.

private void PdfViewer_FreeTextAnnotationMovedOrResized(object sender, FreeTextAnnotationMovedOrResizedEventArgs args) 
{ 
     //Retrieve the old bounds of the annotation
     Rectangle oldBounds = args.OldBounds;

     //Retrieve the new bounds of the annotation
     Rectangle newBounds = args.NewBounds;
	 
	 //Retrieve the page number in which the free text is
     int pageNumber = args.PageNumber;
}

Deleting free text annotations

PDF viewer can remove a selected annotation or all annotations in the PDF document.

Removing a selected free text annotation

The following code snippet illustrates removing a selected free text from the PDF document.

<syncfusion:SfPdfViewer x:Name="pdfViewer" FreeTextAnnotationSelected="PdfViewer_FreeTextAnnotationSelected"/>
<Button x:Name="deleteFreeTextAnnotationButton" Grid.Row="1" Clicked="deleteFreeTextAnnotationButton_Clicked" />
FreeTextAnnotation selectedFreeTextAnnotation;

private void PdfViewer_FreeTextAnnotationSelected(object sender, FreeTextAnnotationSelectedEventArgs args)
{
	//Cast the sender object to free text annotation.
	selectedFreeTextAnnotation = sender as FreeTextAnnotation;
}

private void deleteFreeTextAnnotationButton_Clicked(object sender, EventArgs e)
{
	//Delete the selected free text annotation
	pdfViewer.RemoveAnnotation(selectedFreeTextAnnotation);
}

Removing all annotations

The ClearAllAnnotations method can be used to delete all annotations irrespective of their types from the PDF.

<syncfusion:SfPdfViewer x:Name="pdfViewer" />
<Button x:Name="deleteAllAnnotationsButton" Command="{Binding ClearAllAnnotationsCommand, Source={x:Reference Name=pdfViewer}} />
private void DeleteAllAnnotationsButton_Clicked(object sender, EventArgs e)
{
	pdfViewer.ClearAllAnnotations();
}

Detecting the removal of a free text

The event FreeTextAnnotationRemoved will be raised when a free text annotation is removed from the PDF.

<syncfusion:SfPdfViewer x:Name="pdfViewer" FreeTextAnnotationRemoved="PdfViewer_FreeTextAnnotationRemoved"/>
pdfViewer.FreeTextAnnotationRemoved += PdfViewer_FreeTextAnnotationRemoved;

The properties of the removed free text can be obtained from the args parameter of the event handler.

private void PdfViewer_FreeTextAnnotationRemoved(object sender, FreeTextAnnotationRemovedEventArgs args)
{
	//Get the bounds 
	Rectangle bounds = args.Bounds;
	//Get the page number on which the deselected free text is 
	int pageNumber = args.PageNumber;
	//Get the text of the free text annotation 
	string text = args.Text;
	//Get the text color 
	Color textColor = args.TextColor;
	//Get the text size 
	float textSize = args.TextSize;
}

How to enable or disable free text annotation interaction?

The interaction operation can be enabled or disabled for free text annotation alone by setting the IsLocked API to false or true respectively.

For example, the following code disables the interaction operations for all free text annotations in the PDF. But other annotation types can be selected, moved, resized, or removed.

//Disable the free text annotation interaction
pdfViewerControl.AnnotationSettings.FreeText.IsLocked = true;

The interaction with free text annotation types will be allowed only if the SfPdfViewer.AnnotationSettings.IsLocked API is set to false. The following code does not allow the interactions with free text annotations, although the IsLocked property of the free text annotation is set to false.

//Disables the free text annotation interaction, though its 'IsLocked' property is set to ‘false’ 
pdfViewerControl.AnnotationSettings.IsLocked = true;
pdfViewerControl.AnnotationSettings.FreeText.IsLocked = false;

How to get and set the name of the free text annotations?

The PDF Viewer allows the users to get and set the name of free text annotations through the Name API.

The following code sample explains modifying the name of the free text annotation in the FreeTextAnnotationAdded event.

private void PdfViewerControl_ FreeTextAnnotationAdded (object sender, FreeTextAnnotationAddedEventArgs args)
{
//Sets the name for the annotation.
(sender as FreeTextAnnotation).Name = "FreeText1";           
}

NOTE

For illustration purposes, we have only provided the sample for modifying the name of the free text annotation in the FreeTextAnnotationAdded event. But this can be done in all other events as well.

NOTE

You can also explore our Xamarin.Forms PDF Viewer example to knows the functionalities of each feature.