It doesn't matter if it is in WPF or Winform, because I tried both and the results were the same.
I am making a PDF reader by using WebBrower.
First of all, I added the reference and the directive "using mshtml". Then I loaded the PDf file like this:
OpenFileDialog dlg = new OpenFileDialog() { Filter = "*.pdf(PDF file)|*.pdf" }; ;
if (dlg.ShowDialog() == DialogResult.OK)
{
webBrowser1.Navigate(dlg.FileName);
}
Then I tried to search for a string in the WebBrowser, which did not work:
if (webBrowser1.Document != null)
{
IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
if (document != null)
{
IHTMLSelectionObject currentSelection = document.selection;
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
const string search = "Privacy";//This string is in the PDF file;
if (range.findText(search, search.Length, 2))
{
range.select();
}
}
}
}
I also set a breakpoint at this line:
IHTMLSelectionObject currentSelection = document.selection;
But the breakpoint was never triggered, which means the variable "document" is always null. Why is that? Do I have to set a document property for the WebBrowser? I can't figure out what causes the problem.
I want to search for a string and then highlight it.
Thanks a lot.