日期:2014-05-16  浏览次数:20354 次

js打印控件使用
关于如何实现jsp页面横向打印,去掉页眉页脚的打印,网络上有很多。但是,都不是很好,都有缺陷。在此。我把我这次实现的方法,告诉大家,希望能给大家一些帮助了。以下这些代码都是我自己通过测试后发表。大家如果觉得,很好,就给点掌声。谢谢!有什么不足,请提出!

<!--打印控件 -->
<object id="factory" style="display:none" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" codebase="smsx.cab" viewastext></object>

在页面头部创建控件:从网站上下载smsx.cab控件。把这个smsx.cab控件放在跟打印页面同一目录。

下面是js代码:


<script language="javascript">
//横向打印设置
function pageprint() {
   document.all("print").style.display="none";
   factory.printing.header = "";
   factory.printing.footer = "";
   factory.printing.portrait = false; //portrait是指打印方向,设置为true就是纵向,false就是横向。
   factory.DoPrint(false); //设置为false,直接打印
   document.all("print").style.display="";
}
//打印预览
function pagePrintView(){
   document.all("print").style.display="none";
   factory.printing.header = "";
   factory.printing.footer = "";
   factory.printing.portrait = false; //portrait是指打印方向,设置为true就是纵向,false就是横向。
   factory.printing.Preview();//打印预览
   document.all("print").style.display="";
}

function pagePrintSet(){
   factory.printing.PageSetup();//打印设置
}
</script>

html代码:

<div style="position:absolute;top:26px;left:103px;" id="print">
<!-- 打印 -->
<span style="font-family:'Arial',sans-serif;font-size:22.964;"><input value="打 印" type="button" onClick="pageprint()"/></span>
<span style="font-family:'Arial',sans-serif;font-size:22.964;"><input value="打印预览" type="button" onClick="pagePrintView()"/></span>
<span style="font-family:'Arial',sans-serif;font-size:22.964;"><input value="打印设置" type="button" onClick="pagePrintSet()"/></span>
</div>
--------------------------------------------------------------------


<head>


  <script language="javascript">
  function printsetup(){
// 打印页面设置
wb.execwb(8,1);

}
function printpreview(){
// 打印页面预览
wb.execwb(7,1);
}

function printit()
{
if (confirm('确定打印吗?')) {
wb.execwb(6,6)
}else{
}
}
  </script>
<style media=print type="text/css">  
.noprint{visibility:hidden}  
</style>
</head>
<body>
<div class="noprint">
<input type='button' name=button_print value="打印" onclick="javascript:printit()"/>
<input type='button' name=button_setup value="打印页面设置" onclick="javascript:printsetup();"/>
<input type='button' name=button_show value="打印预览" onclick="javascript:printpreview();"/>
</div>
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
</body>