JavaScript打印 WebBrowser 控件打印

1.JavaScript打印 


<object id="WebBrowser" width=0 height=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"> 
</object> 
<input type="button" name="Button" value="关闭窗口" onClick="document.all.WebBrowser.ExecWB(45,1)"> 
试着改变参数会得到其他一些功能: 

WebBrowser.ExecWB(1,1) 打开 
WebBrowser.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口 
WebBrowser.ExecWB(4,1) 保存网页 
WebBrowser.ExecWB(6,1) 打印 
WebBrowser.ExecWB(7,1) 打印预览 
WebBrowser.ExecWB(8,1) 打印页面设置 
WebBrowser.ExecWB(10,1) 查看页面属性 
WebBrowser.ExecWB(15,1) 好像是撤销,有待确认 
WebBrowser.ExecWB(17,1) 全选 
WebBrowser.ExecWB(22,1) 刷新 
WebBrowser.ExecWB(45,1) 关闭窗体无提示 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ly198478/archive/2008/06/03/2506606.aspx 

<input id="btnPrint" type="button" value="打印" οnclick="javascript:window.print();" /> 

可以用样式控制,你想让那块打印就打印啊,样式如下: 

<style type="text/css" media=print> 
.noprint{display : none } 
</style> 

然后使用样式就可以: 

<p class="noprint">不需要打印的地方</p> 

============================================================= 

2.IE打印 

<script language="javascript"> 
       function printsetup(){ 
       // by http://www.cidu.net/ 打印页面设置 
       wb.execwb(8,1); 
       } 
       function printpreview(){ 
       //by http://www.cidu.net/ 打印页面预览           
       wb.execwb(7,1); 
       } 
     
       function printit() 
       { 
       if (confirm('确定打印吗?')) { 
       wb.execwb(6,6) 
       } 
       } 
</script> 
     <OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT> 
     <input type=button name=button_print value="打印本单据" οnclick="javascript:printit()"> 
     <input type=button name=button_setup value="打印页面设置" οnclick="javascript:printsetup();"> 
     <input type=button name=button_show value="打印预览" οnclick="javascript:printpreview();"> 
     <input type=button name=button_fh value="关闭" οnclick="javascript:window.close();"> 
    
      ------------------------------------------------ 
      关于这个组件还有其他的用法,列举如下: 
      WebBrowser.ExecWB(1,1) 打开 
      Web.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口 
      Web.ExecWB(4,1) 保存网页 
      Web.ExecWB(6,1) 打印 
      Web.ExecWB(7,1) 打印预览 
      Web.ExecWB(8,1) 打印页面设置 
      Web.ExecWB(10,1) 查看页面属性 
      Web.ExecWB(15,1) 好像是撤销,有待确认 
      Web.ExecWB(17,1) 全选 
      Web.ExecWB(22,1) 刷新 
      Web.ExecWB(45,1) 关闭窗体无提示  

不过不巧的是IE阻止了activeX运行 

点击“打印”、“直接打印”、“页面设置”、“打印预览”出现类似“Internet Explorer 已经阻止此站点用不安全方式使用ActiveX控件”警告请直接使用浏览器中“文件”菜单项中的相应打印选项或设置浏览器如下: 

可以不点击网页中的打印按钮,而是选择“文件”“打印”即可。 

