0

是WPF还是Winform都没有关系,因为我都试过了,结果是一样的。

我正在使用 WebBrowser 制作 PDF 阅读器。

首先,我添加了引用和指令“使用 mshtml”。然后我像这样加载了 PDf 文件:

       OpenFileDialog dlg = new OpenFileDialog() { Filter = "*.pdf(PDF file)|*.pdf" }; ;
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            webBrowser1.Navigate(dlg.FileName);
        }

然后我尝试在 WebBrowser 中搜索一个字符串,但没有成功:

       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();
                    }
                }
            }
        }

我还在这一行设置了一个断点:

  IHTMLSelectionObject currentSelection = document.selection;

但是断点从未被触发,这意味着变量“document”始终为空。这是为什么?我必须为 WebBrowser 设置文档属性吗?我无法弄清楚导致问题的原因。

我想搜索一个字符串,然后突出显示它。

非常感谢。

4

0 回答 0