使用JS+WebBrowser解决Web打印问题(完成事件) 2021-12-19 我主要使用了IE内置的WebBrowser控件,无需用户下载和安装。WebBrowser有很多功能,除打印外的其他功能就不再赘述了,你所能用到的打印功能也几乎全部可以靠它完成,下面的问题就是如何使用它了。先说显示后打印,后面说后台打印。 1.首先引入一个WebBrowser在需要打印的页面,可以直接添加: 到页面,或者使用JavaScript在需要的时候临时添加也可以: document.body.insertAdjacentHTML("beforeEnd", "classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\">"); 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 stuff function 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", "classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\">"); printFireEvent(frame, eventScope, "onbeforeprint"); frame.focus(); window.printHelper = printHelper; setTimeout("window.printHelper()", 0); } // helpers function 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已经为我们提供了解决方案,我们只要结合需求把它应用好就行了。 推荐阅读 能源行业大数据分析,能源系统与数据分析 冰糖红梨水熬多长时间能熟,冰糖红梨水熬多长时间能熟透 Qt for Android之Hello World Excel有密码怎么破解?Excel密码破解方法介绍 2023年度横州城乡居民医保退费流程 广西城乡医保退费通知 电蚊香多久换一个 192号段迟迟不能放号运营,广电5G到底还有什么没搞定? 如何注册品牌名称和商标 注册品牌需要什么手续,怎么申请品牌,注册商标 怎么查手机号注册过哪些软件 软件需要注册什么专利保护,企业软件 迎接春天的超可爱唯美文案 适合疫情结束春天来临的唯美句子 河北哪些市经济最好 Pygame实战|【Pygame实战】众志成城 同心战“疫”,为抗疫加油,只待春暖花开(附抗疫小游戏) 长屿硐天熊猫馆怎么样 台州长屿硐天熊猫馆怎么去 王智38岁 王智因为选人事件被骂 富贵籽摆放客厅什么位置好 富贵籽适合放家里吗 2023东莞中考民办普通高中补录条件 东莞2020年中考补录学校 营养素|煎炒蒸煮焯,不同的蔬菜烹饪法,营养成分流失的差别有多大? 新东方教育机构靠谱吗 新世界教育是正规的吗 wordpress模板淘宝客模板下载,wordpress淘宝客自动采集 意志力在成功中的作用 如何锻炼意志力 parallels|parallels desktop 解决网络初始化失败问题 考研英语阅读终极解决方案——阅读理解如何巧拿高分 由浅入深理解AOP 【译】20个更有效地使用谷歌搜索的技巧 mybatisplus如何在xml的连表查询中使用queryWrapper MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决 MybatisPlus使用queryWrapper如何实现复杂查询 SpringBoot调用公共模块的自定义注解失效的解决 解决SpringBoot引用别的模块无法注入的问题 iOS中的Block