zoukankan      html  css  js  c++  java
  • JS网页打印设置技巧

    转自:http://blog.sina.com.cn/s/blog_3eba8f1c0100emu4.html

    最近写一些东西需要提供网页打印功能,所以归纳总结了一下,本节主要讲述使用IE6支持打印功能,不同浏览器安全设置与支持有差异,如果不支持,请使用您的浏览器自带打印功能(或手动设置启用ActiveX控件)。书写有不足或描述不清的地方请大家指正。^-^

    利用CSS样式打印是经常使用的一种打印方法,利用它可以非常方便的实现打印页面中的指定内容和分页打印,下面将通过具体实例介绍如何利用CSS样式打印。

    [分析]:
    1.打印样式区分:打印网页带页面样式,需指明一个media='print'的样式,建议分开,如下创建军一个bankprint.css打印样式文件。
    <link rel="stylesheet" media="screen" type="text/css" href="/public/default/css/bank.css" />
    <!-- 打印样式 -->
    <link rel="stylesheet" media="print" type="text/css" href="/public/default/css/bankprint.css" />
    例:
    <style media=‘print’>

    .Noprint {display:none;}

    .PageBreak {page-break-after: always;}

    </style>
    说明:
    media类型是CSS属性媒体类型,用于直接引入媒体的属性。其语法格式如下:
    @media screen | print | projection | braille | aural | tv | handheld | all
    参数说明
      screen:指计算机屏幕。
      print:指用于打印机的不透明介质。
      projection:指用于显示的项目。
      braille:盲文系统,指有触觉效果的印刷品。
      aural:指语音电子合成器。
      tv:电视类型的媒体。
      handheld:指手持式显示设备。
      all:用于所有媒体。
    2.WebBrowser控件
    同其他控件一样,首先我们需要在页面中嵌入WebBrowser控件,不过由于该控件是IE浏览器自带的,支持浏览器默认安全设置,因此避免了安全性设置的麻烦。对于IE7及以上安全性要求更高的浏览器,您或许还是需要自定义IE的安全性级别。
    <OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" id="wb" width="0" height="0"></OBJECT>
    下面就是该控件涉及打印的功能调用,用户可以在JavaScrip中调用:
    wb.execwb(6,1); //打印,打印当前页面
    wb.execwb(7,1); //打印预览
    wb.execwb(8,1); //打印设置,调出系统打印设置对话框

    3.页眉、页脚设置:打印时,有的需要去掉页眉页脚,或替换成自已想要的。
    <script language="JavaScript">
     
       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,"");
                //&b 第&p页/共&P页 &b
            }catch(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){}
        }
    ...
    </script>

    [源码例子]:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>打印设置</title>
    <link rel="stylesheet" media="screen" type="text/css" href="http://www.chinasvf.com/Webs/public/default/css/bank.css" />
    <!-- 打印样式 -->
    <link rel="stylesheet" media="print" type="text/css" href="http://www.chinasvf.com/Webs/public/default/css/bankprint.css" />
    <script language="JavaScript">
        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,"");
                //&b 第&p页/共&P页 &b
            }catch(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){}
         
         //打印选区内容
        function doPrint() {
            pagesetup_null();
            bdhtml=window.document.body.innerHTML; 
            sprnstr="<!--startprint-->"; 
            eprnstr="<!--endprint-->"; 
            prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); 
            prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); 
            window.document.body.innerHTML=prnhtml; 
            window.print(); 
        }
        //打印页面预览
        function printpreview(){
            pagesetup_null();
            //wb.printing.header = "居左显示&b居中显示&b居右显示页码,第&p页/共&P页";
            //wb.printing.footer = "居左显示&b居中显示&b居右显示页码,第&p页/共&P页";
            try{
                wb.execwb(7,1);
            }catch(e){
                alert("您的浏览器不支持此功能,请选择'文件'->'打印预览'");
            }
        }
        //打印
        function prints(){
            pagesetup_null();
            //wb.printing.header = "居左显示&b居中显示&b居右显示页码,第&p页/共&P页";
            //wb.printing.footer = "居左显示&b居中显示&b居右显示页码,第&p页/共&P页";
            try{
                wb.execwb(6,1);
            }catch(e){
                alert("您的浏览器不支持此功能");
            }
        }
    </script>
    </head>
    <body>
    <OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" id="wb" width="0" height="0"></OBJECT>
    <div id="bankwrap">
      <div class="Noprint"><a href="http://www.chinasvf.com" style="cursor:pointer; color:#0000FF">返回首页</a></div>
      <div style="text-align:right">
        <p class="Noprint">
            <span style="cursor:pointer; color:#0000FF" onclick="javascript:window.open('http://www.chinasvf.com/Webs/Share/printhelp')" class="Noprint">打印帮助</span>
            <span style="cursor:pointer; color:#0000FF" onclick="printpreview();">打印预览</span>
            <span style="cursor:pointer; color:#0000FF" onclick="prints();" class="Noprint">打印</span>
        </p>
      </div>
      <div class="banktitle">内容</div>
    </div>
    </body>
    </html>

    相关1:網頁打印代碼大全

    2.http://topic.csdn.net/t/20050228/17/3812979.html

    3.

  • 相关阅读:
    06-图3 六度空间
    06-图2 Saving James Bond
    06-图1 列出连通集
    05-树9 Huffman Codes
    数据结构学习笔记04树(堆 哈夫曼树 并查集)
    05-树8 File Transfer
    05-树7 堆中的路径
    十天学会单片机Day1点亮数码管(数码管、外部中断、定时器中断)
    设计模式—— 四:接口隔离原则
    设计模式—— 一:单一职责原则
  • 原文地址:https://www.cnblogs.com/blsong/p/1616365.html
Copyright © 2011-2022 走看看