Skip to content
rkone edited this page Apr 5, 2021 · 3 revisions

A quick-start guide to using the APIs

  1. Install a Zebra Printer so it's accessible on your Print Queue (we're going to assume the name of your printer is ZDesigner S4M-203dpi ZPL).
  2. Add a reference to the SharpZebra libraries to your project
  3. Write the code below to print a label into a class
  4. Run the code:
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "ZDesigner S4M-203dpi ZPL";
ps.Width = 203 * 4;
ps.Length = 203 * 6;
ps.Darkness = 30;

List<byte> page = new List<byte>();
page.AddRange(ZPLCommands.ClearPrinter(ps));
page.AddRange(ZPLCommands.TextWrite(10, 150, ElementDrawRotation.NO_ROTATION, ZPLFont.STANDARD_SCALABLE, 15, 10, "Hello World!"));
page.AddRange(ZPLCommands.PrintBuffer(1));
new SpoolPrinter(ps).Print(page.ToArray());

Broken down:

PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "ZDesigner S4M-203dpi ZPL";
ps.Width = 203 * 4;
ps.Length = 203 * 6;
ps.Darkness = 30;

PrinterSettings stores information about your printer. It also stores how to communicate to the printer. For printers installed into windows, it is sufficient to set PrinterName to the name of the printer. Width and height are set in dots, so here we are multiplying the 203 dpi times the label size (4x6"). A good darkness value varies depending on the media used, 30 is a good place to start.

List<byte> page = new List<byte>();
page.AddRange(ZPLCommands.ClearPrinter(ps));

An easy way to build labels (the "page" variable in this case) is to store them as List.

The sample printer in this example supports ZPL, so the (more robust) ZPLCommands class is used to generate the label. The first item to add to you your label is always the ClearPrinter command. This adds commands to initialize your printer and sets any configuration you've changed through the PrinterSettings variable.

page.AddRange(ZPLCommands.TextWrite(10, 150, ElementDrawRotation.NO_ROTATION, ZPLFont.STANDARD_SCALABLE, 15, 10, "Hello World!"));

Now ZPLCommands are used to add content to the label. This is telling the printer to add "Hello World!" in the standard printer font, 15 pixels high, 10 pixels from the left of the label and 150 pixels down the label.

page.AddRange(ZPLCommands.PrintBuffer(1)); 

When finished creating the label, the final command is to tell the printer how many copies of the label we want it to print. PrintBuffer(1) will print 1 page.

new SpoolPrinter(ps).Print(page.ToArray()); 

Finally, we submit all our information to the SpoolPrinter class. This nicely handles all the complexities of connecting directly to the printer and submits our print job (page.ToArray()). One label is printed!

Clone this wiki locally