Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Generating and Printing Barcodes on Zebra Printer

2.23/5 (5 votes)
4 Jan 2011CPOL 82.9K  
Access zebra printer from ASP.NET

It is really difficult to find something on internet to print from ASP.NET form yes what you will find is JavaScript to acheive the same. Also its easy to do from windows application.


Issue: When you use JavaScript, you have to save the image (barcode) on aspx page and then fire the javascript print command. It works well with lazer printer but has issue with Zebra printer as image gets generated but there, we are not able to scan it. So Barcode generated is no use if you are using Zebra printer


It allows you to print barcode and also access zebra printer from ASP.NET.



  1. Install font which is free on .net "3 of 9 Barcode"
  2. Create an ASP.NET project
  3. Add this code

Both aspx and cs file code is attached


Any one who wants to print barcodes using zebra printers can use it, using ASP.NET (webforms). Any one using JavaScript for printing and try to print on zebra printer has issues. AS barcode generated does not get scanned. So using the code the barcode generated is corrected printed by zebra printer.


C#
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.Text;
using System.Drawing;
using System.IO;
using System.ComponentModel;
namespace RahulMehtaGhatkopar100
{
    public partial class WebForm4 : System.Web.UI.Page
    { 
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public void Submit_Click(object sender, EventArgs e)
        {
            try
            {
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                // Set the printer name. 
                //pd.PrinterSettings.PrinterName = "\\NS5\hpoffice
                //pd.PrinterSettings.PrinterName = "Zebra New GK420t"               
                pd.Print();
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.ToString());
            }
        }
        void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            Font printFont = new Font("3 of 9 Barcode", 17);
            Font printFont1 = new Font("Times New Roman", 9, FontStyle.Bold);

            SolidBrush br = new SolidBrush(Color.Black);

            ev.Graphics.DrawString("*AAAAAAFFF*", printFont, br, 10, 65);
            ev.Graphics.DrawString("*AAAAAAFFF*", printFont1, br, 10, 85);  
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)