5,427,813 members and growing! (16,158 online)
Email Password   helpLost your password?
Desktop Development » Printing » General     Intermediate License: The Code Project Open License (CPOL)

Print HTML in C# with or without the web browser control and the print dialog

By Borun

Shows how to print HTML in C# with or without the web browser control and the Windows default print dialog. It also implements the HTMLDocumentClass, IHTMLDocument2, and IHTMLDocument4 interfaces of MSHTML.
C#, HTML, .NET, Windows, Win2K, WinXP, Visual Studio, Dev

Posted: 24 Jan 2006
Updated: 24 Jan 2006
Views: 91,648
Bookmarked: 37 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
20 votes for this Article.
Popularity: 6.01 Rating: 4.62 out of 5
1 vote, 5.0%
1
1 vote, 5.0%
2
0 votes, 0.0%
3
3 votes, 15.0%
4
15 votes, 75.0%
5

Sample Image - printhml.jpg

Introduction

Printing with the web browser control in .NET platform is not hard work. But the challenge for me was to navigate to a specific URL and then print the document with a single click event. There was another challenge I had to face, when I tried to print an HTML document from its source in the same manner.

Well, this article describes some techniques to print an HTML document with that challenge (if anyone thinks so). This code is also able to print an HTML page without the print dialog. So, if any one needs printing without any user interaction, this code may be helpful for you. In addition, this code is also able to print an HTML document without any web browser control in the form.

Background

This project came out of a work requirement for some kind of a content management system. I needed to be able to print HTML without any user interaction and also print the HTML source directly.

Using the code

If you want to show a web page in your own form, you have to add a web browser control (I will name it axW in this article). Now, if you want to print a page which is already loaded in your web browser control, just add two lines of code to print the page:

For printing with print dialog:

object em =null;
axW.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, 
           SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER, 
           ref em, ref em);

For printing without print dialog:

axW.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, 
           SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, 
           ref em, ref em);

Now, what if you want to navigate and print with a single click? You have to navigate the web browser to a specific URL with its Navigate method and wait until it loads the whole HTML page. That is:

for(;axW.ReadyState!=SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE;)
{
    System.Windows.Forms.Application.DoEvents();
}

Then print the page with any of the previous options you want.

Now, if you want to print an HTML page from its source, what should do is, just add the HtmlPrinter project to your solution. Then go to Add References option, by right clicking on your own project. From the Project tab, select the HtmlPrinter project.

Now, to print directly, you need to create a HtmlPrinter object and then call its PrintUrlFromMemory method with the URL as its parameter.

using HtmlPrinter;

hpObj=new HtmlPrinter.HtmlPrinter();
hpObj.PrintUrlFromMemory(txtUrl.Text);

Now, you add the code in your project to print the HTML page from its source text:

HtmlPrinter.HtmlPrinter hpObj=new HtmlPrinter.HtmlPrinter();
hpObj.PrintHtml(txtString.Text, true);

If you want to print without the print dialog, then use the following line:

hpObj.PrintHtml(txtString.Text, false);

Points of Interest

Working with MSHTML's interfaces and classes is pretty complex. So I suggest, before you work on it, try to know about the basic structure of MSHTML and of course about COM.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Borun


I am a cooker in Unicorn Software & Solution,Bangladesh and I fairly cook my own brain with my own taste, patience and intelligence to serve our clients. I have to try to take care as much items as in our dish and thus I burn myself in our kitchen and think to leave those there....but i never can..Perhaps I am cooker by born.
Occupation: Architect
Company: Unicorn Software & Solutions
Location: Bangladesh Bangladesh

Other popular Printing articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 33 (Total in Forum: 33) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralNeed to run in x64 bit machinememberv.jayakrishnan22:26 9 Jul '08  
GeneralWait for print dialog ok before continuing [modified]memberpetebob79623:57 1 Apr '08  
QuestionCan it export ot dll and run on IEmemberNguoi Lap Trinh17:43 15 Nov '07  
GeneralIs there a vb.net version of this code?membertimescale8:29 7 Nov '07  
GeneralIt is the little things that countmemberMichaelJRD6:24 12 Oct '07  
QuestionSame solution for ASP.NET??memberYendi12:12 2 Jul '07  
GeneralThanksmemberJeweel2:42 17 May '07  
GeneralIE Script Error when run in servicememberblahblahblaahlaksjdf5:54 20 Apr '07  
GeneralPrinting format optionsmembernetJP12L9:30 11 Dec '06  
GeneralCustom header and footersmemberMukund Pujari0:45 29 Jun '06  
AnswerRe: Custom header and footersmemberBorun7:12 13 Aug '06  
GeneralPrinter Namememberrev12311:27 13 Jun '06  
GeneralRe: Printer Namememberdrekon757:02 18 Jul '06  
GeneralRe: Printer NamememberPete O'Hanlon1:51 1 Dec '06  
GeneralRe: Printer Namememberdsandhya13:58 14 Mar '07  
GeneralPrint to System.Drawing.Printing.PrintDocumentmemberWelliton Alves Toledo5:52 19 May '06  
GeneralThanksmemberMukund Pujari18:32 11 May '06  
GeneralRe: ThanksmemberBorun22:34 11 May '06  
Generalcustom libraries in the referencemembertantc18:22 23 Feb '06  
GeneralRe: custom libraries in the referencemembersprash2521:00 7 Jun '06  
GeneralRe: custom libraries in the referencememberBorun7:04 8 Jun '06  
GeneralRe: custom libraries in the referencememberInnoDepot9:30 11 Sep '06  
GeneralLandscape printingmemberJozef Sevcik0:18 20 Feb '06  
GeneralThanksmembercoolsap22:55 19 Feb '06  
GeneralCustom Printer settingsmemberrammreddy7:59 14 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jan 2006
Editor: Smitha Vijayan
Copyright 2006 by Borun
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project