C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


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


Posted Date: 07 Jun 2008    Resource Type: Articles    Category: .NET Framework
Author: shakti singh tanwarMember Level: Diamond    
Rating: Points: 18



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.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Printing HTML pages using SHDocVw.dll  .  Print HTML pages  .  Downloading web pages to hard disk  .  Downloading web pages  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: how to create shared assembly
Previous Resource: Multiple Active Result Set ( MARS ADO.Net 2.0 )
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use