Working with text search in UWP PDF Viewer (SfPdfViewer)

18 Aug 20216 minutes to read

Text search can be done in two ways, by using the SearchText method or by using the commands.

In all code snippets found below, ‘buffer’ is the byte array read from the PDF file either using FileOpenPicker or from Assets folder, as illustrated in the Viewing PDF section.

Using search methods

The following code shows how to initiate the text search using the method.

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(buffer);
    pdfViewer.LoadDocument(loadedDocument);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Searches for the text in the PDF Document.
    pdfViewer.SearchText("the");
}
Private Sub Page_Loaded(sender As Object, e As RoutedEventArgs)
    Dim loadedDocument As New PdfLoadedDocument(buffer)
    pdfViewer.LoadDocument(loadedDocument)
End Sub

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    'Searches for the text in the PDF Document.
    pdfViewer.SearchText("the")
End Sub

The following code shows how to search for the next instance.

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(buffer);
    pdfViewer.LoadDocument(loadedDocument);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Searches for the text in the PDF Document.
    pdfViewer.SearchNextText("the");
}
Private Sub Page_Loaded(sender As Object, e As RoutedEventArgs)
    Dim loadedDocument As New PdfLoadedDocument(buffer)
    pdfViewer.LoadDocument(loadedDocument)
End Sub

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    'Searches for the text in the PDF Document.
    pdfViewer.SearchNextText("the")
End Sub

The following code shows how to search for the previous instance.

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(buffer);
    pdfViewer.LoadDocument(loadedDocument);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Searches for the text in the PDF Document.
    pdfViewer.SearchPrevText("the");
}
Private Sub Page_Loaded(sender As Object, e As RoutedEventArgs)
    Dim loadedDocument As New PdfLoadedDocument(buffer)
    pdfViewer.LoadDocument(loadedDocument)
End Sub

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    'Searches for the text in the PDF Document.
    pdfViewer.SearchPrevText("the")
End Sub

Using search commands

The following code shows how to search for the next instance using SearchNextCommand.

<Grid>
        <syncfusion:SfPdfViewerControl Name="pdfViewer"></syncfusion:SfPdfViewerControl>
        <Button Content="Search Next" Command="{Binding ElementName=pdfViewer, Path=SearchNextCommand}" CommandParameter="{Binding Text, ElementName=PageSearchTxtBox}"></Button>
</Grid>

The following code shows how to search for the previous instance using SearchPreviousCommand.

<Grid>
        <syncfusion:SfPdfViewerControl Name="pdfViewer"></syncfusion:SfPdfViewerControl>
        <Button Content="Search Next" Command="{Binding ElementName=pdfViewer, Path=SearchPreviousCommand}" CommandParameter="{Binding Text, ElementName=PageSearchTxtBox}"></Button>
</Grid>

Using asynchronous search methods

The PDF viewer allows the users to perform text search asynchronously using the SearchTextAsync method. The user can also cancel the asynchronous text search when it is in progress.

CancellationTokenSource cts = new CancellationTokenSource();

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(buffer);
    pdfViewer.LoadDocument(loadedDocument);
}

private void SearchButton_Click(object sender, RoutedEventArgs e)
{
    //Searches for the text in the PDF Document.
    pdfViewer.SearchTextAsync(targetText, cts.Token);
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
   cts.Cancel();
}

In the above code sample, the targetText is the input text to be searched and the cancellationToken enables the users to cancel the asynchronous text search.

The following code explains how to search for the next instance of the text asynchronously.

CancellationTokenSource cts = new CancellationTokenSource();

private void SearchNextButton_Click(object sender, RoutedEventArgs e)
{
    //Searches for the next text instance in the PDF Document.
    pdfViewer.SearchNextTextAsync(targetText, cts.Token);
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
   cts.Cancel();
}

The following code explains how to search for the previous instance of the text asynchronously.

CancellationTokenSource cts = new CancellationTokenSource();

private void SearchPreviousButton_Click(object sender, RoutedEventArgs e)
{
    //Searches for the previous instance of the text in the PDF Document.
    pdfViewer.SearchPreviousTextAsync(targetText, cts.Token);
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
   cts.Cancel();
}

NOTE

The text search operations can only be performed after the display of the PDF document in the viewer control and this cannot be initiated immediately after loading the PDF document.