Search mode: AND OR
 
Print Html in C# with or without print dialog


Sample Image - maximum width is 600 pixels

 

Download demo project - 23 Kb

Introduction

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

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

 

Background

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

Using the code

If you wan to show a web page in your own form, you have to add a webbrowser control(I will name it axW in this article). Now if you want to print a page which is already loaded in your webbrowser control, just add two lines 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 just with a single click. You have to navigate the webbrowser 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 a html page from its source, what you should to do, just add the HtmlPrinter project to your solution. Then go to Add Reference option by right clicking on your own project. From project tab select 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 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 off course about the COM.

About Abdullah Al- Farooq (Borun)

Borun is working in  Unicorn Software & Solution,Bangladesh as Chief Technical Officer. He has experience in Software development, Software Components, Communications. His favorite domains are  C++, MFC, COM, Win32, Managed C++, C#, ASP.NET, Linux based developments.

 

 
 


 

   

 

   

© 2008. Unicorn Software and Solutions.  All rights reserved