Finding Text in the Document

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 or MyDocuments folder in the ComponentOne Samples\Studio for Windows Phone\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.

<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>

 

5.   Switch to Code View by right-clicking the page and selecting View Code from the menu.

6.   Add the following imports statement to the top of the page:

      Visual Basic

C#

7.   Add the following code to the main class:

      Visual Basic

C#

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

      Visual Basic

      C#

9.   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.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.