Use WebBrowser to completely solve web printing problems (including background printing) _javascript tips

Source: Internet
Author: User

With the idea of "serving the public", I summed it up and took it to the Internet to share with you, hoping to help friends who met similar problems.
I mainly use IE built-in WebBrowser control, no user download and installation. WebBrowser has a lot of features, in addition to printing other features will no longer repeat, you can use the printing function can almost all rely on it to complete, the following question is how to use it. First say after the display, print, behind the background print.
1. First, introduce a WebBrowser page that needs to be printed, you can add directly:
<object id= "WebBrowser" CLASSID=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height= "0" width= "0" >
</object>
To the page, or use JavaScript to temporarily add when needed:

Document.body.insertAdjacentHTML ("BeforeEnd",
"<object id=\" webbrowser\ "width=0 height=0 \
Classid=\ "Clsid:<st1:chmetcnv tcsc=" 0 "numbertype=" 1 "negative=" false "Hasspace=" false "sourcevalue=" 8856 " Unitname= "F" w:st= "on" >8856f</st1:chmetcnv>961<st1:chmetcnv tcsc= "0" numbertype= "1" negative= "True" Hasspace= "False" sourcevalue= "Unitname=" "a" w:st= "on" >-340a</st1:chmetcnv>-11d0-a96b<st1:chmetcnv tcsc= "0" numbertype= "1" negative= "True" hasspace= "False" sourcevalue= "0" unitname= "C" w:st= "on" >-00c</st1: Chmetcnv>04fd<st1:chmetcnv tcsc= "0" numbertype= "1" negative= "false" Hasspace= "false" sourcevalue= "705" Unitname= "A" w:st= "on" >705a</st1:chmetcnv>2\ ">");

2. Page Setup and print preview
As shown below, direct call can be

Document.all.WebBrowser.ExecWB (6,6) Direct printing
Document.all.WebBrowser.ExecWB (8,1) Page Setup
Document.all.WebBrowser.ExecWB (7,1) Print preview
Or:
Execscript ("Document.all.WebBrowser.ExecWB 7, 1", "VBScript");

3 Hide page elements and pagination that are not printed
CSS has a media property that allows you to set the print and display format separately.
such as <style media= "print" type= "Text/css" > ...</style> the middle of the format will only work when printing, does not affect the display interface.
So you can set
<style media= "print" type= "Text/css" >
. Noprint{display:none;}
. Pagenext{page-break-after:always;}
</style>
Then add the page elements that you do not want to print: class= "noprint", which will not appear in print and print preview.
Want to page the place add: <div class= "Pagenext" ></div> on it.

4. Print a specific part of a page
I am by the need to print a specific part of a page, and then loaded into the main page of an IFRAME, and then invoke the IFrame printing method, only the content of the IFRAME to achieve.
Such as:
<iframe style= "visibility:visible" name= "Frameid" width= "100%" height= "30%" src= "needprintedpage.asp" ></ Iframe>
The following Pringframe JS function will only print the contents of the IFRAME, can be directly referenced using, 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 isn't not download Ed 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",
"<object id=\" printwb\ "width=0 height=0 \
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 page loaded in the IFRAME can be printed on the page you are loading, such as pagination.
5. Print Spooler
I was implemented by building a hidden iframe, of course, there will still be the process of page loading.
The following function creates an IFRAME Mount page and prints it. such as Printhidden (URL)//url for page address
function Printhidden (URL) {
Document.body.insertAdjacentHTML ("BeforeEnd",
"<iframe name=printhiddenframe width=0 height=0></iframe>");
var doc = printhiddenframe.document;
Doc.open ();
Doc.write ("<body onload=\" Parent.onprinthiddenframe () ">");
Doc.write ("<iframe name=printme width=0 height=0 src=\" "+
URL + "></iframe>");
Doc.write ("</body>");
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 refer to the previous function.

In short, Webbroswer has provided us with a solution, we just need to combine the requirements to apply it to the right

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.