取得正确的页面设置 <HTML XMLNS:IE> <HEAD> <?IMPORT NAMESPACE="IE" IMPLEMENTATION="#default"> <STYLE TYPE="text/css"> .lorstyle { width:5.5in; height:8in; margin:1in; background:white; } .pagestyle { background:white; border-left:1 solid black; border-top:1 solid black; border-right:4 solid black; border-bottom:4 solid black; width:8.5in; height:11in; margin:10px; overflow:hidden; } .headerstyle { position:absolute; top:.25in; width:6in; left:1in; } .footerstyle { position:absolute; top:10.5in; width:6in; left:1in; } </STYLE> <SCRIPT LANGUAGE="JScript"> var iNextPageToCreate = 1; var oPageStyleClass; var oLorStyleClass; var oHeaderStyleClass; var oFooterStyleClass; // Returns the object corresponding to a named style rule function FindStyleRule(styleName) { for (i = 0; i < document.styleSheets.length; i++) { for (j = 0; j < document.styleSheets(i).rules.length; j++) { if (document.styleSheets(i).rules(j).selectorText == styleName) return document.styleSheets(i).rules(j); } } } function Init() { AddFirstPage(); oPageStyleClass = FindStyleRule(".pagestyle"); oLorStyleClass = FindStyleRule(".lorstyle"); oHeaderStyleClass = FindStyleRule(".headerstyle"); oFooterStyleClass = FindStyleRule(".footerstyle"); oPageStyleClass.style.width = printer.pageWidth/100 + "in"; oPageStyleClass.style.height = printer.pageHeight/100 + "in"; oLorStyleClass.style.marginTop = printer.marginTop/100 + "in"; oLorStyleClass.style.marginLeft = printer.marginLeft/100 + "in"; oLorStyleClass.style.width = (printer.pageWidth - (printer.marginLeft + printer.marginRight))/100 + "in"; oLorStyleClass.style.height = (printer.pageHeight - (printer.marginTop + printer.marginBottom))/100 + "in"; oHeaderStyleClass.style.left = printer.marginLeft/100 + "in"; oHeaderStyleClass.style.top = printer.unprintableTop/100 + "in"; oHeaderStyleClass.style.width = oLorStyleClass.style.width; oFooterStyleClass.style.left = printer.marginLeft/100 + "in"; oFooterStyleClass.style.width = oLorStyleClass.style.width; oFooterStyleClass.style.bottom = printer.unprintableBottom/100 + "in"; } function CheckPrint() { switch (dialogArguments.__IE_PrintType) { case "Prompt": if (printer.showPrintDialog()) PrintPrep(); break; case "NoPrompt": PrintPrep(); break; case "Preview": default: break; } } function AddFirstPage() { newHTML = "<IE:DEVICERECT ID='page1' MEDIA='print' CLASS='pagestyle'>"; newHTML += "<IE:LAYOUTRECT ID='layoutrect1' CONTENTSRC='document' ONLAYOUTCOMPLETE='OnRectComplete()' NEXTRECT='layoutrect2' CLASS='lorstyle'/>"; newHTML += "</IE:DEVICERECT>"; pagecontainer.insertAdjacentHTML("afterBegin", newHTML); headfoot.textHead = printer.header; headfoot.textFoot = printer.footer; headfoot.url = dialogArguments.__IE_BrowseDocument.URL; headfoot.title = dialogArguments.__IE_BrowseDocument.title; headfoot.page = 1; AddHeaderAndFooterToPage(1); iNextPageToCreate = 2; } function OnRectComplete() { if (event.contentOverflow == true) AddNewPage(); else { headfoot.pageTotal = document.all.tags("DEVICERECT").length; for (i = 1; i <= headfoot.pageTotal; i++) AddPageTotalToPages(i); setTimeout("CheckPrint();", 100); } } function AddNewPage() { document.all("layoutrect" + (iNextPageToCreate - 1)).onlayoutcomplete = null; headfoot.page = iNextPageToCreate; newHTML = "<IE:DEVICERECT ID='page" + iNextPageToCreate + "' MEDIA='print' CLASS='pagestyle'>"; newHTML += "<IE:LAYOUTRECT ID='layoutrect" + iNextPageToCreate + "' ONLAYOUTCOMPLETE='OnRectComplete()' NEXTRECT='layoutrect" + (iNextPageToCreate + 1) + "' CLASS='lorstyle'/>"; newHTML += "</IE:DEVICERECT>"; pagecontainer.insertAdjacentHTML("beforeEnd", newHTML); AddHeaderAndFooterToPage(iNextPageToCreate); iNextPageToCreate++; } function AddHeaderAndFooterToPage(pageNum) { newHeader = "<DIV CLASS='headerstyle'>" + headfoot.htmlHead + "</DIV>"; newFooter = "<DIV CLASS='footerstyle'>" +headfoot.htmlFoot + "</DIV>"; document.all("page" + pageNum).insertAdjacentHTML("afterBegin", newHeader); document.all("page" + pageNum).insertAdjacentHTML("beforeEnd", newFooter); } function AddPageTotalToPages() { oSpanCollection = document.all.tags("span"); for (i = 0; i < oSpanCollection.length; i++) { if (oSpanCollection[i].className == "hfPageTotal") oSpanCollection[i].innerText = headfoot.pageTotal; } } function PrintPrep() { if (layoutrect1.contentDocument.readyState == "complete") { // This block will be called when printing with user prompt // because the Print dialog box gives time for the content // document to complete loading PrintNow(); } else { // This block will usually be called when printing w/o user prompt // and sets an event handler that listens for the loading of the content // document before printing. Sometimes, however, the content document // will be loaded in time for the previous block to execute layoutrect1.contentDocument.onreadystatechange = PrintWhenContentDocComplete; } } function PrintWhenContentDocComplete() { if (layoutrect1.contentDocument.readyState == "complete") { layoutrect1.contentDocument.onreadystatechange = null; PrintNow(); } } function PrintNow() { var startPage; var endPage; var oDeviceRectCollection = document.all.tags("DEVICERECT"); if (dialogArguments.__IE_PrintType == "NoPrompt" || printer.selectedPages == false) { startPage = 1; endPage = oDeviceRectCollection.length; } else if (printer.currentPage == true) { // Determine current page, if necessary. // Normally, the print current page option is disabled because a print // template has no way of determining which section of a document is displayed // in the browser and cannot calculate a current page. In print preview, // a print template can enable the print current page option because the // template can determine which page is currently showing in the print // preview dialog. The print preview window would need a user interface // with a print button so that the user could print from the print preview // window. See template7.htm for an example showing how to add a user // interface to a print preview template. } else { startPage = printer.pageFrom; endPage = printer.pageTo; if (startPage > endPage) { alert("Error: Start page greater than end page"); return; } if (startPage > oDeviceRectCollection.length) { alert("Error: Start page greater than number of pages in print job."); return; } if (endPage > oDeviceRectCollection.length) { alert("Warning: End page greater than number of pages in print job. Continuing Print Job."); endPage = oDeviceRectCollection.length; } } printer.startDoc("Printing from Tmplt6.htm"); for (i = startPage - 1; i < endPage; i++) printer.printPage(oDeviceRectCollection[i]); printer.stopDoc(); } </SCRIPT> </SCRIPT> <HEAD> <BODY ONLOAD="Init()"> <IE:TEMPLATEPRINTER ID="printer"/> <IE:HEADERFOOTER ID="headfoot"/> <DIV ID="pagecontainer"> <!-- Dynamically created pages go here. --> </DIV> </BODY> </HTML>
为了使得用户在打印预览的时候可以缩放页面的大小,本模板增加了一个函数——Zoomer。Zoomer是Zoom In 和Zomm Out按钮的onmouseup事件和文本框的onkeyup事件处理程序。当用户单击任何一个按钮或者更改zoom文本框的值的时候,它更改ID为zoomcontainer的DIV元素的zoom属性(初始值是50%)。