Working with free text annotations

16 Dec 2020 / 8 minutes to read

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

Enabling free text 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 PDF page displayed and a popup will appear. Type the text in the popup and click "Ok" to add text to the page. Zooming, panning, and scrolling will be enabled again if the free text annotation is added.

In all code snippets, the term ApplicationContext represents the ApplicationContext property of the MainActivity.

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
pdfViewer.AnnotationMode = AnnotationMode.FreeText;

Disabling free text mode

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

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
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.

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
pdfViewer.FreeTextAnnotationAdded += PdfViewer_FreeTextAnnotationAdded;

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.

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
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 is raised.

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
pdfViewer.FreeTextAnnotationSelected += PdfViewer_FreeTextAnnotationSelected;

When the event is raised, the properties of the selected free text can be obtained from the args parameter of the event handler.

private void PdfViewer_FreeTextAnnotationSelected(object sender, FreeTextAnnotationSelectedEventArgs args)
{
	//Obtain the bounds of the annotation
	Rect bounds = args.Bounds;

	//Obtain the text in the annotation
	string text = args.Text;

	//Obtain the size of the text in the annotation
	float textSize = args.FontSize;

	//Obtain the color of the text
	Color textColor = args.TextColor;

	//Obtain the page number in which the selected annotation is
	int pageNumber = args.PageNumber;
}

Deselecting free text annotations

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

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
pdfViewer.FreeTextAnnotationDeselected += PdfViewer_FreeTextAnnotationDeselected;

Customizing the appearance of free text annotations

You can customize the default text color and size of all 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.FreeText.TextColor property. Refer to the following code.

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
pdfViewer.AnnotationSettings.FreeText.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.FreeText.TextSize property. Refer to the following code example.

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
pdfViewer.AnnotationSettings.FreeText.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;

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 modifying its properties. The following code shows how to change the properties.

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
pdfViewer.FreeTextAnnotationSelected += PdfViewer_FreeTextAnnotationSelected;
changeFreeTextPropertiesButton.Clicked += ChangeFreeTextPropertiesButton_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 ChangeFreeTextPropertiesButton_Clicked(object sender, EventArgs e)
{
	//Change the color of the text
    selectedFreeTextAnnotation.Settings.TextColor = Color.Purple;

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

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

The event FreeTextAnnotationMovedOrResized is raised when you move or resize the selected annotation.

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
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
	Rect oldBounds = args.OldBounds;

	//Retrieve the new bounds of the annotation
	Rect newBounds = args.NewBounds;

	//Retrieve the current page number
	int pageNumber = args.PageNumber;
}

Deleting free text annotations

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

Removing all annotations

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

pdfViewer.ClearAllAnnotations();

Removing a selected free text annotation

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

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);
}

Detecting the removal of a free text

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

SfPdfViewer pdfViewer = new SfPdfViewer(ApplicationContext);
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 
	Rect 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.FontSize;
}