簡體   English   中英

沒有對話框的ASP.Net打印

[英]ASP.Net printing without dialog box

我希望我的Web應用程序在出現后自動在彈出頁面上打印,而不要求客戶端選擇要選擇的打印機。

如何使用Java腳本或Ajax在ASP.Net中處理無聲打印,或者在這種情況下最合適的解決方案是什么?

您不能並且有充分的理由,例如:

  • 用戶應始終能夠選擇他們要使用的打印機。

  • 用戶應始終能夠選擇是否打印某些內容(想象一下垃圾郵件會不斷從打印機中飛出)

一些第三方控件對此可用(在WPF中)。 請檢查這在asp.net中是否也有用。

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

//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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM