I want to extract the searched text from a pdf doc and display it. I am using this code but not getting the result not even the searched text while debugging. Can anyone help with the correct code or logic. Thanks!
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="230" TextChanged="textBox_TextChanged" Margin="20,20,0,0"/>
<Button x:Name="button" Content="Search" HorizontalAlignment="Left" Margin="285,20,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
<Grid Margin="10,60,10.4,9.8">
<ItemsControl Name="pdftext">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" />
<ProgressBar Grid.Column="1" Minimum="0" Maximum="100" Value="{Binding Completion}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Cs page:
privatevoid button_Click(object sender, RoutedEventArgs e)
{
string filename = System.AppDomain.CurrentDomain.BaseDirectory;
filename =@"D:\Tourists.pdf";
if (File.Exists(filename))
{
try
{
StringBuilder text =newStringBuilder();
PdfReader pdfReader =newPdfReader(filename);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
ITextExtractionStrategy strategy =newSimpleTextExtractionStrategy();
string currentText =PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
text.Append(System.Environment.NewLine);
text.Append("\n Page Number:"+ page);
text.Append(System.Environment.NewLine);
currentText =Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default,Encoding.UTF8,Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
pdfReader.Close();
}
pdftext.DataContext += text.ToString();
}
catch (Exception ex)
{
MessageBox.Show("Error: "+ ex.Message, "Error");
}
}
}