C# tutorial: barcodes

Generating barcodes

In iTextSharp, a barcode is typically added to a PDF document as an Image object. iTextSharp provides you convenient classes to support different types of barcodes as shown in the table below.

Class Type of Barcode Description
Barcode128 plain code 128, raw code 128, UCC/EAN-128 This  is an alpha-numeric barcode. It can encode numbers, lower and upper case letters, standard ASCII symbols and control codes.
Barcode39 code 39 and code 39 extended This  is an alpha-numeric barcode widely used- in non-retail environments. It can encode numbers, upper case letters, and symbols.
BarcodeCodabar code codabar Codabar is used to encode numbers. Howerver, The start/stop (A, B, C, D, E, *, N, or T) characters must be used in matching pairs and may not appear elsewhere in the barcode.
BarcodeEAN EAN13, EAN8, UPCA, UPCE, supplemental 2 and 5 EAN stands for European Article Number code; UPC for Universal Product Code. Each type represents a number with a
different number of digits.
BarcodeEANSUPP supplemental 2 and 5 EAN-13, UPC-A, EAN-8, and UPC-E allow for a supplemental
two- or five-digit number to be appended to the main barcode.
BarcodeInter25 Interleaved 2 of 5 This is a numeric-only barcode that must consist of an even number of digits.
BarcodePDF417 2D barcode PDF417 This two-dimensional (2D) barcode is able to encode a large amount of data.
BarcodeQRCode QRCode This is a 2D barcode created for Quick Response. These
codes can be read by mobile phones with their camera in the context of mobile tagging.

In the image below, there are five types of barcodes are created. The first barcode is in EAN-13 format. It contains an ISBN of a book. The second barcode is an EANSUPP bar code. It combines the first barcode and a 5-digit supplemental barcode. The third barcode is in PDF417 format. It contains the text "iTextSharp". The rest barcodes are created in Code39 and Code128 formats.

//create a document object
var doc = new Document();
//get the current directory
string path ="d:";
//get PdfWriter object
PdfWriter writer= PdfWriter.GetInstance (doc, new FileStream(path + "/pdfdoc.pdf",  FileMode.Create));
//open the document for writing
//get PdfContentByte object
PdfContentByte content = writer.DirectContent;
//create EAN13 barcode
BarcodeEAN barean = new BarcodeEAN();
barean.CodeType = Barcode.EAN13;
barean.Code = "9781935182610";
//create SUPPLEMENTAL-5 barcode
BarcodeEAN barsup = new BarcodeEAN();
barsup.CodeType = Barcode.SUPP5;
barsup.Code = "55999";
barsup.Baseline = -2;
//create a EANSUPP barcode
BarcodeEANSUPP sup =  new BarcodeEANSUPP(barean, barsup);
Image imgeansub =  barean.CreateImageWithBarcode (content,BaseColor.GREEN,BaseColor.CYAN);
//create PDF417 barcode
BarcodePDF417 barpdf417 = new BarcodePDF417();
barpdf417.Text = ASCIIEncoding.ASCII.GetBytes("iTextSharp");
Image imgpdf417 = barpdf417.GetImage();
//create code39 barcode
Barcode39 bar39 = new Barcode39();
bar39.Code = "12345ABCDE";
Image img39 =  bar39.CreateImageWithBarcode(content, null, null);
//create code128 barcode
Barcode128 bar128 = new Barcode128();
bar128.Code = "12345ABCDE&^_@";
Image img128 =  bar128.CreateImageWithBarcode(content, null, null);
//view the result pdf file
System.Diagnostics.Process.Start(path + "/pdfdoc.pdf");

pdf barcode


LENG Sovandara comment

 LENG Sovandara

Can I generate the Barcode with text below and save it as image file by iTextsharp?

barbard comment


Thanks for your sharing.


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.