WPF か Winform かは関係ありません。両方を試してみた結果は同じだったからです。
WebBrowerを使ってPDFリーダーを作っています。
まず、「using 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" は常に null です。何故ですか?WebBrowser のドキュメント プロパティを設定する必要がありますか? 問題の原因がわかりません。
文字列を検索して強調表示したい。
どうもありがとう。