rkone / sharpzebra

A .net library that simplifies printing to Zebra printers in their native EPL2/ZPL languages without needing to know EPL2 or ZPL.
MIT License
89 stars 34 forks source link

Print two-by-one two column labels #17

Closed seevali closed 3 years ago

seevali commented 3 years ago

An awesome work!

How to print two column labels (2x1") on a 4" paper?

rkone commented 3 years ago

You can use the ZPL.TextAlign method to create a column for text. Unfortunately ZPL has no method to overflow from a first to a 2nd column, and short of using your premade font there's no way to calculate where such a break would need to be.

seevali commented 3 years ago

For anyone who is interested in a solution for printing 2x1" labels on a 4" wide paper;

Barcode barCode = new Barcode()
{
    Type = BarcodeType.CODE128_AUTO,
};

PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "<<Printer-Name>>";
ps.Width = 203 * 4; //Full width of the physical paper
ps.Length = 203 * 1;
ps.Darkness = 30;

for (int i = 0; i < labels.Count; i = i + 2)
{
    List<byte> page = new List<byte>();
    page.AddRange(ZPLCommands.ClearPrinter(printerSettings));

    var items = labels.Skip(i).Take(2).ToList();
    page.AddRange(ZPLCommands.BarCodeWrite(0, 50, 100, ElementDrawRotation.NO_ROTATION, barCode, true, items[0].BarcodeValue));

    if (items.Count == 2)
    {
        //left:417 to push the second label to the right side
        page.AddRange(ZPLCommands.BarCodeWrite(417, 50, 100, ElementDrawRotation.NO_ROTATION, barCode, true, items[1].BarcodeValue));
    }
    page.AddRange(ZPLCommands.PrintBuffer(1));
    new SpoolPrinter(printerSettings).Print(page.ToArray());
}
rkone commented 3 years ago

I like to have a label function, it helps simplify editing the label design.

    public static byte[] Barcode2Up(string serial1,string serial2,BarcodeType barcodeType)
    {
        var res = new List<byte>();
        var barcode = new Barcode { Type = barcodeType };
        res.AddRange(ZPLCommands.BarCodeWrite(208, 20, 50, ElementDrawRotation.NO_ROTATION, barcode, true, serial1));
         if (string.IsNullOrWhiteSpace(serial2))
            return res.ToArray();
        res.AddRange(ZPLCommands.BarCodeWrite(530, 20, 50, ElementDrawRotation.NO_ROTATION, barcode, true, serial2));
        return res.ToArray();
    }

...

                    var res = new List<byte>();
                    for (var i = 0; i < labels.Count; i += 2)
                    {
                        res.AddRange(ZPLCommands.ClearPrinter(LabelPrinter.Settings));
                        res.AddRange(i + 2 > labels.Count
                            ? Barcode2Up(labels[i].Serial, null, barcodeType)
                            : Barcode2Up(labels[i].Serial, labels[i + 1].Serial, barcodeType));                            
                        res.AddRange(ZPLCommands.PrintBuffer());
                    }

                    LabelPrinter.Print(res.ToArray());