In this article, we’ll see how to generate barcodes with Bytescout Barcode SDK. Generating different types of barcodes from scratch can be challenging for developers, considering one has to follow all specifications and rules for each barcode type. Bytescout Barcode SDK takes heavy lifting from the developer and provides easy to use SDKs for it.
In order to get an idea of how Bytescout Barcode SDK works, we’ll be covering the following programs.
These programs will be in C#, however, Bytescout Barcode SDK supports different languages too like C++, VBScript, Delphi, Javascript, etc. Bytescout also provides restful web API for these SDKs, so it can be used in all development environments.
You also need to install Bytescout SDKs in your machine in order to take reference of Bytescout SDK and reference it to execute these programs. This is an installation package to install BarCode SDK.
Let’s get started.
We’ll be using console application in visual studio, which can be created from menu File -> New -> Project, then choose “Console Application” under the “Windows” tab. If you are using the latest IDE at the time of writing this article which is Visual Studio 2019, then select “Console App” from the start page.
Then we’ll be adding a reference for “Bytescout BarCode SDK”.
In this program we’ll be going to generate Code39 barcode as follows:
using Bytescout.BarCode;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
// Create new barcode
Barcode barcode = new Barcode();
barcode.RegistrationName = "demo";
barcode.RegistrationKey = "demo";
// Set symbology
barcode.Symbology = SymbologyType.Code39;
//barcode.Symbology = SymbologyType.QRCode;
// Set value
barcode.Value = "Sample";
// Save barcode to image
barcode.SaveImage("result.png");
// Show image in default image viewer
Process.Start("result.png");
}
}
}
The output is below.
As you have noticed the code is very simple and straightforward. We can divide this process into the following steps:
Here, we are generating an object of Barcode class, and providing registration key and name to it. In this demo we’re using demo keys (which has watermarks), however, in production one has to use original keys received upon purchase of Bytescout SDKs.
Barcode barcode = new Barcode();
barcode.RegistrationName = "demo";
barcode.RegistrationKey = "demo";
Specify Symbology depending on which kind of barcode we need to create. Here we’re using Code39 type, however, as shown in the comment we can use different types as per our requirement like QRCode type.
// Set symbology
barcode.Symbology = SymbologyType.Code39;
//barcode.Symbology = SymbologyType.QRCode;
We’re using the “Value” property to specify barcode input value.
barcode.Value = "Sample";
Here we’re saving barcode to an image, however, there is different output option available like saving to stream. Or we can add this barcode to existing PDF or Image which we’ll see in the next program.
// Save barcode to image
barcode.SaveImage("result.png");
Bytescout BarCode SDK covers almost all kinds of barcodes to generate. The following are some of the supported Barcode Types with their usage.
Barcode Type | Description |
Code128 | Code 128 barcode (Also known as Code-128). It is a very effective, high-density symbology which permits the encoding of alphanumeric (subject to alphabet selection) data. Code 128 is a very dense code, used extensively worldwide. |
Code39 | Code 39 barcode (Also known as USD-3, Code 3 of 9, LOGMARS and in extended mode also known as Code39Extended, Code 39 Full ASCII mode). Code 39 symbology allows all ASCII symbols to be encoded in extended mode or symbols from this string “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%” in standard mode. This symbology used for example by the U.S. Government and military, required for DoD applications. |
UPCA | UPC-A barcode (Also known as UPCA). Used with consumer products in U.S. UPC-A symbology allows only numeric values to be encoded. |
EAN8 | EAN-8 barcode (GTIN-8). This symbology is a short version of EAN-13 that is intended to be used on the packaging which would be otherwise too small to use one of the other versions. Used with consumer products internationally. EAN-8 symbology allows only numeric values to be encoded. |
ISBN | ISBN Number encoded as an EAN-13 barcode. |
Codabar | Codabar barcode (Also known as Ames Code, USD-4, NW-7, Code 2 of 7). Codabar symbology allows only symbols from this string ‘0123456789-$:/.+’ to be encoded. This symbology used for example in libraries and blood banks. |
I2of5 | Interleaved 2 of 5 barcodes (Also known as Code 2 of 5 Interleaved). Interleaved 2 of 5 symbology allows only numeric values to be encoded. This symbology is used primarily in the distribution and warehouse industry. |
Code93 | Code 93 barcode (Also known as USS-93, Code93, and in extended mode also known as Code93Extended, Code 93 Full ASCII mode). Code 93 symbology allows all ASCII symbols to be encoded in extended mode or symbols from this string “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%” in standard mode. This symbology was designed to complement and improve upon Code 39 symbology. Code 93 produces denser code than that of Code 39. |
EAN13 | An EAN-13 barcode (GTIN-13). Used with consumer products internationally. EAN-13 symbology allows only numeric values to be encoded. |
PDF417 | PDF417 symbology. This symbology is heavily used in the parcel industry. The PDF417 symbology can encode a vast amount of data into a small space. This symbology allows a maximum data size of 1850 text characters or 2710 digits. |
DataMatrix | Data Matrix symbology. The most popular application for Data Matrix is marking small items. The Data Matrix can encode text and raw data. Usual data size is from a few bytes up to 2 kilobytes. |
QRCode | QR Code symbology. QR Code initially used for tracking parts in vehicle manufacturing, but now QR Codes used in a much broader context, including both commercial tracking applications and convenience-oriented applications aimed at mobile phone users (known as mobile tagging). |
Aztec | Aztec Code symbology. An Aztec code barcode is used by Deutsche Bahn, Trenitalia, and by Swiss Federal Railways for tickets sold online and printed out by customers. The Aztec Code has been selected by the airline industry (IATA’s BCBP standard) for the electronic boarding passes. |
EAN128 | EAN128 symbology (Also known as EAN-128, EAN-14, Shipping Container Code, UCC-14, DUN-14 (Distribution Unit Number), SSC-14, GS1-128, UCC-128, UCC/EAN-128. This symbology was developed to provide a worldwide format and standard for exchanging common data between companies. |
EAN2 | The EAN-2 (Also known as EAN/2 and EAN 2) is a supplement to the EAN-13 and UPC-A barcodes. It is often used in magazines and periodicals to indicate an issue number. |
EAN5 | The EAN-5 (Also known as EAN/5 and EAN 5) is a supplement to EAN-13 and UPC-A barcodes. It is often used to give a suggestion for the price of the book. |
EAN14 | The EAN14 symbology is used for traded goods. |
MacroPDF417 | The Macro version of PDF417 Symbology. |
MicroPDF417 | The Micro version of PDF417 Symbology. |
GS1_DataMatrix | GS1 DataMatrix is a 2D (two-dimensional) barcode that holds large amounts of data in a relatively small space. These barcodes are used primarily in aerospace, pharmaceuticals, medical device manufacturing, and by the U.S. Department of Defense to add visibility to the value chain. GS1 DataMatrix can be used for parts that need to be tracked in the manufacturing process because the barcode allows users to encode a variety of information related to the product, such as date or lot number. They are not intended to be used on items that pass through retail point-of-sale (POS). |
IntelligentMail | The Intelligent Mail Barcode symbology. This symbology is used in the USPS mail stream. It is also known as the USPS OneCode Solution or USPS 4-State Customer Barcode (abbreviated 4CB, 4-CB, or USPS4CB) |
GS1_DataBar_Omnidirectional | The GS1 DataBar Omnidirectional symbology. |
GS1_DataBar_Truncated | The GS1 DataBar Truncated symbology. |
GS1_DataBar_Stacked | The GS1 DataBar Stacked symbology. |
GS1_DataBar_Limited | The GS1 DataBar Limited symbology. |
GS1_DataBar_Expanded | The GS1 DataBar Expanded symbology. |
MaxiCode | The MaxiCode symbology. |
Plessey | The Plessey Code symbology. This symbology is used primarily in libraries and for retail grocery shelf marking. |
MSI | The MSI (also known as Modified Plessey) symbology. This symbology is used primarily for inventory control, marking storage containers and shelves in warehouse environments. |
ITF14 | The ITF-14 (GTIN-14, UCC-14) symbology. ITF-14 is the GS1 implementation of an Interleaved 2 of 5 bar code to encode a Global Trade Item Number. ITF-14 symbols are generally used on a packaging step of products. The ITF-14 always encodes 14 digits. |
GTIN12 | The GTIN-12 (12-digit UPC-A) symbology. GTIN-12 is a 12-digit number used primarily in North America. |
GTIN8 | The GTIN-8 (EAN-8, UCC-8): this is an 8-digit number used predominantly outside of North America. |
GTIN13 | The GTIN-13 (EAN-13, UCC-13): this is a 13-digit number used predominantly outside of North America. |
GS1_QRCode | GS1 QRCode |
PharmaCode | Pharma Code |
The full list of all supported barcode types can be viewed at the following URL.
https://cdn.bytescout.com/help/BytescoutBarCodeSDK/html/79288258-f847-7548-2a5d-de6d03c5f335.htm
This program will demonstrate how to generate a barcode and write to an existing image or PDF file. This can be very helpful in scenarios where we want to add barcodes to existing images or PDF documents.
The input file is as follows:
The program is as below.
class Program
{
static void Main()
{
// Create new barcode and register it.
Barcode barcode = new Barcode();
barcode.RegistrationName = "demo";
barcode.RegistrationKey = "demo";
// Set symbology
barcode.Symbology = SymbologyType.Code39;
// Set value
barcode.Value = "Sample";
// Place barcode at top-right corner of every document page
barcode.DrawToPDF("wikipedia.pdf", -1, 500, 50, "result.pdf");
// Place barcode at bottom-right corner of the image
// barcode.DrawToImage("wikipedia.png", -1, 400, 900, "result.png");
// Open output file in default PDF viewer
System.Diagnostics.Process.Start("result.pdf");
// System.Diagnostics.Process.Start("result.png");
}
}
It generates the output:
If we analyze this program with the previous program, the only difference is the way we generating output. In this program, we are putting a generated barcode to all PDF pages at a certain location ( top-right corner).
// Place barcode at top-right corner of every document page
barcode.DrawToPDF("wikipedia.pdf", -1, 500, 50, "result.pdf");
Method “DrawToPDF” has the following arguments.
Arg-1 (inputFile/input stream) | The first argument represents the input file to process. An overloaded version of this method, we can also pass the input stream also. |
Arg-2 (pageIndex) | The second argument expects pageIndex on which barcode should be printed, If provided -1 then it considers all pages like in this case. |
Arg-3,4 (x & y coordinates) | Third and Fourth arguments represent x and y coordinates respectively, to identify where barcode printing should take place. |
Arg-5 (outputFile/output stream) | The fifth argument represents the output file location or stream. |
In case we want to render a barcode on an image, we can use the method “DrawToImage” as shown below.
// Place barcode at bottom-right corner of the image
barcode.DrawToImage("wikipedia.png", -1, 400, 900, "result.png");
Arguments of “DrawToImage” are the same as that of the pdf version. One might wonder why we need pageIndex argument for this method which deals in images. Answer to that is, it’s for when we have TIFF file as input; as it can have multiple pages; clever right?.
That’s all guys. I hope you enjoyed this article. If you want to explore more please visit these documents.
You can also try an online barcode scanner for easier daily tasks.
Happy Coding 🙂