Click here to Skip to main content
Click here to Skip to main content

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

By , 23 Jan 2006
 

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWorks but can't print two pages? [modified]memberanalog99912 Jun '12 - 11:10 
Great code. I'm using the PrintHtml method to print packing slips (one page). I need two copies of the document, one for us and one for our customer.
 
But, any time I try to print two copies in any fashion programmatically, my form/application gets killed, and the printer thinks there is additional data coming. Eventually the printer will time out and print an error code.
 
Here's what I've tried:
 
1) Calling PrintHtml(mystring) twice, sequentially & immediately. One page gets printed, and printer goes into crap mode waiting for more data. Kills app.
 
2) Calling PrintHtml(mystring) twice, with a thread wait of a few seconds between calls. One page gets printed, and printer goes into crap mode waiting for more data. Kills app.
 
3) Creating two separate instances of HtmlPrinter.HtmlPrinter and calling each of them the same way as in #1 and #2. One page gets printed, and printer goes into crap mode waiting for more data. Kills app.
 
4) Adding a HTML page break to the HTML source I'm printing and calling PrintHtml once. Both pages do get printed, but the printer STILL goes into crap mode waiting for more data. Kills app.
 
5) Changing the PrintHtml function to pass opt "2" instead of opt "1". Prints one page, leaves printer in crap mode and kills app.
 
6) Added this line of code to the end of my function that is doing the printing: this.BeginInvoke(new MethodInvoker(() => PrintPackingSlip(null, null))); One page is printed, app is killed, printer into crap mode.

7) Print page by clicking on menu item, and then manually click the menu item again and print again. This is the only thing that *sometimes* works and doesn't send the printer into crap mode. Kills app when it doesn't work.
 
Maybe there is a better way. I can't find documentation on the control to see how I might be able to set the control to print two pages.
 
Any other suggestions?

modified 13 Jun '12 - 3:46.

AnswerRe: Works but can't print two pages? PinmemberAlejandro Miralles7 Aug '12 - 13:00 
Try this
http://alemiralles.blogspot.com.ar/2012/08/printing-html-using-embedded-web.html[^]

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 24 Jan 2006
Article Copyright 2006 by Borun
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid