2013-06-13 165 views
1

我是新手編程。如何直接打印斑馬打印機而不將其設置爲使用C#的默認打印機?

我想將打印機直接打印到斑馬打印機,而不將打印機設置爲DEFAULT,因爲我使用的是連接到許多打印機的一臺計算機,同時也牢記我從網上獲取記錄/數據點擊打印按鈕時的瀏覽器

我該如何實現這一目標?提前致謝。如果將打印機設置爲默認

下面的代碼工作正常

<%@ Page Language="C#" AutoEventWireup="true"%> 
<%@ Import Namespace="System.Data.SqlClient" %> 
<%@ Import Namespace="System.IO" %> 

<% 
    System.Web.Script.Serialization.JavaScriptSerializer jsoner = new System.Web.Script.Serialization.JavaScriptSerializer(); 

    string UtiWayBillNumber =Request.QueryString["UtiWayBillNumber"]; 
    string labelSerials = Request.QueryString["labelSerials"] ?? null; 
    string[] serialNumbers = labelSerials.Split('$'); 


    using (SqlConnection dbConnection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["HestoProductionControl"].ConnectionString)) 
    { 
     dbConnection.Open(); 

     SqlCommand cmd = dbConnection.CreateCommand(); 
     cmd.CommandType = System.Data.CommandType.StoredProcedure; 
     cmd.CommandText = "GM_GetShipmentDetailInformation"; 
     cmd.Parameters.AddWithValue("@utiWaybillNumber", UtiWayBillNumber); 
     SqlDataReader reader = cmd.ExecuteReader(); 
     System.Collections.Generic.List<object> labelList = new List<object>(); 

     string appPath = Request.PhysicalApplicationPath; 
     string IPAddress = HttpContext.Current.Request.UserHostAddress; 

      StringBuilder fileContents = new StringBuilder(); 

     while (reader.Read()) 
      { 

      if (labelSerials.StartsWith(" ")) 
       { 
         DateTime date = DateTime.Now; 
         string quantity = reader["PackingQuantity"].ToString(); 
         quantity = quantity.Remove(2,7); 


         fileContents.Append(reader["HestoBarcodeSerial"]); 
         fileContents.Append(","); 
         fileContents.Append(reader["CustomerStockCode"].ToString().Trim()); 
         fileContents.Append(","); 
         fileContents.Append(quantity); 
         fileContents.Append(","); 
         fileContents.Append(reader["Description"].ToString().Trim()); 
         fileContents.Append(","); 
         fileContents.Append(reader["StockCode"]); 
         fileContents.Append(","); 
         fileContents.Append(date.ToString("s")); 
         fileContents.Append(","); 
         fileContents.Append(reader["CustomerBarcodeSerial"]); 
         fileContents.Append("\r\n"); 
       } 

      else{ 
         DateTime date = DateTime.Now; 
         string quantity = reader["PackingQuantity"].ToString(); 
         quantity = quantity.Remove(2,7); 


         if (serialNumbers.Contains<string>(reader["Serial"].ToString()) == false) 
         { 
          continue; 
         } 

         fileContents.Append(reader["HestoBarcodeSerial"]); 
         fileContents.Append(","); 
         fileContents.Append(reader["CustomerStockCode"].ToString().Trim()); 
         fileContents.Append(","); 
         fileContents.Append(quantity); 
         fileContents.Append(","); 
         fileContents.Append(reader["Description"].ToString().Trim()); 
         fileContents.Append(","); 
         fileContents.Append(reader["StockCode"]); 
         fileContents.Append(","); 
         fileContents.Append(date.ToString("s")); 
         fileContents.Append(","); 
         fileContents.Append(reader["CustomerBarcodeSerial"]); 
         fileContents.Append("\r\n"); 
       } 

      }; 

      Response.Write(fileContents.ToString()); 

      Directory.CreateDirectory(appPath + "//PrintFile/" + IPAddress); 
      StreamWriter w; 

      w = File.CreateText(appPath + "//PrintFile/" + IPAddress + "/printLabels.txt"); 
      w.WriteLine(fileContents.ToString()); 
      w.Flush(); 
      w.Close(); 
    } 
%> 

回答

0

是在你的系統上的打印機列表中列出的打印機?如果是這樣,你可以使用

PrintDocument pd = new PrintDocument(); 
pd.PrinterSettings.PrinterName = "Zebra Printer"; 

// Do stuff formatting your document, like drawing strings and images (possibly a zebra?) 

if(pd.PrinterSettings.IsValid) pd.Print(); 
else MessageBox.Show("Printer is invalid."); 

注:我這一關的this thread另一個論壇上,但我使用了類似的方法來打印到特定的打印機時,我不知道這是否是默認打印機。

我剛剛注意到您可能正在使用一個網站。上述方法僅在您使用服務器端以選擇默認打印機時纔可用。

我不知道這是否可以做客戶端,但我懷疑它。它會讓你的網站訪問你的客戶的電腦,這是一個巨大的安全漏洞。我想你必須顯示一個打印對話框,用戶可以在其中選擇要使用的打印機。

+0

非常感謝很多人讓我嘗試一下,我現在有一個小線索 – IBonesh