没有对话框的ASP.Net打印

时间:2012-11-06 07:10:57

标签: javascript asp.net printing

我希望我的网络应用程序在自动出现后立即打印弹出页面而不要求客户选择打印机进行选择。

如何使用java-script或ajax处理ASP.Net中的静默打印?或者这种情况下最合适的解决方案是什么?

3 个答案:

答案 0 :(得分:0)

你不能并且有充分的理由,例如:

  • 用户应始终能够选择要使用的打印机。

  • 用户应始终能够选择是否打印某些内容(想象一下垃圾邮件会不断地从您的打印机中飞出)

答案 1 :(得分:0)

可以使用某些第三方控件(在WPF中)。请检查这在asp.net中是否有用。

http://www.textcontrol.com/en_US/support/documentation/dotnet/n_wpf_printing.printing.htm

答案 2 :(得分:0)

//OnTouchPrint.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Printing;
using System.IO;
using System.Drawing;

namespace TokenPrint
{
    public partial class Try : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }

        }

         private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
           {

                Graphics g = e.Graphics;
                SolidBrush Brush = new SolidBrush(Color.Black);
                string printText = TextBox1.Text;
                g.DrawString(printText, new Font("arial", 12), Brush, 10, 10);

            }


        protected void Press_Click(object sender, EventArgs e)
        {
            try
            {
                string Time = DateTime.Now.ToString("yymmddHHMM");
                System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
                ps.PrintToFile = true;
               // ps.PrintFileName = "D:\\PRINT\\Print_"+Time+".oxps"; /* you can save file here */
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
                System.Drawing.Printing.StandardPrintController printControl = new System.Drawing.Printing.StandardPrintController();
                pd.PrintController = printControl;
                pd.DefaultPageSettings.Landscape = true;
                pd.PrinterSettings = ps;
                pd.Print();
                TextBox1.Text = "";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Printed Successfully.Check: Drive D')", true);


            }
            catch (Exception ex)
            {

            }


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Try.aspx");
        }
    }
}

//OnTouchPrint.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OnTouchPrint.aspx.cs" Inherits="TokenPrint.Try" %>

<!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">


    <asp:TextBox ID="TextBox1" runat="server" Width="235px" Height="142px" 
        TextMode="MultiLine"></asp:TextBox>


    <br />
    <br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ControlToValidate="TextBox1" ErrorMessage="Empty message can not be printed!" 
        ValidationGroup="vgp1"></asp:RequiredFieldValidator>
    <br />
    <br />
    <asp:Button ID="Press" runat="server" Text="Press" onclick="Press_Click" 
        ValidationGroup="vgp1" />


    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Refresh" 
        ValidationGroup="vgp2" />
&nbsp;&nbsp;&nbsp;


    </form>
</body>
</html>