Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Printing HTML Pages in Windows Applications using SHDocVw.dll
|
This article is an effort towards understanding the powers of a forgotten hero shdocvw.dll. It's forgotten since this has been replaced by Microsoft's WebBrowser control but still it is very useful in scenarios where you want to print a page without opening in web browser. In this article we will print a page without opening it in Browser. The page will be downloaded to user’s disk by using WebRequest class.
Microsoft Internet Explorer comes up with a fairly comprehensive, although sparsely documented, Object Model. If you've used the Web Browser control in Access, you are already familiar with the capabilities of IE's Object Model. All of the functionality in IE's object model (not counting external support, like scripting support etc.) is provided by the following two dlls:
• shdocvw.dll (Microsoft Internet Controls) • mshtml.tlb (Microsoft HTML Object Librar
Both the above files won’t be available in .Net or COM tab while adding reference. Please add the reference by browsing to %windir%\system32. Include the following Namespaces:-
using mshtml; using System.Net; using System.IO;
Create an instance of InternetExplorer. If you are familiar with COM working there is a interface for every class that you create in COM. COM is heavily based on interfaces.
SHDocVw.InternetExplorer internetExplorer = new SHDocVw.InternetExplorerClass();
Cast the Internet Explorer just created to an object of type IWebBrowser2. We will be using this object for ptinting and navigation purposes.
SHDocVw.IWebBrowser2 webBrowser = (SHDocVw.IWebBrowser2)internetExplorer;
Navigate to blank page.This is just to get a blank document that we will be filling later on with html of page that we want to print.
webBrowser.Navigate("about:blank", ref noValue, ref noValue, ref noValue, ref noValue); mshtml.IHTMLDocument2 htmlDoc = internetExplorer.Document as mshtml.IHTMLDocument2;
Download the page using WebRequest Class.
WebRequest objRequest = System.Net.HttpWebRequest.Create("http://www.dotnetspider.com/resources/17962-Sending-WEB-Page-mail-body-via-C-gmail.aspx"); StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream()); string result = sr.ReadToEnd(); sr.Close();
Now result contains the html of page that you want to print.Write this html in HtmlDocument object.
htmlDoc.writeln(result);
Print the document.
htmlDoc.execCommand("Print", true, o);
and That’s it. Don’t forget to close the document. Here goes the complete code.
SHDocVw.InternetExplorer internetExplorer = new SHDocVw.InternetExplorerClass();
SHDocVw.IWebBrowser2 webBrowser = (SHDocVw.IWebBrowser2)internetExplorer;
// Make the web browser visible.
// webBrowser.Visible = true;
// Display empty page so we have something to manipulate.
object noValue = System.Reflection.Missing.Value;
webBrowser.Navigate("about:blank", ref noValue, ref noValue, ref noValue, ref noValue);
// Get access to the webbrowser's document.
mshtml.IHTMLDocument2 htmlDoc = internetExplorer.Document as mshtml.IHTMLDocument2;
WebRequest objRequest = System.Net.HttpWebRequest.Create("http://www.dotnetspider.com/resources/17962-Sending-WEB-Page-mail-body-via-C-gmail.aspx"); StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream()); string result = sr.ReadToEnd(); sr.Close(); // Update the document.
htmlDoc.writeln(result); object o = ""; htmlDoc.execCommand("Print", true, o); htmlDoc.close();
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|