在ASP.NET应用程序中,常常需要在浏览网页的同时,对网页中的内容进行打印。通过JavaScript打印方法和WebBrowser组件可以这病Web窗体的打印功能。
一、 打印Web窗本
protected void Page_Load(object sender, EventArgs e)
{
string path = Server.MapPath(@"App_Data/TextFile.txt");
StreamReader reader = new StreamReader(path, System.Text.Encoding.Default);
lblTitle.Text = reader.ReadLine();
lblContext.Text = reader.ReadToEnd();
reader.Close();
}
protected void btnPrint_Click(object sender, EventArgs e)
{
Response.Write("<script>window.print()</script>");
}
二、 利用WebBrowser实现打印
WebBrowser是IE内置的浏览器组件,无须用户下载。它的优点是客户端能独立打印目标文档,减轻服务器负荷;缺点是源文档的分析操作复杂,并且源文档的打印内容要有约束。
<body style="text-align: center">
<form id="Form1" runat="server">
<table>
<tr>
<td style="width: 466px; background-color: #ffffff; text-align: center;">
<asp:Label ID="Label1" runat="server" Font-Size=" 12pt " Font-Bold="True" ForeColor="Blue"></asp:Label>
</td></tr>
<tr><td style="font-size: 9pt ; width: 466px; text-indent: 8pt ; text-align: left; height: 380px;">
<asp:Label ID="Label2" runat="server" Font-Size=" 9pt " Height="376px" Width="430px" BorderColor="#8080FF" BorderWidth="1px" ForeColor="#404040"></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: center; width: 466px; height: 46px;">
<object id="WebBrowser" height="0" width="0" classid="CLSID: 8856F 961 -340A -11D0-A96B -00C 04FD 705A 2">
</object>
<asp:Panel ID="panel1" runat="server" Width="460px" Height="12px">
<input onclick="document.all.WebBrowser.ExecWB(6,1);" type="button" value="打印" id="Button1" >
<input onclick="document.all.WebBrowser.ExecWB(6,6);" type="button" value="直接打印" />
<input onclick="document.all.WebBrowser.ExecWB(8,1);" type="button" value="页面设置" />
<input onclick="document.all.WebBrowser.ExecWB(7,1);" type="button" value="打印预览" id="Button4" />
<input type="button" value="关闭窗口" onclick="javascript:window.close()" />
</asp:Panel></td>
</tr>
</table>
</form>
</body>
protected void Page_Load(object sender, EventArgs e)
{
string path = Server.MapPath(@"App_Data/TextFile.txt");
StreamReader reader = new StreamReader(path, System.Text.Encoding.Default);
lblTitle.Text = reader.ReadLine();
lblContext.Text = reader.ReadToEnd();
reader.Close();
}
protected void btnPrint_Click(object sender, EventArgs e)
{
Response.Write("<script>window.print()</script>");
}
Document.all.WebBrowser.ExecWB(6,1) | 表示打印 |
Document.all.WebBrowser.ExecWB(6,6) | 表示直接打印 |
Document.all.WebBrowser.ExecWB(7,1) | 表示打印预览 |
Document.all.WebBrowser.ExecWB(8,1) | 表示页面设置 |
|
|
Document.all.WebBrowser.ExecWB(1,1) | 表示打开 |
Document.all.WebBrowser.ExecWB(2,1) | 表示关闭现在所有的IE窗口,并打开一个新窗口。 |
Document.all.WebBrowser.ExecWB(4,1) | 表示保存网页 |
Document.all.WebBrowser.ExecWB(0,1) | 表示查看钢页属性 |
Document.all.WebBrowser.ExecWB(15,1) | 表示撤消 |
Document.all.WebBrowser.ExecWB(17,1) | 表示全选 |
Document.all.WebBrowser.ExecWB(22,1) | 表示刷新 |
Document.all.WebBrowser.ExecWB(45,1) | 表示关闭窗体无提示。 |