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