2012-11-06 93 views
0

我希望我的web應用程序在出現後自動打印出彈出頁面,而不要求客戶端選擇打印機進行選擇。沒有對話框的ASP.Net打印

如何使用java-script或ajax處理ASP.Net中的無提示打印,或者對於這種情況最適合的解決方案是什麼?

回答

0

你不能有很好的理由,如:

  • 用戶應該隨時都可以選擇他們想要使用的打印機。

  • 用戶應該隨時都可以選擇是否打印的東西還是不(想象一下,將不斷飛出您的打印機,否則垃圾郵件)

+0

它可以使用ActiveX控制 – user1194842

+0

它也可能與Flash,Silverlight的或Java小程序來完成,但似乎並沒有要你問什麼來完成。這是否會在您擁有用戶軟件控制權的商業環境中使用? – Fishcake

+0

是的,這個應用程序是在一家公司,每天有大約300個分支和每天打印數千篇論文,所以它浪費時間讓用戶總是按下ctrl + P選擇打印機爲每張紙打印。我希望我可以罰款一個解決方案,因爲它對我來說很重要 – user1194842

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> 
+0

感謝您的回答,如果您不僅將工作代碼放入其中,而且還解釋您的代碼(解決方案的步驟)到OP。 –

+0

哦..它的服務器端打印不是客戶端。 當我在服務器上託管此解決方案並將其與客戶端一起使用時,我找不到打印機。 –