Select and Copy the Text in Xamarin Pdf Viewer (SfPdfViewer)
1 Dec 20236 minutes to read
The Xamarin PDF Viewer supports text selection and copy feature, which allows user to select the text in the PDF document and copy it to the clipboard. This section illustrates about how to use this feature.
Property | Action |
---|---|
IsTextSelectionEnabled | Gets or sets the value that enables or disables the text selection feature in the PDF viewer. This property when set to true enables text selection and vice versa. By default, this property is set to true. |
<syncfusion:SfPdfViewer x:Name="pdfViewerControl" IsTextSelectionEnabled="False"/>
//Text selection feature in the PDF viewer is disabled.
pdfViewerControl.IsTextSelectionEnabled = false;
How to select text?
To select a text in the PDF page, press and hold any word of the text until selection bubbles appear at the top-left and bottom-right corners of its bounds. Then, use the left bubble to select the text at the left and top, and the right bubble to select the text at the right and bottom directions.
How to enable or disable the context menu?
By default, PDF viewer comes with a context menu that will be displayed above the selected text in the PDF document, which has a button (option) to copy the selected text. The display of the context menu can be disabled by setting ShowContextMenu property of the TextSelectionSettings class to false. The below code illustrates the same. By default, context menu will be enabled in the PDF viewer.
//The display of the default context menu for the text selection is disabled.
pdfViewerControl.TextSelectionSettings.ShowContextMenu = false;
How to modify the selection and its handle color?
The color used for text selection and the color of the handle can be customized based on the developer’s requirements. The properties TextSelectionColor and TextSelectionHandleColor of the TextSelectionSettings class can be used to customize them. The below code snippet illustrates the same.
//Customizing the color being displayed while selecting the text from PDF document.
pdfViewerControl.TextSelectionSettings.TextSelectionColor = Color.FromRgba(0, 0, 205, 80);
//Customizing the color of text selection handler displayed while selecting the text from PDF document.
pdfViewerControl.TextSelectionSettings.TextSelectionHandleColor = Color.FromRgb(0, 0, 255);
How to acquire selected text?
The completion of the text selection action would trigger TextSelectionCompleted event. The event argument of this event will contain a copy of the selected text in the String format.
<syncfusion:SfPdfViewer x:Name="pdfViewerControl" InputFileStream="{Binding PdfDocumentStream}" TextSelectionCompleted="PdfViewerControl_TextSelectionCompleted"/>
private void PdfViewerControl_TextSelectionCompleted(object sender, TextSelectionCompletedEventArgs args)
{
//The selected text is acquired and stored in the variable selectedText.
string selectedText = args.SelectedText;
}
How to acquire page number, page bounds and selected region?
The completion of the text selection action would trigger TextSelectionCompleted event. The event argument would contain details about the page number in which the selection operation is performed, bounds of the page and the selection region.
<syncfusion:SfPdfViewer x:Name="pdfViewerControl" InputFileStream="{Binding PdfDocumentStream}" TextSelectionCompleted="PdfViewerControl_TextSelectionCompleted"/>
private void PdfViewerControl_TextSelectionCompleted(object sender, TextSelectionCompletedEventArgs args)
{
//The number of the page in which the selection is performed is acquired.
int pageNumber = args.PageNumber;
//The bounds of the page in which the selection is performed is acquired.
Rectangle pageBounds= args.PageBounds;
//The region of the text being selected is acquired.
Rectangle selectedRegion= args.SelectedRegion;
}
How to get the start index and end index of the selected text?
The completion of the text selection action would trigger the TextSelectionCompleted event. The event argument would contain details about the start index and end index of the selected text.
private void PdfViewerControl_TextSelectionCompleted(object sender, TextSelectionCompletedEventArgs args)
{
//Starting index of the selected text on the page.
int startIndex = args.SelectedTextStartIndex;
//Ending index of the selected text on the page.
int endIndex= args.SelectedTextEndIndex;
}
NOTE
The values of SelectedTextStartIndex and SelectedTextEndIndex properties correspond only to the text present in the page on which the text is selected.
How to customize the text selection context menu?
By default, the text selection context menu contains built-in menu items such as Copy, Highlight, Underline, and Strikethrough. You can add additional items in the text selection context menu by creating an instance of type TextSelectionMenuItem and add the item to the TextSelectionSettings.MenuOptions.Items property.
As the type of this property is ObservableCollection, the following operations can be performed on the text selection context menu items.
- Adding a new item
- Clearing the menu items
- Inserting a new item
- Removing an existing item
Below code illustrates adding a new menu item to the text selection context menu.
TextSelectionMenuItem menuItem = new TextSelectionMenuItem();
//The text to display on the menu item
menuItem.Text = “Find text”;
//The ID to uniquely identify the menu item.
menuItem.Id = "find_text";
pdfViewer.TextSelectionSettings.MenuOptions.Items.Add(menuItem);
How to handle the click event of the menu item?
The TextSelectionMenuItemClicked event is raised when any menu item is clicked. While performing the intended operation when a menu item is clicked, the selected text can be obtained as described in the above section.
pdfViewer.TextSelectionSettings.MenuOptions.TextSelectionMenuItemClicked += PdfViewerControl_TextSelectionMenuItemClicked;
private void PdfViewerControl_TextSelectionMenuItemClicked(object sender, TextSelectionMenuItemClickedEventArgs args)
{
TextSelectionMenuItem menuItem = sender as TextSelectionMenuItem;
switch(menuItem.Id)
{
//Perform the intended operation after identifying the menu item from its ‘Id’ value.
}
}
NOTE
You can also explore our Xamarin.Forms PDF Viewer example to knows the functionalities of each feature.