還會更新:
HTML代碼
<head>
<script src="/Scripts/printCurrentPage.js" type="text/javascript"></script>
</head>
<body>
<object id='WebBrowser' width="0" height="0" classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'
viewastext>
</object>
Javascript代碼printCurrentPage.js文件
var hkey_root, hkey_path, hkey_key;
hkey_root = "HKEY_CURRENT_USER";
hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
// 設置網頁打印的頁眉頁腳爲空
function pagesetup_null() {
try {
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key = "header";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "");
hkey_key = "footer";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "");
} catch (e) { alert(e); }
}
// 設置網頁打印的頁眉頁腳爲默認值
function pagesetup_default() {
try {
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key = "header";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&w&b頁碼,&p/&P");
hkey_key = "footer";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&u&b&d");
} catch (e) { alert(e); }
}
function printMethod() {
pagesetup_null();
//window.print();
WebBrowser.ExecWB(6, 6);
//WebBrowser.ExecWB(6, 1);
//WebBrowser.ExecWB(7, 1);
window.opener = null;
window.close();
}
//設置默認的頁眉頁腳
function SetupPage() {
try {
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key = "header"
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&w&b頁碼,&p/&P")
hkey_key = "footer"
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&b&d") //去掉了&u 因爲我不想顯示當前打印頁的網址
hkey_key = "margin_bottom";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.39"); //0.39相當於把頁面設置裏面的邊距設置爲10
hkey_key = "margin_left";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.39");
hkey_key = "margin_right";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.39");
hkey_key = "margin_top";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.39");
}
catch (e) { }
}
//設置紙張方向
function SetupLandscape() {
try {
var wsShell = new ActiveXObject("WScript.Shell");
//打印頁面的Menubar必須可見,此操作類似按鍵盤上的Alt+F+U也就是 調出頁面設置對話框
wsShell.sendKeys('%fu');
//此操作類似按鍵盤上的Alt+A也就是 設置橫向打印
wsShell.sendKeys('%a');
//此操作類似按鍵盤上的回車 頁面設置對話框的默認焦點在 確定上 所以直接確定
wsShell.sendKeys('{ENTER}');
}
catch (e) { }
}
window.onload = function () {
//printMethod();
SetupLandscape();
//setTimeout("printMethod()", 2000);
//setTimeout("SetupLandscape()", 1000);
//setTimeout("SetupPage()", 2000);
//setTimeout("printMethod()", 3000);
}
---------------------------
<!DOCTYPE html>
<html>
<head>
<title>print</title>
<meta charset="utf-8">
<style media="print">
@page {
size: auto;
margin: 0mm;
}
</style>
</head>
<body>
<div style="width:300px; height:300px;border: 1px solid blue;">
<label>label</label>
<button>button</button>
</div>
<button onclick='print_page()'>print</button>
</body>
<script type="text/javascript">
function print_page() {
if (!!window.ActiveXObject || "ActiveXObject" in window) {
remove_ie_header_and_footer();
}
window.print();
};
function remove_ie_header_and_footer() {
var hkey_root, hkey_path, hkey_key;
hkey_path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
try {
var RegWsh = new ActiveXObject("WScript.Shell");
RegWsh.RegWrite(hkey_path + "header", "");
RegWsh.RegWrite(hkey_path + "footer", "");
} catch (e) {}
}
</script>
</html>