Print HTML In C# With Or Without The Web Browser Control And The Print Dialog | |||
| |||
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. <PRE lang=cs nd="7"> object em =null; axW.ExecWB( SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER, ref em,ref em);</PRE> <P nd="11">For printing without print dialog:</P><PRE lang=cs nd="12"> axW.ExecWB( SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref em,ref em);</PRE> <P nd="15">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 <CODE nd="16">Navigate</CODE> method and wait until it loads the whole HTML page. That is:</P><PRE lang=cs nd="18"> for(;axW.ReadyState!=SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE;) { System.Windows.Forms.Application.DoEvents(); }</PRE> <P nd="19">Then print the page with any of the previous options you want.</P> <P nd="20">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.</P> <P nd="21">Now,to print directly,you need to create a <CODE nd="22">HtmlPrinter</CODE> object and then call its <CODE nd="23">PrintUrlFromMemory</CODE> method with the URL as its parameter.</P><PRE lang=cs nd="25"> using HtmlPrinter; hpObj=new HtmlPrinter.HtmlPrinter(); hpObj.PrintUrlFromMemory(txtUrl.Text);</PRE> <P nd="27">Now,you add the code in your project to print the HTML page from its source text:</P><PRE lang=cs nd="28"> HtmlPrinter.HtmlPrinter hpObj=new HtmlPrinter.HtmlPrinter(); hpObj.PrintHtml(txtString.Text,true);</PRE> <P nd="31">If you want to print without the print dialog,then use the following line:</P><PRE lang=cs nd="32">hpObj.PrintHtml(txtString.Text,false);</PRE> Tags: C# C# Print HtmlPrinter Print Print HTML PrintDocument PrintHtml PrintUrlFromMemory | |||
| |||
| |||
Login for comment |