Click here to Skip to main content
14,978,528 members
Articles / Web Development / HTML
Posted 23 Jan 2006


95 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.88/5 (38 votes)
23 Jan 2006CPOL2 min read
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


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.


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;
           ref em, ref em);

For printing without print dialog:

           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:


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();

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.


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


About the Author

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

GeneralMy vote of 1 Pin
Member 148847398-Jul-20 20:13
MemberMember 148847398-Jul-20 20:13 
Questionno formatting Pin
charlywrx3-Apr-16 16:49
Membercharlywrx3-Apr-16 16:49 
QuestionPrint HTML with settings Pin
Sheriffff12-Oct-15 7:51
MemberSheriffff12-Oct-15 7:51 
QuestionDLL SIte link: Pin
hrojasara26-Apr-13 18:07
Memberhrojasara26-Apr-13 18:07 
QuestionReferences Pin
mrcouthy15-Apr-13 4:05
Membermrcouthy15-Apr-13 4:05 
AnswerRe: References Pin
zaid farhan3-Jul-20 14:35
Memberzaid farhan3-Jul-20 14:35 
Questionhow to print to bullzip pdf printer? Pin
Nguyen Luong Son6-Sep-12 21:43
MemberNguyen Luong Son6-Sep-12 21:43 
Questionhow about pdf & doc files? Pin
Pavan_N4-Sep-12 21:30
MemberPavan_N4-Sep-12 21:30 
QuestionOrientation Pin
LWFDN1-Jul-12 7:48
MemberLWFDN1-Jul-12 7:48 
AnswerRe: Orientation Pin
Borun1-Jul-12 8:14
MemberBorun1-Jul-12 8:14 
AnswerRe: Orientation Pin
Alejandro Miralles7-Aug-12 13:02
MemberAlejandro Miralles7-Aug-12 13:02 
QuestionWorks but can't print two pages? Pin
analog99912-Jun-12 11:10
Memberanalog99912-Jun-12 11:10 
AnswerRe: Works but can't print two pages? Pin
Alejandro Miralles7-Aug-12 13:00
MemberAlejandro Miralles7-Aug-12 13:00 
Questionprinting doesn't work properly Pin
TLOH4529-Sep-11 23:13
MemberTLOH4529-Sep-11 23:13 
AnswerRe: printing doesn't work properly Pin
Alejandro Miralles7-Aug-12 13:01
MemberAlejandro Miralles7-Aug-12 13:01 
QuestionC++? Pin
rvangaal1-Aug-11 3:07
Memberrvangaal1-Aug-11 3:07 
GeneralMy vote of 5 Pin
Md. Rashidul Hasan Masum4-Nov-10 0:16
professionalMd. Rashidul Hasan Masum4-Nov-10 0:16 
QuestionGot an error - some included library is not in project Pin
ShaharM25-Sep-10 6:31
MemberShaharM25-Sep-10 6:31 
AnswerRe: Got an error - some included library is not in project Pin
Pavan_N12-Apr-12 23:12
MemberPavan_N12-Apr-12 23:12 
GeneralRe: Got an error - some included library is not in project Pin
zaid farhan3-Jul-20 14:16
Memberzaid farhan3-Jul-20 14:16 
QuestionIs there a way to print without opening the form? Pin
shiravidas25-Aug-10 21:24
Membershiravidas25-Aug-10 21:24 
GeneralNice little tool. Thanks Pin
Joseph Growney16-Mar-10 6:55
MemberJoseph Growney16-Mar-10 6:55 
GeneralRe: Nice little tool. Thanks Pin
Borun16-Mar-10 19:28
MemberBorun16-Mar-10 19:28 
GeneralMy vote of 1 Pin
Trenton Adams3-Dec-09 8:41
MemberTrenton Adams3-Dec-09 8:41 
GeneralWPF (.NET 3.5) Pin
yvisek19-Nov-09 7:55
Memberyvisek19-Nov-09 7:55 

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

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