Click here to Skip to main content
11,490,007 members (57,652 online)
Click here to Skip to main content

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

, 24 Jan 2006 CPOL 311.4K 7.9K 90
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Borun
Architect Unicorn Software & Solutions
Bangladesh Bangladesh
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.

Comments and Discussions

 
QuestionDLL SIte link: Pin
hrojasara26-Apr-13 19:07
memberhrojasara26-Apr-13 19:07 
QuestionReferences Pin
mrcouthy15-Apr-13 5:05
membermrcouthy15-Apr-13 5:05 
Questionhow to print to bullzip pdf printer? Pin
Nguyen Luong Son6-Sep-12 22:43
memberNguyen Luong Son6-Sep-12 22:43 
Questionhow about pdf & doc files? Pin
Pavan Nagumalli4-Sep-12 22:30
memberPavan Nagumalli4-Sep-12 22:30 
QuestionOrientation Pin
LWFDN1-Jul-12 8:48
memberLWFDN1-Jul-12 8:48 
AnswerRe: Orientation Pin
Borun1-Jul-12 9:14
memberBorun1-Jul-12 9:14 
AnswerRe: Orientation Pin
Alejandro Miralles7-Aug-12 14:02
memberAlejandro Miralles7-Aug-12 14:02 
QuestionWorks but can't print two pages? [modified] Pin
analog99912-Jun-12 12:10
memberanalog99912-Jun-12 12:10 
AnswerRe: Works but can't print two pages? Pin
Alejandro Miralles7-Aug-12 14:00
memberAlejandro Miralles7-Aug-12 14:00 
Questionprinting doesn't work properly Pin
TLOH4530-Sep-11 0:13
memberTLOH4530-Sep-11 0:13 
AnswerRe: printing doesn't work properly Pin
Alejandro Miralles7-Aug-12 14:01
memberAlejandro Miralles7-Aug-12 14:01 
QuestionC++? Pin
rvangaal1-Aug-11 4:07
memberrvangaal1-Aug-11 4:07 
GeneralMy vote of 5 Pin
Member 16043004-Nov-10 1:16
memberMember 16043004-Nov-10 1:16 
QuestionGot an error - some included library is not in project Pin
ShaharM25-Sep-10 7:31
memberShaharM25-Sep-10 7:31 
AnswerRe: Got an error - some included library is not in project Pin
Pavan Nagumalli13-Apr-12 0:12
memberPavan Nagumalli13-Apr-12 0:12 
QuestionIs there a way to print without opening the form? Pin
shiravidas25-Aug-10 22:24
membershiravidas25-Aug-10 22:24 
GeneralNice little tool. Thanks Pin
Joseph Growney16-Mar-10 7:55
memberJoseph Growney16-Mar-10 7:55 
GeneralRe: Nice little tool. Thanks Pin
Borun16-Mar-10 20:28
memberBorun16-Mar-10 20:28 
GeneralMy vote of 1 Pin
Trenton Adams3-Dec-09 9:41
memberTrenton Adams3-Dec-09 9:41 
GeneralWPF (.NET 3.5) Pin
yvisek19-Nov-09 8:55
memberyvisek19-Nov-09 8:55 
QuestionC# ExecCommand Pin
BMW74015-Nov-09 22:44
memberBMW74015-Nov-09 22:44 
GeneralVB.net Version Pin
Member 43764219-Oct-09 12:17
memberMember 43764219-Oct-09 12:17 
QuestionCan generate a thumb of webpage from html? Pin
Member 45548952-Sep-09 14:17
memberMember 45548952-Sep-09 14:17 
Generalpage settings would be great!! Pin
nin824-Mar-09 11:56
membernin824-Mar-09 11:56 
GeneralPrint Html page in C# Pin
Babita Shivade4-Mar-09 4:47
memberBabita Shivade4-Mar-09 4:47 
Questionno htmlprint namespace Pin
joelha2000125-Feb-09 9:52
memberjoelha2000125-Feb-09 9:52 
Generalhi Pin
Babita Shivade19-Feb-09 2:12
memberBabita Shivade19-Feb-09 2:12 
GeneralInvalidActiveXStateException Pin
AndrewLe7-Oct-08 18:42
memberAndrewLe7-Oct-08 18:42 
GeneralNeed to run in x64 bit machine Pin
v.jayakrishnan9-Jul-08 22:26
memberv.jayakrishnan9-Jul-08 22:26 
GeneralWait for print dialog ok before continuing [modified] Pin
petebob7961-Apr-08 23:57
memberpetebob7961-Apr-08 23:57 
QuestionCan it export ot dll and run on IE Pin
Nguoi Lap Trinh15-Nov-07 17:43
memberNguoi Lap Trinh15-Nov-07 17:43 
QuestionIs there a vb.net version of this code? Pin
timescale7-Nov-07 8:29
membertimescale7-Nov-07 8:29 
GeneralIt is the little things that count Pin
MichaelJRD12-Oct-07 6:24
memberMichaelJRD12-Oct-07 6:24 
QuestionSame solution for ASP.NET?? Pin
Yendi2-Jul-07 12:12
memberYendi2-Jul-07 12:12 
GeneralThanks Pin
Jeweel17-May-07 2:42
memberJeweel17-May-07 2:42 
GeneralIE Script Error when run in service Pin
blahblahblaahlaksjdf20-Apr-07 5:54
memberblahblahblaahlaksjdf20-Apr-07 5:54 
GeneralRe: IE Script Error when run in service Pin
RaghuSetty14-Oct-08 1:49
memberRaghuSetty14-Oct-08 1:49 
GeneralPrinting format options Pin
netJP12L11-Dec-06 9:30
membernetJP12L11-Dec-06 9:30 
GeneralCustom header and footers Pin
Mukund Pujari29-Jun-06 0:45
memberMukund Pujari29-Jun-06 0:45 
AnswerRe: Custom header and footers Pin
Borun13-Aug-06 7:12
memberBorun13-Aug-06 7:12 
GeneralRe: Custom header and footers Pin
yvisek29-Nov-09 10:26
memberyvisek29-Nov-09 10:26 
GeneralRe: Custom header and footers Pin
Pavan Nagumalli4-Sep-12 22:27
memberPavan Nagumalli4-Sep-12 22:27 
GeneralPrinter Name Pin
rev12313-Jun-06 11:27
memberrev12313-Jun-06 11:27 
GeneralRe: Printer Name Pin
drekon7518-Jul-06 7:02
memberdrekon7518-Jul-06 7:02 
GeneralRe: Printer Name Pin
Pete O'Hanlon1-Dec-06 1:51
memberPete O'Hanlon1-Dec-06 1:51 
GeneralRe: Printer Name Pin
dsandhya14-Mar-07 13:58
memberdsandhya14-Mar-07 13:58 
GeneralPrint to System.Drawing.Printing.PrintDocument Pin
Welliton Alves Toledo19-May-06 5:52
memberWelliton Alves Toledo19-May-06 5:52 
GeneralThanks Pin
Mukund Pujari11-May-06 18:32
memberMukund Pujari11-May-06 18:32 
GeneralRe: Thanks Pin
Borun11-May-06 22:34
memberBorun11-May-06 22:34 
Generalcustom libraries in the reference Pin
tantc23-Feb-06 18:22
membertantc23-Feb-06 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 24 Jan 2006
Article Copyright 2006 by Borun
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid