1、利用JavaScript实现打印页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function printPage() {
//打印当前窗体
window.print();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
ASP.NET学习
</div>
<input type="button" id="btnPrint" value="打印" onclick="printPage()" />
</form>
</body>
</html>
2、控制打印范围
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="_Default2" %>
<!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 runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function printPage() {
//获取页面内容
var bdhtml = document.body.innerHTML;
var beginstr = "<!--startprint-->";
var endstr = "<!--endprint-->";
//获取要打印的内容
var prnhtml = bdhtml.substr(bdhtml.indexOf(beginstr) + 17);
prnhtml = prnhtml.substr(0, prnhtml.indexOf(endstr));
//预览
window.document.body.innerHTML = prnhtml
//打印当前窗体
window.print();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<!--startprint-->
<div align="center">
<span>这里是我需要打印的内容</span>
</div>
<!--endprint-->
<div>
ASP.NET学习
</div>
<input type="button" id="btnPrint" value="打印需要部分" onclick="printPage()" />
</form>
</body>
</html>
在HTML中标记内容的开始位置和结束位置,然后截取需要打印的内容。
3、利用WebBrower组件实现打印
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function printTools(type) {
switch (type) {
case 1:
//打印预览
document.all.wb1.ExecWB(7,1);
break;
case 2:
//打印
document.all.wb1.ExecWB(6, 1);
break;
case 3:
//打印设置
document.all.wb1.ExecWB(8, 1);
break;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<object id="wb1" width="0" height="0" classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>
<div>
ASP.NET学习
</div>
<input type="button" value="打印预览" id="btnPreview" onclick="printTools(1)" />
<input type="button" value="打印" id="btnPrint" onclick="printTools(2)" />
<input type="button" value="打印设置" id="btnSetting" onclick="printTools(3)" />
</form>
</body>
</html>
首先使用object标签加载ExecWB组件,必须指定本页中的唯一标识ID
运行会提示加载ActiveX控件,很不爽,需要设置下
在Internet选项的安全选项中设置
4、实际开发中一般为要打印的内容单独做个页面,如:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebAppPrint.WebForm1" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
需要打印的内容
</div>
<object id='Object1' name="WebBrowser" width="0" height="0" classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'>
</object>
<script type="text/javascript">
document.getElementById("WebBrowser").ExecWB(7, 1);
window.close();
</script>
</form>
</body>
</html>
然后在主页面拖放一个Button按钮指向此页面,例如指向上面的页面:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm1.aspx");
}