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\WinRT XAML 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:
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>
Imports C1.Phone.PdfViewer
•
To write the code in Visual Basic:
Imports C1.Xaml.PdfViewerTo write the code in C#:
using C1.Xaml.PdfViewer;
using C1.Phone.PdfViewer;
To write the code in Visual Basic:
Visual Basic Copy CodeDim btnShowFindText As New ApplicationBarMenuItem("find text") To write the code in C#:
C# Copy CodeApplicationBarMenuItem btnShowFindText = new ApplicationBarMenuItem("find text");
ApplicationBarMenuItem btnShowFindText = new ApplicationBarMenuItem("find text");
To write the code in Visual Basic:
Visual Basic Copy CodebtnShowFindText.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;
}
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;
}
}
}
}
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;
}
}
}
}
Note that you can search for specific text or characters in the document. When it is found, it will be highlighted.