1. First introduce a WebBrowser on the page to be printed, you can directly add:
Copy codeThe Code is as follows: <object id = "WebBrowser" classid = CLSID: 8856F961-340A-11D0-A96B-00C04FD705A2 height = "0" width = "0">
</Object>
To the page, or use JavaScript to temporarily add it as needed:Copy codeThe Code is as follows: document. body. insertAdjacentHTML ("beforeEnd ",
"<Object id = \" WebBrowser \ "width = 0 height = 0 \
Classid = \ "clsid: 8856F961-340A-11D0-A96B-00C04FD705A2 \"> ");
2. Page settings and print preview
Directly callCopy codeThe Code is as follows: document. all. WebBrowser. ExecWB (6) print directly
Document. all. WebBrowser. ExecWB (8, 1) page settings
Document. all. WebBrowser. ExecWB (7,1) print preview
Or:Copy codeThe Code is as follows: execScript ("document. all. WebBrowser. ExecWB 7, 1", "VBScript ");
3. Hide non-printable page elements and pages
CSS has a Media attribute that allows you to set the print and display formats separately.
For example, <style media = "print" type = "text/css">... </Style> the format in the middle will only take effect during printing and will 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 "class =" Noprint "to the page element that you do not want to print. This will not appear in print or print preview.
Add <div class = "PageNext"> </div> to the page.
4. Print specific parts of the page
Create a page for a specific part to be printed, load it into an IFrame on the home page, and call the IFrame printing method to print only the content in the IFrame.
For example:
<Iframe style = "visibility: visible" name = "FrameId" width = "100%" height = "30%" src = "NeedPrintedPage. asp"> </iframe>
The following pringFrame js function will print only the content in Iframe and can be directly referenced for use, such as printFrame (FrameId );Copy codeThe Code is as follows: 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.doc ument. readyState! = "Complete ")&&(! Frame.doc ument. 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 ",
"<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.exe cScript (handler); break;
Case "function": handler ();
}
}
Function printGetEventScope (frame ){
Var frameset = frame.doc ument. all. tags ("FRAMESET ");
If (frameset. length) return frameset [0];
Return frame.doc ument. body;
}
You can set the print effect of the page loaded in Iframe on the loaded page, such as paging.
5. Background Printing
By creating a hidden Iframe, there is still 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 ",
"<Iframe name = printHiddenFrame width = 0 height = 0> </iframe> ");
Var doc = printHiddenFrame.doc ument;
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 reference the previous function.
Demo:
<Html>
<Head>
<Title> Report </title>
<Object id = "WebBrowser"
Classid = CLSID: 8856F961-340A-11D0-A96B-00C04FD705A2 height = "0"
Width = "0">
</Object>
<Style media = "print" type = "text/css">
. Noprint {display: none ;}
. PageNext {page-break-after: always ;}
</Style>
</Head>
<Body>
<Div class = "Noprint">
<Input type = "button" value = "print"
Onclick = "document. all. WebBrowser. ExecWB (6, 6)">
<Input type = "button" value = "printset"
Onclick = "document. all. WebBrowser. ExecWB (8, 1)">
<Input type = "button" value = "view"
Onclick = "document. all. WebBrowser. ExecWB (7,1)">
<Input type = "button" value = "frame"
Onclick = "printHidden (FrameId)">
</Div>
<Div class = "PageNext">
1
</Div>
<Div class = "PageNext">
2
</Div>
<Iframe style = "visibility: visible" diplay = "none" name = "FrameId" width = "100%"
Height = "30%" src = "2.html"> </iframe>
</Body>
</Html>
<Script type = "text/javascript">
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.doc ument. readyState! = "Complete ")&&(! Frame.doc ument. 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 ",
"<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.exe cScript (handler); break;
Case "function": handler ();
}
}
Function printGetEventScope (frame ){
Var frameset = frame.doc ument. all. tags ("FRAMESET ");
If (frameset. length) return frameset [0];
Return frame.doc ument. body;
}
Function printHidden (url ){
Document. body. insertAdjacentHTML ("beforeEnd ",
"<Iframe name = printHiddenFrame width = 0 height = 0> </iframe> ");
Var doc = printHiddenFrame.doc ument;
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 );
}
</Script>