The Sharpzebra library consists of a number of commands and functions that allow you to compose a label, and send it to a ZPL/EPL2 compatible printer.

Code to create a sample label would look like this:

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, ZebraFont.STANDARD_NORMAL, 15, "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<byte>.

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, ZebraFont.STANDARD_NORMAL, 15, "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!

Last edited Feb 9, 2015 at 10:23 PM by rkone, version 3

Comments

No comments yet.