Printing Bar Codes
Sometimes it is necessary to print a bar code on a document or label. There are a lot of formats and encodings, in one or two dimensions, depending on the amount of information to be read by the terminal. In this article I will show three of the most common formats for one-dimensional codes and provide code in CSharp to encode them.
In this link you can download the source code for the BarCodes project, written with Visual Studio 2013. You will also find three fonts of free distribution to print the barcodes.
Code 39
The code 39, or code 3 of 9, system is one of the simplest, and can be written directly into a document with no more than to add the * character at the beginning and end of the string with the code. This is the barcode for the string 812345678901:
This type of code allows use alphanumeric characters and some symbols, it is easy to see which characters are valid simply by typing the string to encode in Word, having selected the bar code font.
With the sample program, you can encode a string to print it using code 39 with the CBEncoder class, by calling the static method CodifyCode39.
Code 128
The code 128 code also supports alphanumeric characters. The advantage over the code 39 is that it is more compact, since the data are encoded and the resulting string is shorter than the input. In addition, a checksum of the data is performed, which prevents incorrect readings. It is more convenient when the text string to encode is long, because the code 39 may generate too long barcodes. This is the result for the string in the previous example:
To encode a string, use the CodifyCode128 function, in the CBEncoder class.
EAN 13
The EAN 13 code is one of the best known. You can see it in almost all commercial products. It is a code that only allows alphanumeric characters. The string must be exactly 12 characters, but can be padded with 0 at the beginning if the code has fewer numbers. Another digit is added, calculated by a checksum, which prevents read errors. This is the aspect with the code that we are using as an example:
To encode a string to use this code, call the CodifyEAN13 function of the CBEncoder class.