抱着“取之于众 服务于众”的思想,我总结了一下,把它拿到网上来与大家分享,希望能帮助遇到类似问题的朋友。 我主要使用了IE内置的WebBrowser控件,无需用户下载和安装。WebBrowser有很多功能,除打印外的其他功能就不再赘述了,你所能用到的打印功能也几乎全部可以靠它完成,下面的问题就是如何使用它了。先说显示后打印,后面说后台打印。 1.首先引入一个WebBrowser在需要打印的页面 ,可以直接添加: 到页面,或者使用JavaScript在需要的时候临时添加也可以: document.body.insertAdjacentHTML("beforeEnd", "classid=\"clsid:8856F 961-340A -11D0-A96B-00C 04FD705A 2\">"); 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 属性,可以分开设置打印和显示的格式。 如 中间的格式将只在打印时起作用,不会影响显示界面。 所以可以设定 然后给不想打印的页面元素添加: class="Noprint" ,那就不会出现在打印和打印预览中了。 想分页的地方添加:
That's it. 4. Printing a specific part of the page I achieve this by creating a new page for the specific part that needs to be printed, then loading it into an IFrame on the main page, and then calling the IFrame's print method to print only the content in the IFrame. . For example: The following springFrame js function will only print the content in the Iframe and can be referenced directly, such as 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; } The printing effect of the page loaded in the Iframe can be set on the loaded page, such as paging, etc. 5. Background printing I implemented it by building a hidden Iframe. Of course, there will still be a page loading process. The following function creates an Iframe loading page and prints it. For example, printHidden(url) //url is the page address 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); } It uses printFrame, so don’t forget to quote the previous function. In short, WebBroswer has provided us with a solution, we just need to apply it based on our needs
Statement: The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn