>  기사  >  웹 프론트엔드  >  웹 페이지 페이징 인쇄_자바스크립트 기술 구현

웹 페이지 페이징 인쇄_자바스크립트 기술 구현

WBOY
WBOY원래의
2016-05-16 18:51:221062검색
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 속성이 있습니다. 인쇄 및 표시 형식을 별도로 설정할 수 있습니다.
예를 들어 중간에 있는 형식은 인쇄 중에만 작동하며 디스플레이 인터페이스에는 영향을 미치지 않습니다.
그래서

그런 다음 인쇄하지 않을 페이지 요소에 class="Noprint"를 추가하면 인쇄 및 인쇄 미리보기에 표시되지 않습니다.
페이지를 매길 위치를 추가하세요:


4. 페이지의 특정 부분 인쇄 인쇄해야 할 특정 부분에 대해 새 페이지를 만든 다음 이를 메인 페이지의 IFrame에 로드한 다음 IFrame - IFrame의 콘텐츠만 인쇄합니다.
예:

다음 springFrame 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,所以别忘了引用前面的函数。
以下为demo:


报表
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0"
width="0">





onclick="document.all.WebBrowser.ExecWB(6,6)">
onclick="document.all.WebBrowser.ExecWB(8,1)">
onclick="document.all.WebBrowser.ExecWB(7,1)">
onclick="printHidden(FrameId)">


1


2

") ;
var doc = printHiddenFrame.document;
doc.open();
doc.write("");
doc.write("");
doc.write("");
doc.close();
}
함수 onprintHiddenFrame() {
함수 onfinish() {
printHiddenFrame.outerHTML = "";
if ( window.onprintcomplete ) window.onprintcomplete();
}
printFrame(printHiddenFrame.printMe, onfinish);
}

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.