Articles in this section
Category / Section

How to create custom search toolbar to perform text search operation in PDF Viewer

2 mins read

This KB article explains the creation of a custom search toolbar to perform the text search operation using the PdfViewer control.

 

To start with, add two rows in MainPage.Xaml of the project. The first row should contain a custom search toolbar that has a search bar, previous, and next button. The next and previous button is used to search the next and the previous operation respectively, whereas the search bar used to search a text in the loaded PDF document. In the second row, add the SfPdfViewer control with the built-in toolbar disabled.

 

XAML

 <Grid x:Name="mainGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
            <Grid x:Name="toolbar" Grid.Row="0" BackgroundColor="#E9E9E9" HorizontalOptions="Fill" VerticalOptions="Fill">
                <Grid.ColumnDefinitions>                                     
                    <ColumnDefinition Width="5*" />
                    <ColumnDefinition Width="4*" />
                    <ColumnDefinition Width="5*" />
            </Grid.ColumnDefinitions>             
             <SearchBar x:Name="textSearch" Grid.Column="0" SearchButtonPressed="textSearch_SearchButtonPressed" TextChanged= “textSearch_TextChanged” />
            <Button x:Name="searchPrev" Text="Previous" Grid.Column="1" Clicked="searchPrev_Clicked"></Button>
            <Button x:Name="searchNext" Text="Next" Grid.Column="2" Clicked="searchNext_Clicked"></Button>
            </Grid>        
        <Grid x:Name="pdfViewerGrid" Grid.Row="1">
            <syncfusion:SfPdfViewer x:Name="pdfViewerControl" IsToolbarVisible="False" InputFileStream="{Binding PdfDocumentStream}"/>
        </Grid>
    </Grid>

 

C#

 
private void textSearch_SearchButtonPressed(object sender, EventArgs e)
{
         searchText = (sender as SearchBar).Text;
// Performs text search operation
     if (searchText != string.Empty)
          pdfViewerControl.SearchText(searchText);
 
}
 
private void searchNext_Clicked(object sender, EventArgs e)
{
 
     //Perform search next text operation 
    if (searchText != null && searchText != string.Empty)
             pdfViewerControl.SearchNext();
 
}
 
private void searchPrev_Clicked(object sender, EventArgs e)
{
     //Perform search previous text operation
       if (searchText != null && searchText != string.Empty)
             pdfViewerControl.SearchPrevious();
 
}
 
private void textSearch_TextChanged(object sender, TextChangedEventArgs e)
{
// Cancel the search operation when search bar text changed
            pdfViewerControl.CancelSearch();
}

 

Now, wire up the SearchCompletedEvent in the constructor of MainPage.Xaml. Whenever the user performs the search operation using the custom search toolbar, this event will be triggered when searching text instances complete one full cycle of search or when it does not find any match for the searched text in the loaded PDF document. The event argument would contain NoMatchFound and NoMoreOccurrenceFound properties.

When the NoMatchFound property is set to true, it means that the PDF Viewer could not find any match for the searched text in the PDF document.

When the NoMoreOccurrenceFound property is set to true, it means that the PDF Viewer had completed one full cycle of search and has hit the first instance again.

Based on the value obtained from the above properties, you can set up a customized display alert message to show either there is no more search occurrence found or no match found for the searched text in the loaded PDF document. Refer to the following code sample.

C#

public MainPage()
{
InitializeComponent();
pdfViewerControl.SearchCompleted += PdfViewerControl_SearchCompleted;
}
 
 
private void PdfViewerControl_SearchCompleted(object sender, Syncfusion.SfPdfViewer.XForms.TextSearchCompletedEventArgs args)
{
//When PDF viewer could not find any match for the searched text in the loaded PDF document
if (args.NoMatchFound)
{
 
Device.BeginInvokeOnMainThread(() =>
{
 
DisplayAlert("Information", "No matches were found", "OK");
 
});
 
}
else if (args.NoMoreOccurrence)
{
//When the PDF viewer had completed one full cycle of search and hit the first instance again.Device.BeginInvokeOnMainThread(() =>
{
 
DisplayAlert("Information", "No more occurrence found", "OK");
});
 
}
 
}
 

 

Sample:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/SearchToolbar90244763.zip

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied