1

I would like to open a PDF file after clicking a hyperlink. I've tried to add a href tag but it did not work with WebBrowser. Besides that, if this is impossible, is there any Toolbox for replacement?

pic


private void btSearch_Click(object sender, EventArgs e)
{
    this.webBro.DocumentText = initDoc();
    MessageBox.Show("Completed", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private string GetBodyText()
{
    StringBuilder strB = new StringBuilder();

    string[] filePaths = Directory.GetFiles(@"C:\Users\huydq\Downloads\Documents\", "*.pdf", SearchOption.AllDirectories);
    for (int i = 0; i < filePaths.Length; i++)
    {
        string settext = GetTextFromPDF(filePaths[i]).Replace("\r\n", " ").Replace("\n", " ").Replace("\r", " ").Replace("\t", " ");
        string searchText = tbSearch.Text;
        int prefix = 50, postfix = 50;
        int index = settext.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);

        if (index >= 0)
        {
            string foundText = settext.Substring(index, searchText.Length);
            int contextStart = Math.Max(0, index - prefix);
            int contextLength = Math.Min(settext.Length - contextStart, searchText.Length + prefix + postfix);
            string contextText = settext.Substring(contextStart, contextLength);
            string files = Path.GetFileName(filePaths[i]);
            MessageBox.Show(contextText);
            strB.AppendFormat("<img src='" + pdficon + "' /> <a href='{0}'>{1}</a></br>{2}<br></br>", filePaths[i], files, contextText);
        }
    }
    return strB.ToString();
}

private string initDoc()
{
    StringBuilder sb = new StringBuilder();
    sb.Append(@"<!DOCTYPE HTML PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml'><head>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
    sb.Append(string.Format(@"<title>{0}</title> ", "View Document"));
    sb.Append("<body>");
    sb.Append("<div id='Content'>");
    sb.Append(GetBodyText());
    sb.Append("</div>");
    sb.Append(@"</body></html>");
    return sb.ToString();
}
PMay 1903
  • 1,103
  • 3
  • 15
  • 31

4 Answers4

0

This should work. System.Diagnostics.Process.Start(filepath);

Yytsi
  • 424
  • 1
  • 5
  • 14
0

You shouldn't think what application to start. Just specify an URI and the system will open a PDF viewer for you.

Process.Start(new ProcessStartInfo( /* AbsoluteUri */ ));

norekhov
  • 3,915
  • 25
  • 45
0

Try to change links to pdf files as follows:

<a href="file:///C:/myFolder/myfile.pdf">Click Here</a>

For details click here.

EDIT If you would like to handle click on hyperlink and run PDF as separate process:

    void Form_Load()
    {
        webBrowser1.DocumentText = "<html><body><a href=\"D:\\test.pdf\">Click Me!</a></body></html>";
        webBrowser1.Document.Click += Document_Click;
    }

    void Document_Click(object sender, HtmlElementEventArgs e)
    {
        if (webBrowser1.Document.ActiveElement.TagName == "A")
        {
            System.Diagnostics.Process.Start(webBrowser1.Document.ActiveElement.GetAttribute("HREF"));
        }
        e.ReturnValue = false;
    }
Community
  • 1
  • 1
Pavel Timoshenko
  • 721
  • 6
  • 13
  • Truly, the Toolbox WebBrowser does not get the `href` tag. Nothing happened. – PMay 1903 Nov 11 '14 at 08:13
  • Try to see questions here: http://stackoverflow.com/questions/7194851/load-local-html-file-in-a-c-sharp-webbrowser – Pavel Timoshenko Nov 11 '14 at 08:23
  • Thank you for your comment. I thought your method is to load a PDF file on the WebBrowser. However, I would like to call a new process as default reader for a PDF file as `Adobe Reader` or something like that. – PMay 1903 Nov 11 '14 at 08:30
0

I just found the answer and it worked quite well.

private void webBro_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    if (e.Url.ToString() != "about:blank")
    {
        e.Cancel = true;
        System.Diagnostics.Process.Start(e.Url.ToString());
    }
}
PMay 1903
  • 1,103
  • 3
  • 15
  • 31