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

566b019c081e05d770ba379fb7a03a29.png

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#

Copy Code

object em =null;

axW.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,

SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER,

ref em, ref em);

For printing without print dialog:

C#

Copy Code

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#

Copy Code

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#

Copy Code

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#

Copy Code

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#

Copy Code

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值