ZPL How to Center Barcode (Code 128)

darkdog picture darkdog · Mar 18, 2014 · Viewed 18.6k times · Source

i'd like to know how to center barcode code 128. on the picture you should see that it is right now left justified. Label

my zpl:

^XA
^LH10,10
^FO0,0^XGE:SWESE001.GRF^FS
^FO440,0^XGE:SWESE000.GRF^FS
^FO0,70^FB550,50,0,C,0^AQN,25,30^FDSpraynozzle 50mm^FS
^FO0,130^BY2^BCN,30,Y,Y,N,N^FDS/N:941001-0114-0001^FS
^FO180,170^AQN,23,20^FDwww.swepro.com^FS
^XZ

Answer

Nicolas Garfinkiel picture Nicolas Garfinkiel · May 13, 2015

I'm posting my solution in case someone is looking for it. As E_S mentions, in order to center a barcode in a label you have to calculate it by code following these steps:

  • Check your narrow bar width, in your case 2 (^BY2)
  • Find out your label total width in dots. For this you have to know what is your printer's resolution (eg: 8 dots / mm). so if you have a 80 mm wide label, 80 * 8 = 640 dots
  • Count each character in your barcode, including invocation codes and check digit as specified below. For information on invocation codes see: https://www.zebra.com/content/dam/zebra/manuals/en-us/software/zpl-zbi2-pm-en.pdf (Page 95)
  • Note that invocation codes (">:", ">5", etc.) count as one character, and that characters in mode C are stored in pairs. For more information on mode C, refer to http://en.wikipedia.org/wiki/Code_128
  • If your barcode is >:S/N:941001-0114-0001 you have to count [start code B] + [20 characters] + [check digit] = 22
  • If your barcode is >:S/N:>5941001>6->50114>6->50001 you have to count [start code B] + [4 characters for 'S/N:'] + [mode C invocation] + [3 characters for '941001'] + [mode B invocation] + [1 characters for '-'] + [mode C invocation] + [2 characters for '0114'] + [mode B invocation] + [1 characters for '-'] + [mode C invocation] + [2 characters for '0001'] + [check digit] = 20
  • Every character occupies 11 units mixing spaces and bars, with the exception of stop code that has 2 extra units (that is a total of 13)
  • Here comes the good stuff... The barcode width is: ((chars counted [22 or 20] * 11) + (stop char * 13)) * narrow bar width = 510 dots or 466 dots
  • Now all we have to do is round((label width - barcode width) / 2) and use that to position the barcode with ^FT

That's it! Hope it helps someone!