梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
打印是一个头痛的问题.
AxtiveX控件收费,用户使用需降低安全级别.
最后还是打算用webBrowser打印.

1.js
/*
*设置页眉和页脚
*/

function setPageHF(isSet)
{
    
try
    

        
var regWriteShell = new ActiveXObject("WScript.Shell");
        
var regKey="HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
        
if(isSet)
        
{
            
//设置
            regWriteShell.RegWrite(regKey+"header","");
            regWriteShell.RegWrite(regKey
+"footer","&b科印传媒 第&p页 共&P页");
            
//regWriteShell.RegWrite(regKey+"footer","");
        }

        
else
        

            
//清除
            regWriteShell.RegWrite(regKey+"header","");
            regWriteShell.RegWrite(regKey
+"footer","");
        }

    }
catch(e){} 
}


/*
*要完成打印的必须信息
*/

function writeInfo()
{
    
//对象信息
    var objInfo = "<object id='WebBrowser' width=0 height=0 classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>";
    document.write(objInfo);
    
    
//相关CSS
    var cssInfo = "<style media='print'>";
    cssInfo 
+= ".noPrint{display:none;background-color:#939392;}";
    cssInfo 
+= ".nextPage{page-break-after: always;}";
    cssInfo 
+= "</style>";
    
//document.write(cssInfo);
}


/*
*打印
*/

function print()
{
    document.all.WebBrowser.Execwb(
6,1);
}


/*
*直接打印
*/

function printDirect ()
{
    document.all.WebBrowser.ExecWB(
6,6);
}


/*
*打印预览
*/

function printView()
{
    document.all.WebBrowser.ExecWB(
7,1);
}


/*
*打印设置
*/

function printSet()
{
    document.all.WebBrowser.ExecWB(
8,1);
}

2.table html
   1).第个打印页有表头
   2).每页表格线连续
StringBuffer printPageStr = new StringBuffer();
        
// 当前页DIV
        printPageStr.append("<div id=\"page").append(currentPage).append("\" class='nextPage'>");
        printPageStr.append(
"<table id=\"table_1");
        printPageStr.append("\" width=\"100%\" class=\"tabp\"  cellpadding='2' cellspacing='0'>");
        // 表头
        printPageStr.append("<tr>");
        printPageStr.append(
"<thead style='display:table-header-group;font-weight:bold;'>");
        
        
for (Object headerName : baseInfoHeadLst)
        
{
            printPageStr.append(
"<th nowrap=\"nowrap\"  class='thp' style='border-top:1px solid #000000;'>");
            printPageStr.append(headerName 
== null ? "" : headerName);
            printPageStr.append(
"</th>");
        }

        printPageStr.append(
"</thead></tr>");
        
// 表体
        for(List innerLst : dataLst)
        
{
            printPageStr.append(
"<tr>");
            
for(Object data : innerLst)
            
{
                printPageStr.append(
"<td nowrap=\"nowrap\" class='tdp'>");
                printPageStr.append(data 
== null ? "" : data).append("&nbsp");
                printPageStr.append(
"</td>");
            }

            printPageStr.append(
"</tr>");
        }

        printPageStr.append(
"</table>");
        
        printPageStr.append(
"<br/>");
        printPageStr.append(
"</div>");
        
return printPageStr.toString();

3.jsp
<!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>
    
<title>打印</title>
    
    
<meta http-equiv="pragma" content="no-cache"/>
    
<meta http-equiv="cache-control" content="no-cache"/>
    
<meta http-equiv="expires" content="0"/>    
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"/>
    
<meta http-equiv="description" content="This is my page"/>
      
<script type="text/javascript" src="/js/jquery.js"></script>
      
<script type="text/javascript" src="/js/print2/print.js"></script>
    
<style    type="text/css" media='print'> 
        .noprint 
{display: none;}
    
</style> 
    
<style    type="text/css">
        .tdp 
        
