How to print barcode labels to the default thermal printer of the client machine in ASP.NET with ThermalLabel SDK

In this short article we’ll show you how to print to the default thermal printer of the client machine (a.k.a. client-side printing) without showing any print dialog to the end-user in an ASP.NET scenario by referring to the same scenario stated in our guide about ASP.NET Client-side cross-browser barcode thermal label printing with ThermalLabel SDK

Please read the above guide to learn more about how our client-side barcode thermal printing approach works before trying the following code.

To print to the default printer of the user (at the client machine) and based our code on the same wrote in our ASP.NET Client-side cross-browser barcode thermal label printing with ThermalLabel SDK guide, you should:

  • The first thing to do is to instruct to the user to set up in Windows OS the thermal printer as the default printer in the client machine. The following screenshot is from Windows 7 on which the user has set up the Zebra GK420tprinter as the default printer.
    Setting the default printer on Windows 7

    Note on the Windows Driver for the client printer.

    In this case the user installed the Zebra ZDesigner driver for the printer. However, in most cases the simple “Generic / Text Only” driver that comes with Windows will work just fine too.

  • The ThermalLabel SDK does support both Zebra ZPL and EPL printer languages i.e. on a given ThermalLabel object, you can instruct our product to generate ZPL or EPL commands and send them to the target printer. So the second important thing you need to knowis what kind of language the default client printer does support i.e. if it a ZPL or EPL printer. Some Zebra printer models (like the GK420t) do support both languages in which case you should not be worried about it (by default our SDK generates ZPL commands). However, others like the LP/TLP-2844 or TLP-3842 which only supports EPL should be taken into account.So you should get that info somehow from the client/user if this is a variable factor. In some enterprise scenarios, thing is sometimes simplified as the whole environment could be using the same printer model and then, the printer command language is already known. So please consider this topic when you need to “print to the default client printer”.
  • In addition to know the client printer’s language (ZPL or EPL) you also need to know the printing resolution (DPI) supported by the client printer. Nowadays, Zebra thermal printers come with 203, 300 and 600 dpi. The same method for getting if the client printer is ZPL or EPL capable may be applied for getting the DPI support.
  • So based on the above considerations, we could change the code from our main guide to the following. We know (somehow) that the printer is ZPL-capable and that it has 203 dpi of printing resolution. With that info we can print the label to the default client printer without showing any dialog to the user by modifying the code in the Page_Init event handler of our ASP.NET page to the following:

    For VB code:

    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
    
            'is this a request for a WebPrintJob handler?
            If Not Request("webPrintJob") Is Nothing Then
                'Create a WebPrintJob obj
                Dim webPj As New WebPrintJob()
    
                'set a ThermalLabel obj
                webPj.ThermalLabel = GenerateBasicThermalLabel()
    
                'Print to the defaul client printer
                  webPj.ShowPrintDialog = False
                  webPj.PrinterSettings = New PrinterSettings()
                  webPj.PrinterSettings.Communication.CommunicationType = CommunicationType.PrinterDriver
                  webPj.PrinterSettings.ProgrammingLanguage = ProgrammingLanguage.ZPL
                  webPj.PrinterSettings.Dpi = 203
                  webPj.PrinterSettings.UseDefaultPrinter = True
    
                'Serialize WebPrintJob and send it back to the client
                'so it can be processed by the TLClientPrint utility
                Response.ContentType = "text/plain"
                Response.Write(webPj.ToString())
                Response.Flush()
                Response.End()
            End If
    
    End Sub
    
    

    For C# code:

    protected void Page_Init(object sender, EventArgs e)
    {
        //is this a request for a WebPrintJob handler?
        if (Request["webPrintJob"] != null)
        {
            //Create a WebPrintJob obj
            WebPrintJob webPj = new WebPrintJob();
    
            //set a ThermalLabel obj
            webPj.ThermalLabel = GenerateBasicThermalLabel();
    
            //Print to the defaul client printer
            webPj.ShowPrintDialog = false;
            webPj.PrinterSettings = new PrinterSettings();
            webPj.PrinterSettings.Communication.CommunicationType = CommunicationType.PrinterDriver;
            webPj.PrinterSettings.ProgrammingLanguage = ProgrammingLanguage.ZPL;
            webPj.PrinterSettings.Dpi = 203;
            webPj.PrinterSettings.UseDefaultPrinter = true;
    
            //Serialize WebPrintJob and send it back to the client
            //so it can be processed by the TLClientPrint utility
            Response.ContentType = "text/plain";
            Response.Write(webPj.ToString());
            Response.Flush();
            Response.End();
        }
    }
    
    

    With that change of the Page_Init event handler, when the user clicks on the “Print Basic Label…” button of the page, the TLClientPrint utility will run and based on the WebPrintJob settings we did, the label should automatically be printed to the client printer without showing any print dialog at all.

If you need further assistance do not hesitate to contact our tech support.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: