How to print a Barcode to a shared / network Barcode Printer using .Net (System.Drawing.Printing.PrintDocument)

Hello All!

Today I’ll talk about how you can print a Barcode or a generic text to a Printer or a Barcode Label Printer.

This blog post will cover the following topics. You can choose your familiarity and proceed to the topic you wish to know.

  1. How to install a Barcode Printer
  2. How to share the Printer
  3. Printing Barcode to a Network Printer

You don’t need to spend on getting a Print Server or a Network Card enabled Barcode Label Printer (which few companies would try selling you) to bring the Barcode Label Printer into the Network. 

Follow these steps to make it all work! Ensure that you have connected a printer to your computer and you are able to run a Test Print out of it. You can name the printer whatever name you wish to name it as. This is the most essential part to get it all working.

If you do not know how to install a Barcode Label Printer in your workstation, please follow the below steps: ( I am installing the Barcode Label Printer with a Generic Text Driver)

STEPS TO INSTALL A BARCODE PRINTER

  1. Go to ControlPanel in your computer and click ‘Add a printer’

Addprinter.png

2. In the Add Printer Modal window, click ‘The printer that I want isn’t listed’ link. It will open up another modal window allowing you to choose the mode you want to add the printer.

addprinter2.png

 

3. In this window, choose ‘Add a local printer or network printer with manual settings’ option and click Next

addprinter3.png

4. In the next window, choose the port you have your printer connected. Mine is connected to USB port, so I am selected USB001 port.

TIP: If you see multiple USB ports in the list (which is normal), try to change the ports in the list and see if you are able to run a test print through that. I had faced similar problems in the past.

addprinter4.png

5. I always print barcodes using the Generic Text drivers (if you wish you change, you can according to your need), so in the below screenshot, I am selecting the Manufacturer as Generic and the Printers as Generic / Text Only. Click Next.

addprinter5.png

6. Select ‘Use the driver that is currently installed (recommended)’ Click Next

addprinter6.png

7. Name the printer as BAR or whatever name you wish to name the printer as and click Next.

addprinter7.png

8. In the below modal popup, if you wish to share the printer, click appropriately and give a proper Share Name. or click Do no share this printer and click Next.

addprinter8.png

9. You will receive a successful confirmation of your new printer being added into your printer list. If you wish to make it a default printer, you can check appropriately and click Finish.

addprinter9.png

HOW TO SHARE THE PRINTER

If you have selected to share the printer in the step 8 (above), then your printer is already shared and ready to use.

If not, then please follow the below steps to share the printer.

1. In the printers list, right click on the Barcode Label printer and click Printer Properties menu item.

Sharing1.png

2. This will open up the printers properties popup. Go to the Sharing tab and check the Share this printer checkbox and give it a share name. I usually provide the same name as the printer name to avoid any confusion. And click Ok.

Sharing2.png

That is it. Your printer is shared across the network.

Like I said, You don’t need to spend on getting a Print Server or a Network Card enabled Barcode Label Printer to bring the Barcode Label Printer into the Network. 

PRINTING BARCODE THROUGH THE NETWORK PRINTER

Firstly, you will have to list down the installed printers in the machine that the program is executed. Below is the code, that lists down the installed printers and shows it in a ListBox.

For Each strPrinterName As String In PrinterSettings.InstalledPrinters
ListBox_PrinterList.Items.Add(strPrinterName) ‘ Adding the installed printers.
Next

‘Find the Default printer
Dim PD As New PrintDocument
ListBox_PrinterList.SelectedItem = PD.DefaultPageSettings.PrinterSettings.PrinterName

Next, add the code to print to the printer

Public Sub prt(ByVal text As String, ByVal asPrinterName As String)
TextToBePrinted = text
Dim prn As New Printing.PrintDocument
Using (prn)
prn.PrinterSettings.PrinterName = asPrinterName
AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
prn.Print()
RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
End Using
End Sub

Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
Dim myFont As New Font(“Microsoft San Serif”, 10)
args.Graphics.DrawString(TextToBePrinted, New Font(myFont, FontStyle.Regular), Brushes.Black, 0, 0)
End Sub

In the above code, the asPrinterName argument holds the name of the selected printer. Argument text holds the text/barcode to be printed.

You should be testing it by now!

One Important thing to note:

Barcode Label Printers have their own programming language to send the barcode text to the Barcode Label Printer to print. I am using Zebra printers which enables us to use ZPL or EPL languages to send the barcode to the Zebra Barcode Label Printers.

-Cheers!

Rohant K.