0

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.

KaKaShi_CantAim
  • 37
  • 1
  • 12
  • I don't know the exact details, but I know if you told an IFrame in IE to navigate to a PDF it would create an instance of the web browser control and then create an activeX or object tag or some other mechanism for handling PDF. As I understand it, IE/ web browser control does not handle PDF's natively instead it instantiates something else to handle it for it. – Alexander Ryan Baggett Jan 13 '17 at 02:58
  • @AlexanderRyanBaggett Thanks for the reply. I did some research too, and ended up with the same conclusion as yours. – KaKaShi_CantAim Jan 13 '17 at 06:22

0 Answers0