Click here to Skip to main content
15,885,980 members
Articles / Web Development / HTML
Article

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 502.2K   14.1K   95   73
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:

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

For printing without print dialog:

C#
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:

C#
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.

C#
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:

C#
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:

C#
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)


Written By
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

 
GeneralPrinter Name Pin
rev12313-Jun-06 10:27
rev12313-Jun-06 10:27 
GeneralRe: Printer Name Pin
drekon7518-Jul-06 6:02
drekon7518-Jul-06 6:02 
GeneralRe: Printer Name Pin
Pete O'Hanlon1-Dec-06 0:51
mvePete O'Hanlon1-Dec-06 0:51 
GeneralRe: Printer Name Pin
sandy202014-Mar-07 12:58
sandy202014-Mar-07 12:58 
GeneralPrint to System.Drawing.Printing.PrintDocument Pin
Welliton Alves Toledo19-May-06 4:52
Welliton Alves Toledo19-May-06 4:52 
GeneralThanks Pin
Mukund Pujari11-May-06 17:32
Mukund Pujari11-May-06 17:32 
GeneralRe: Thanks Pin
Borun11-May-06 21:34
Borun11-May-06 21:34 
Generalcustom libraries in the reference Pin
tantc23-Feb-06 17:22
tantc23-Feb-06 17:22 
Hiya,

nice routine. Just what I'm looking for.

those libs like AxInterop.DHTMLEDLib.dll

are these copyrights ? not familiar with them.... cheers..

Teck Chye
GeneralRe: custom libraries in the reference Pin
sprash257-Jun-06 20:00
sprash257-Jun-06 20:00 
GeneralRe: custom libraries in the reference Pin
Borun8-Jun-06 6:04
Borun8-Jun-06 6:04 
GeneralRe: custom libraries in the reference Pin
InnoDepot11-Sep-06 8:30
InnoDepot11-Sep-06 8:30 
GeneralLandscape printing Pin
Jozef Sevcik19-Feb-06 23:18
Jozef Sevcik19-Feb-06 23:18 
GeneralThanks Pin
coolsap19-Feb-06 21:55
coolsap19-Feb-06 21:55 
GeneralCustom Printer settings Pin
rammreddy14-Feb-06 6:59
rammreddy14-Feb-06 6:59 
GeneralRe: Custom Printer settings Pin
KirKone21-Feb-06 21:57
KirKone21-Feb-06 21:57 
GeneralRe: Custom Printer settings Pin
SokTi21-Jun-06 2:50
SokTi21-Jun-06 2:50 
GeneralRe: Custom Printer settings Pin
shiweiwei9721-Jul-08 20:40
shiweiwei9721-Jul-08 20:40 
GeneralRe: Custom Printer settings Pin
smitcher15-Oct-08 5:39
smitcher15-Oct-08 5:39 
AnswerRe: Custom Printer settings Pin
shiweiwei9717-Oct-08 16:19
shiweiwei9717-Oct-08 16:19 
GeneralRe: Custom Printer settings Pin
Ozgur KOC17-Oct-08 10:23
Ozgur KOC17-Oct-08 10:23 
Generalprint other files Pin
annnne9-Feb-06 22:54
annnne9-Feb-06 22:54 
QuestionHow to wait for print completed ? Pin
Warnat31-Jan-06 21:23
Warnat31-Jan-06 21:23 
AnswerRe: How to wait for print completed ? Pin
Borun1-Feb-06 8:52
Borun1-Feb-06 8:52 
AnswerRe: How to wait for print completed ? Pin
Warnat18-Jul-06 0:28
Warnat18-Jul-06 0:28 
GeneralRe: How to wait for print completed ? Pin
Fei184087718-Jan-07 12:09
Fei184087718-Jan-07 12:09 

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.