{ 
            border-bottom
: 1px solid #000000; 
            border-left
: 1px solid #000000; 
            border-right
: 0 solid #ffffff; 
            border-top
: 0 solid #ffffff; 
        
        
}

        .thp
        
{
            border-bottom
: 1px solid #000000; 
            border-left
: 1px solid #000000; 
            border-right
: 0 solid #ffffff; 
            border-top
: 1px solid #ffffff; 
        
}
 
        .tabp 
        
{ 
            border
:1px solid #000000;
            border-top
:1px solid #000000;
            border-right
:2px solid #000000;
        
}
 
    
</style>
    
<script type="text/javascript">
         writeInfo();
        setPageHF(
true);
        
        
/** 切换行是否打印 */
        
function switchPrintRW(tableId)
        
{
            $(
"#"+tableId).find("tr").dblclick(function(i){
                
if($(this).prevAll().length>=0)
                
{
                    
//如果存在则移作此css,如果不存在则添加此css类
                    $(this).find("td").each(function(j){
                        
if($(this).css("background-color")!="#939392")
                        
{
                            $(
this).addClass("noprint");
                            $(
this).css({"background-color":"#939392"});
                        }

                        
else
                        
{
                            $(
this).removeClass("noprint");
                            $(
this).css({"background-color":"white"});
                        }

                    }
);
                }

            }
);
        }

        
        
/** 切换列是否打印 */
        
function switchPrintTD(tableId)
        
{
            $(
"#"+tableId).find("th").dblclick(function(){
                
//取出第几列
                var tdCount = $(this).prevAll().length;
                
//取出总列数
                var rowTdTotalCount = $("#"+tableId).find("th").length;
                
//alert(tdCount + "" + "/" + ""+ rowTdTotalCount);
                
                
//改变列的样式
                $(this).toggleClass("noprint");
                
if($(this).attr("class").indexOf("noprint")>=0)
                
{
                    $(
this).css({"background-color":"#939392"});
                }

                
else
                
{
                    $(
this).css({"background-color":"white"});
                }

                    
                
//改变列的样式
                $("#"+tableId).find('td').each(function(i){
                    
if(i%rowTdTotalCount == tdCount)
                    
{
                        
//如果存在则移作此css,如果不存在则添加此css类
                        $(this).toggleClass("noprint");
                        
if($(this).attr("class").indexOf("noprint")>=0)
                        
{
                            $(
this).css({"background-color":"#939392"});
                        }

                        
else
                        
{
                            $(
this).css({"background-color":"white"});
                        }

                    }

                }
);
            }
);
        }


        $(document).ready(
function(){
            
// 添加列事件
            //;switchPrintRW("table_1");
            switchPrintTD("table_1");    
        }
);                
    
</script>
  
</head>
  
<body oncontextmenu="self.event.returnValue=false" onselectstart="return false">
      
<input type="button" value="打印预览" class="noprint"  onClick="javascript:printView();" style="background:#ccc url('../images/button_bg.gif') no-repeat;border:0px;color:#333;width:100px;height:27px;font-size:13px;font-weight:normal;text-align:center;vertical-align:middle;"/><input type="button" value="打印" class="noprint"  onClick="javascript:printSet();" style="background:#ccc url('../images/button_bg.gif') no-repeat;border:0px;color:#333;width:100px;height:27px;font-size:13px;font-weight:normal;text-align:center;vertical-align:middle;"/><input type="button" value="打印" class="noprint"  onClick="javascript:print();" style="background:#ccc url('../images/button_bg.gif') no-repeat;border:0px;color:#333;width:100px;height:27px;font-size:13px;font-weight:normal;text-align:center;vertical-align:middle;"/><%=printPagesStr%>
  
</body>
</html>

posted on 2010-05-27 14:06 HUIKK 阅读(2094) 评论(2)  编辑  收藏

评论

# re: 还是WebBrowser打印 2010-05-27 14:31 惠万鹏
<!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">  回复  更多评论
  

# re: 还是WebBrowser打印 2010-05-27 14:32 惠万鹏
上面这句重要  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: