System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.DocumentStream = new FileStream("C:\a.html", FileMode.Open, FileAccess.Read);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
wb.Print();
I know how to set the page orientation from a PrinterDocument object, but not from a WebBrowser object. Any way to do this? Thanks!
First, I recommend you to use async event model:
wb.DocumentCompleted += wb_DocumentCompleted;
private void wb_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
{
((WebBrowser)sender).Print();
}
To print (add the reference to Microsoft.mshtml.dll):
mshtml.IHTMLDocument2 doc = wb.Document.DomDocument as mshtml.IHTMLDocument2;
doc.execCommand("print", showUI, templatePath);
See IHTMLDocument2.execCommand, MSDN forum question and follow links.
Related
I am new to WebBrowser library.
When fetching data from https://www.binance.com/indexSpa.html website, I used the following code. But it does not return all the data. What should I do?
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += Wb_DocumentCompleted;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(#"https://www.binance.com/indexSpa.html#");
private void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var wb = (WebBrowser)sender;
Console.WriteLine(wb.DocumentText);
}
Thank you for help.
I am downloading a webpage (http://library.islamweb.net/hadith/RawyDetails.php?RawyID=1), it contains some arabic, which when viewed with the "View Source" option on a browser (chrome/IE) looks fine:
<span lang="ar-qa">رقم الراوي</span>
However when downloaded it looks like :
<span lang="ar-qa">ÑÞã ÇáÑÇæí</span>
My code is very simple:
client.DownloadFile(_webPath, savePath);
What is wrong?
Your Page's encoding char set is "windows-1256" , so you need to read it using that encoding:
private void GetRepliesStats_Load(object sender, EventArgs e)
{
WebBrowser bro = new WebBrowser();
bro.Navigate("http://library.islamweb.net/hadith/RawyDetails.php?RawyID=1");
bro.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(BrowsingCompleted);
}
private void BrowsingCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
Stream documentStream = browser.DocumentStream;
StreamReader streamReader = new StreamReader(documentStream, Encoding.GetEncoding("windows-1256"));
documentStream.Position = 0L;
String My_Result = streamReader.ReadToEnd();
}
I Hope this helps.
This foreach loop checks a webpage and sees if there are any images then downloads them. How do i stop it? When i press the button it continues the loop forever.
private void button1_Click(object sender, EventArgs e)
{
WebBrowser browser = new WebBrowser();
browser.DocumentCompleted +=browser_DocumentCompleted;
browser.Navigate(textBox1.Text);
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
WebClient webClient = new WebClient();
int count = 0; //if available
int maximumCount = imgCollection.Count;
try
{
foreach (HtmlElement img in imgCollection)
{
string url = img.GetAttribute("src");
webClient.DownloadFile(url, url.Substring(url.LastIndexOf('/')));
count++;
if(count >= maximumCount)
break;
}
}
catch { MessageBox.Show("errr"); }
}
use the break; keyword to break out of a loop
You do not have an infinite loop, you have an exception that is being thrown based on how you are writing the file to disk
private void button1_Click(object sender, EventArgs e)
{
WebBrowser browser = new WebBrowser();
browser.DocumentCompleted += browser_DocumentCompleted;
browser.Navigate("www.google.ca");
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
WebClient webClient = new WebClient();
foreach (HtmlElement img in imgCollection)
{
string url = img.GetAttribute("src");
string name = System.IO.Path.GetFileName(url);
string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
webClient.DownloadFile(url, path);
}
}
That code works fine on my environment. The issue you seemed to be having was when you were setting the DownloadFile filepath, you were setting it to a value like `\myimage.png', and the webclient could not find the path so it threw and exception.
The above code drops it into the current directory with the extension name.
Maybe the Event browser.DocumentCompleted cause the error, if the page refreshes the event gets fired again. You could try to deregister the event.
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
browser.DocumentCompleted -= browser_DocumentCompleted;
HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
WebClient webClient = new WebClient();
foreach (HtmlElement img in imgCollection)
{
string url = img.GetAttribute("src");
string name = System.IO.Path.GetFileName(url);
string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
webClient.DownloadFile(url, path);
}
}
I have making the Tab Document Interface like that Firefox. I want to make a some portion of the page refresh Time to Time. I think have you seen already Cricket Page Refresh page after some time. I have used panel for the refresh for page but panel not show. I have try but can't do this.I am working at windows form. plz any body solve this problem and thanks in advance. My coding part here:-
private void Form1_Load(object sender, EventArgs e)
{
AddNewTab();
toolStripTextBox1.Text = "Go to website";
toolStripTextBox2.Text = "Google Search text here";
toolStripComboBox1.Text = "Google.co.in";
toolStripComboBox1.Items.Add("Google.co.in");
timer1.Enabled = true;
timer1.Start();
panel1.Show();
label1.Text = DateTime.Now.ToString();
}
TabPage tp;
WebBrowser wb;
private void AddNewTab()
{
tp=new TabPage();
wb=new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Focus();
wb.Url = new Uri("http://google.co.in");
wb.Dock=DockStyle.Fill;
tp.Controls.Add(wb);
tabControl1.TabPages.Insert(tabControl1.TabPages.Count - 1, tp);
tabControl1.SelectedTab = tp;
}
So I have a custom windows form with a tabbed browser. I want to be able to see the hoovered hyperlink in the status bar. I have searched everywhere but nothing yet.
I checked the MSDN under the web browser class which led me to the StatusTextChanged.
So in my code I have
wb.StatusTextChanged += MainWindow_StatusChange;
then
private void MainWindow_StatusChange(Object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
this.toolStripStatusLabel2.Text = wb.StatusText;
}
Well, the tool.StripStatusLabel2.Text is empty.
Any help?
You have subscribed the wrong event.
Do this...
private void Form1_Load(object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
wb.StatusTextChanged += new EventHandler(wb_StatusTextChanged);
wb.Navigate("http://www.google.de");
}
void wb_StatusTextChanged(object sender, EventArgs e)
{
this.toolStripStatusLabel2.Text = ((WebBrowser) sender).StatusText;
}