简体   繁体   中英

How to print direct to zebra printer without setting it as a default printer using C#?

I'm new to programming.

I want to print direct to the Zebra Printer without setting the printer as a DEFAULT because I'm using one computer that is connected to many printers, and also bear in mind i'm getting the records/DATA from a web browser when PRINT Button is clicked

How can i achieve this ? Thanks in advance.

The following code works fine if the printer is set to DEFAULTS

<%@ 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();
    }   
%>

Is the printer listed in the list of printers on your system? If so, you can use

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.");

Note: I got this off of this thread on another forum, but I'm using a similar method to print to a specific printer when I don't know if it's the default printer.

I just noticed that you're probably using a web site. The above method is only usable if you were to use it server side in order to select the default printer.

I don't know if this can be done client side but I doubt it. It would give your website access to your client's computer which is a huge security breach. I think you'd have to show a print dialog in which users can select the printer to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM