Printing in C#


This application will send raw data to a networked printer.

The printer does not need to be installed on your pc but you obviously need access rights to the network.(ask your friendly administrator)

The printer I used is a Zebra gk420t connected via a network cable. The name of the printer is found under it i.e(ZBR3677984) and The IP isn't static.

Using socket printing you can relocate your printer anywhere on the network. Very handy. I have used VS2005 anc c#.

Create a windows project add a button code the following under it,  add a textbox to display a few interesting infos. 

Obviously you need to change the printer's name in the code.

To get all the info you need from your zebra printer open a browser then goto http://printername (i.e: http://ZBR3677984) you will need

A password to access the settings but default zebra password is usually 1234...........easy

Also my zplstring in the code is a bit of a mess you will need to study the zpl programmers guide to make sense of it all. Happy coding and Hope this helps.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace zpltest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                IPHostEntry IPHost = Dns.GetHostEntry("ZBR3677984");
                string[] aliases = IPHost.Aliases;
                IPAddress[] addr = IPHost.AddressList;
                this.textBox1.Text = IPHost.HostName;
                this.textBox1.Text = this.textBox1.Text + " " + addr[0];
                EndPoint ep = new IPEndPoint(addr[0], 9100);
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect(ep);
                NetworkStream ns = new NetworkStream(sock);
                String transferType = "^MTT";// Sets the type to thermal transfer
                String ZPLString = "^LH5,5" + transferType +
                "^BY2" + "^MNM" +
                "^FO50,30" + "^ADN,96,20^FD" + "blabla" + "^FS" +
                "^FO250,130" + "^BCN,70,N,N,N" + "^FD" +
                "BarCode1" + "^FS" +
                "^FO50,230" + "^ADN,96,20^FD" + " BarCode2" + "^FS";
                ZPLString = "^XA" + ZPLString + "^XZ";                              
                byte[] toSend = Encoding.ASCII.GetBytes(ZPLString);
                ns.Write(toSend, 0, toSend.Length);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all