码迷,mamicode.com
首页 > 编程语言 > 详细

还是WebBrowser,打印设置及JavaScript代码

时间:2015-06-09 16:58:15      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

  1 使用了IE内置的WebBrowser控件,无需用户下载和安装。WebBrowser有很多功能,除打印外的其他功能就不再赘述了,你所能用到的打印功能也几乎全部可以靠它完成,下面的问题就是如何使用它了。先说显示后打印,后面说后台打印。
  2 
  3 1.首先引入一个WebBrowser在需要打印的页面,可以直接添加: 
  4  
  5 <object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0"> 
  6 </object> 
  7 
  8 到页面,或者使用JavaScript在需要的时候临时添加也可以:
  9 
 10 document.body.insertAdjacentHTML("beforeEnd",
 11     "<object id=\"WebBrowser\" width=0 height=0  12 classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\">");
 13 
 14 2 .页面设置和打印预览
 15 
 16 如下所示,直接调用即可
 17 document.all.WebBrowser.ExecWB(6,6)   直接打印
 18 document.all.WebBrowser.ExecWB(8,1)   页面设置
 19 document.all.WebBrowser.ExecWB(7,1)   打印预览
 20 
 21 或者:
 22 
 23 execScript("document.all.WebBrowser.ExecWB 7, 1","VBScript"); 
 24 
 25 3 隐藏不打印的页面元素和分页
 26  CSS 有个Media 属性,可以分开设置打印和显示的格式。
 27 如 <style media="print" type="text/css"> …</style> 中间的格式将只在打印时起作用,不会影响显示界面。
 28 所以可以设定
 29 <style media="print" type="text/css"> 
 30 .Noprint{display:none;} 
 31 .PageNext{page-break-after: always;} 
 32 </style> 
 33 然后给不想打印的页面元素添加: class="Noprint" ,那就不会出现在打印和打印预览中了。
 34 想分页的地方添加:  <div class="PageNext"></div> 就可以了。
 35 
 36 4.打印页面的特定部分
 37 
 38 我是通过将需要打印的特定部分另建一个页面,然后装入主页面的一个IFrame中,再调用IFrame的打印方法,只打印IFrame中的内容实现的。
 39 如:
 40 <iframe style="visibility: visible" name="FrameId" width="100%" height="30%" src="NeedPrintedPage.asp"></iframe>
 41 下面的pringFrame  js函数将只打印Iframe中的内容,可以直接引用使用,如printFrame(FrameId);
 42 
 43 
 44 window.print = printFrame;
 45 // main stuff
 46 function printFrame(frame, onfinish) {
 47   if ( !frame ) frame = window;
 48   function execOnFinish() {
 49     switch ( typeof(onfinish) ) {
 50       case "string": execScript(onfinish); break;
 51       case "function": onfinish();
 52     }
 53     if ( focused && !focused.disabled ) focused.focus();
 54   }
 55   if (( frame.document.readyState !== "complete") &&( !frame.document.confirm("The document to print is not downloaded yet! Continue with printing?") ))
 56   {
 57     execOnFinish();
 58     return;
 59   }
 60  
 61   var eventScope = printGetEventScope(frame);
 62   var focused = document.activeElement;
 63  
 64   window.printHelper = function() {
 65     execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
 66     printFireEvent(frame, eventScope, "onafterprint");
 67     printWB.outerHTML = "";
 68     execOnFinish();
 69     window.printHelper = null;
 70   }
 71  
 72   document.body.insertAdjacentHTML("beforeEnd",
 73     "<object id=\"printWB\" width=0 height=0  74     classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\">");
 75  
 76   printFireEvent(frame, eventScope, "onbeforeprint");
 77   frame.focus();
 78   window.printHelper = printHelper;
 79   setTimeout("window.printHelper()", 0);
 80 }
 81 
 82 // helpers
 83 function printIsNativeSupport() {
 84   var agent = window.navigator.userAgent;
 85   var i = agent.indexOf("MSIE ")+5;
 86   return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0;
 87 }
 88 function printFireEvent(frame, obj, name) {
 89   var handler = obj[name];
 90   switch ( typeof(handler) ) {
 91     case "string": frame.execScript(handler); break;
 92     case "function": handler();
 93   }
 94 }
 95 function printGetEventScope(frame) {
 96   var frameset = frame.document.all.tags("FRAMESET");
 97   if ( frameset.length ) return frameset[0];
 98   return frame.document.body;
 99 }
100 
101 Iframe中所装载页面的打印效果在所装载页面设置就可以了,如分页等。
102 
103 5.后台打印
104 
105 我是通过建一个隐藏Iframe实现的,当然仍然会有页面装载的过程。
106 下面的函数创建Iframe装载页面并打印。如 printHidden(url)  //url为页面地址
107 
108 function printHidden(url) {
109   document.body.insertAdjacentHTML("beforeEnd",
110     "<iframe name=printHiddenFrame width=0 height=0></iframe>");
111   var doc = printHiddenFrame.document;
112   doc.open();
113   doc.write("<body onload=\"parent.onprintHiddenFrame()\">");
114   doc.write("<iframe name=printMe width=0 height=0 src=\"" + 
115       url + "\"></iframe>");
116   doc.write("</body>");
117   doc.close();
118 }
119 function onprintHiddenFrame() {
120   function onfinish() {
121     printHiddenFrame.outerHTML = "";
122     if ( window.onprintcomplete ) window.onprintcomplete();
123   }
124   printFrame(printHiddenFrame.printMe, onfinish);
125 }
126 
127 它用到了printFrame,所以别忘了引用前面的函数。

 

还是WebBrowser,打印设置及JavaScript代码

标签:

原文地址:http://www.cnblogs.com/zyyxxk/p/4563581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!