ComponentOne PdfViewer for UWP
In This Topic
    Finding Text in the Document
    In This Topic

    You can easily allow end-users to search for specific text or characters in PDF documents. This topic will walk you through adding the XAML markup and the code to create a search function.

    Note that in this step you will add a PDF file that is included with the ComponentOne Studio for Windows Phone samples, which are by default installed in the Documents folder in the ComponentOne Samples\UWP\General\CS\ControlExplorer\Resources directory. If you choose, you can instead use another PDF file and adapt the steps.

    To customize your project and add a PDF file to the C1PdfViewer control in your application, complete the following steps:

    1. In the Solution Explorer, right-click on the project name and select Add | Existing Item from the menu. The Add Existing Item dialog box will appear.
    2. Select the sample PDF file, C1XapOptimizer.pdf, and click Add.
    3. Right-click the PDF file you just added in the Solution Explorer and select Properties from the menu. Set the BuildAction property to Resource and confirm that the Copy to Output Directory item is set to Do Not Copy.
    4. Add the following markup after the <Grid.RowDefinitions> tags to create the grid structure for the search function and to add a C1PdfViewer to the page.

    To write the markup in XAML:

    <Grid Grid.Row="0" x:Name="panelFindText" >

        <Grid.ColumnDefinitions>

            < ColumnDefinition Width="Auto" />

            < ColumnDefinition Width="*" />

            < ColumnDefinition Width="Auto" />

        </Grid.ColumnDefinitions>

        <Button x:Name="btnClose" Content="x" Click="btnClose_Click" BorderThickness="0" />

        <TextBox x:Name="txtSearch" TextChanged="txtSearch_TextChanged" Grid.Column="1"  />

        <Button Content="next" Click="btnDoSearch_Click" x:Name="btnDoSearch" Grid.Column="2" />

    </Grid>

    <Grid Grid.Row="1">

        <TextBlock x:Name="txtLoading" Text="Loading..." VerticalAlignment="Center" HorizontalAlignment="Center" />

        <c1pdfviewer:C1PdfViewer x:Name="pdfViewer" />

        <Border VerticalAlignment="Top" HorizontalAlignment="Left" Background="{StaticResource PhoneChromeBrush}" CornerRadius="5" Opacity="0.8" Margin="5" Grid.Row="1" >

                < StackPanel Orientation="Horizontal" Margin="4" >

                    < TextBlock Text="{Binding ElementName=pdfViewer, Path=PageNumber}" Foreground="{StaticResource PhoneForegroundBrush}" />

                    < TextBlock Text=" / " Foreground="{StaticResource PhoneForegroundBrush}" />

                    < TextBlock Text="{Binding ElementName=pdfViewer, Path=PageCount}" Foreground="{StaticResource PhoneForegroundBrush}" />

                < /StackPanel>

            < /Border>

        </Grid>

    </Grid>

    1. Switch to Code View by right-clicking the page and selecting View Code from the menu.
    2. Add the following imports statement to the top of the page:

    To write the code in Visual Basic:

      Imports C1.Xaml.PdfViewer

    To write the code in C#:

      using C1.Xaml.PdfViewer;


    1. Add the following code to the main class:

    To write the code in Visual Basic:

      
    Visual Basic
    Copy Code
    Dim btnShowFindText As New ApplicationBarMenuItem("find text")

    To write the code in C#:

      
    C#
    Copy Code
    ApplicationBarMenuItem btnShowFindText = new ApplicationBarMenuItem("find text");


    1. Add the following code beneath the InitializeComponent() method:

    To write the code in Visual Basic:

      
    Visual Basic
    Copy Code

    btnShowFindText.Click += new EventHandler(this.btnShowFindText_Click);

     btnClose_Click(null, new RoutedEventArgs());

      PhoneApp2_Loaded(object, sender, RoutedEventArgs, e);

     {var;

     resource = Application.GetResourceStream(new Uri("PhoneApp2;component/C1XapOptimizer.pdf", UriKind.Relative));

     pdfViewer.LoadDocument(resource.Stream);

     //pdfViewer.PageRendered = (pdfViewer.PageRendered + s2);

     ,e2;

     Unknown=Greater{//if ((e2.TotalRenderedPages >= 2)) {

         txtLoading.Visibility = System.Windows.Visibility.Visible;

         pdfViewer.Visibility = System.Windows.Visibility.Visible;

         void;

         txtSearch_TextChanged(object, sender, TextChangedEventArgs, e);

         btnDoSearch.IsEnabled = (txtSearch.Text.Trim().Length > 0);

         void;

         btnDoSearch_Click(object, sender, RoutedEventArgs, e);

         pdfViewer.FindText = txtSearch.Text;

         pdfViewer.FindNextCommand.Execute(null);

         void;

         btnClose_Click(object, sender, RoutedEventArgs, e);

         panelFindText.Visibility = System.Windows.Visibility.Visible;

         btnShowFindText.IsEnabled = true;

         void;

         btnShowFindText_Click(object, sender, EventArgs, e);

         panelFindText.Visibility = System.Windows.Visibility.Visible;

         btnShowFindText.IsEnabled = false;

         // TODO: #region ... Warning!!! not translated

         IExposeApplicationBarItems;

         Members;

         (IEnumerable

                     < (IApplicationBarMenuItem > ApplicationBarItems));

         yield;

         return btnShowFindText;

     }



    To write the code in C#:

     

    btnShowFindText.Click += new EventHandler(btnShowFindText_Click);

                    btnClose_Click(null, new RoutedEventArgs());

    void PhoneApp2_Loaded(object sender, RoutedEventArgs e)

            {

     var resource = Application.GetResourceStream(new Uri("PhoneApp2;component/C1XapOptimizer.pdf", UriKind.Relative));

                pdfViewer.LoadDocument(resource.Stream);

                //pdfViewer.PageRendered += (s2, e2) =>

                {

                    //if (e2.TotalRenderedPages >= 2)

                    {

                        txtLoading.Visibility = System.Windows.Visibility.Visible;

                        pdfViewer.Visibility = System.Windows.Visibility.Visible;

                    }

                };

            }

              private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)

            {

                btnDoSearch.IsEnabled = (txtSearch.Text.Trim().Length > 0);

            }

            private void btnDoSearch_Click(object sender, RoutedEventArgs e)

            {

                pdfViewer.FindText = txtSearch.Text;

                pdfViewer.FindNextCommand.Execute(null);

            }

            private void btnClose_Click(object sender, RoutedEventArgs e)        {

                panelFindText.Visibility = System.Windows.Visibility.Visible;

                btnShowFindText.IsEnabled = true;

            }

            void btnShowFindText_Click(object sender, EventArgs e)

            {

                panelFindText.Visibility = System.Windows.Visibility.Visible;

                btnShowFindText.IsEnabled = false;

            }

            #region IExposeApplicationBarItems Members

            public IEnumerable<IApplicationBarMenuItem> ApplicationBarItems

            {

                get

                {

                    yield return btnShowFindText;

                }

            }

        }

    }

    1. Press F5 to run your application. The application should resemble the following image:

     

     

    Note that you can search for specific text or characters in the document. When it is found, it will be highlighted.