主菜单“工具”——Internet选项——安全——自定义级别, 将“安全设置”中“对没有标记为安全的ActiveX”控件进行初始化和脚本运行由“禁用”改为“启用” 
另一种方法,“工具” “Internet选项”“受信任的站点”“站点”,然后填入网址即可,如果这个网站不是以https:连接的把下面“对该区域中的所有站点要求服务器验证”前面的勾去掉即可。 
当然了,以"文件""打印"的方法最为简单,实用而又不影响安全性,是最好的办法。 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【利用WebBrowser彻底解决Web打印问题(包括后台打印) 】利用WebBrowser彻底解决Web打印问题(包括后台打印)BS架构下的打印大家是怎么解决的呢,我最近作了一个项目正好负责这一块,不仅要求打印页面的特定部分,还要求有后台的批量打印,在网上查了一些资料,最后终于解决了。抱着“取之于众 服务于众”的思想,我总结了一下,把它拿到网上来与大家分享,希望能帮助遇到类似问题的朋友。我主要使用了IE内置的WebBrowser控件,无需用户下载和安装。WebBrowser有很多功能,除打印外的其他功能就不再赘述了,你所能用到的打印功能也几乎全部可以靠它完成,下面的问题就是如何使用它了。先说显示后打印,后面说后台打印。1.首先引入一个WebBrowser在需要打印的页面,可以直接添加:到页面,或者使用JavaScript在需要的时候临时添加也可以:document.body.insertAdjacentHTML("beforeEnd","");2 .页面设置和打印预览如下所示,直接调用即可document.all.WebBrowser.ExecWB(6,6) 直接打印document.all.WebBrowser.ExecWB(8,1) 页面设置document.all.WebBrowser.ExecWB(7,1) 打印预览或者:execScript("document.all.WebBrowser.ExecWB 7, 1","VBScript");3 隐藏不打印的页面元素和分页CSS 有个Media 属性,可以分开设置打印和显示的格式。如 … 中间的格式将只在打印时起作用,不会影响显示界面。所以可以设定.Noprint{display:none;}.PageNext{page-break-after: always;}然后给不想打印的页面元素添加: class="Noprint" ,那就不会出现在打印打印预览中了。想分页的地方添加: 就可以了。4.打印页面的特定部分我是通过将需要打印的特定部分另建一个页面,然后装入主页面的一个IFrame中,再调用IFrame的打印方法,只打印IFrame中的内容实现的。如:下面的pringFrame js函数将只打印Iframe中的内容,可以直接引用使用,如printFrame(FrameId);window.print = printFrame;// main stufffunction printFrame(frame, onfinish) {if ( !frame ) frame = window;function execOnFinish() {switch ( typeof(onfinish) ) {case "string": execScript(onfinish); break;case "function": onfinish();}if ( focused && !focused.disabled ) focused.focus();}if (( frame.document.readyState !== "complete") &&( !frame.document.confirm("The document to print is not downloaded yet! Continue with printing?") )){execOnFinish();return;}var eventScope = printGetEventScope(frame);var focused = document.activeElement;window.printHelper = function() {execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");printFireEvent(frame, eventScope, "onafterprint");printWB.outerHTML = "";execOnFinish();window.printHelper = null;}document.body.insertAdjacentHTML("beforeEnd","");printFireEvent(frame, eventScope, "onbeforeprint");frame.focus();window.printHelper = printHelper;setTimeout("window.printHelper()", 0);}// helpersfunction printIsNativeSupport() {var agent = window.navigator.userAgent;var i = agent.indexOf("MSIE ")+5;return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0;}function printFireEvent(frame, obj, name) {var handler = obj[name];switch ( typeof(handler) ) {case "string": frame.execScript(handler); break;case "function": handler();}}function printGetEventScope(frame) {var frameset = frame.document.all.tags("FRAMESET");if ( frameset.length ) return frameset[0];return frame.document.body;}Iframe中所装载页面的打印效果在所装载页面设置就可以了,如分页等。5.后台打印我是通过建一个隐藏Iframe实现的,当然仍然会有页面装载的过程。下面的函数创建Iframe装载页面并打印。如 printHidden(url) //url为页面地址function printHidden(url) {document.body.insertAdjacentHTML("beforeEnd","");var doc = printHiddenFrame.document;doc.open();doc.write("");doc.write("");doc.write("");doc.close();}function onprintHiddenFrame() {function onfinish() {printHiddenFrame.outerHTML = "";if ( window.onprintcomplete ) window.onprintcomplete();}printFrame(printHiddenFrame.printMe, onfinish);}它用到了printFrame,所以别忘了引用前面的函数。总之,WebBroswer已经为我们提供了解决方案,我们只要结合需求把它应用好就行了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值