Persits Software, Inc. Web Site
 Navigator:  Home |  Manual |  Chapter 13: Barcodes
Chapter 14: Document Stitching and Other Features Chapter 12: Form Fill-in
  Chapter 13: Barcodes
13.1 Regular Barcodes
13.2 Two-Dimensional Barcodes

13.1 Regular Barcodes

AspPDF.NET contains the ability to generate printable, scannable barcodes. Various 1D (linear) barcodes as well as two 2D barcode are supported.

13.1.1 DrawBarcode Method

Linear (one-dimensional) barcodes are provided by the DrawBarcode method of the PdfCanvas object. This method expects two parameters: a string indicating the data to encode as a barcode, and a PdfParam object or parameter string providing parameters.

The following code sample prints a UPC-A (Universal Product Code) barcode on a page.

C#
...
//Barcode type 1 is UPC-A
string strParams = "x=72; y=600; height=96; width=144; type=1";
string strData = "04310070524";

// Draw text on page
objPage.Canvas.DrawBarcode( strData, strParams );
...
VB.NET
...
'Barcode type 1 is UPC-A	
Dim strParams As string = "x=72; y=600; height=96; width=144; type=1"
Dim strData As string = "04310070524"

' Draw text on page
objPage.Canvas.DrawBarcode( strData, strParams )
...

Click the links below to run this code sample:

http://localhost/asppdf.net/manual_13/13_barcode.cs.aspx
http://localhost/asppdf.net/manual_13/13_barcode.vb.aspx  Make sure AspPDF.NET is installed on your local machine and IIS is running for these links to work.

This will produce the following:

Note that AspPDF.NET automatically calculated and added the UPC-A check digit (the 6.)

The DrawBarcode method will validate the data passed to it and throw an error exception if it is invalid for the specified type of barcode.

AspPDF.NET also includes a sample of an HTML form page into which a user can enter data for a barcode. This code is not repeated here due to length. Click the links below to run this sample:

http://localhost/asppdf.net/manual_13/13_barcode_form.cs.aspx
http://localhost/asppdf.net/manual_13/13_barcode_form.vb.aspx  Make sure AspPDF.NET is installed on your local machine and IIS is running for these links to work.

13.1.2 Required Barcode Parameters

Type - What type of barcode to draw. See below for a list of supported types.

X, Y - X and Y coordinates of the lower-left corner of the barcode.

Width, Height - Width and height of the barcode.

The X, Y, Width, and Height parameters only specify the size of the actual rectangular bars. Text associated with barcodes may extend slightly outside the specified area.

13.1.3 Optional Barcode Parameters

Color - Specifies the color of the bars and text. This can be a named color constant like "red" or a hex RGB value. Defaults to black.

tc, tm, ty, tk - Specify the CMYK color components of the bars and text. The values must be between 0 and 1. If these parameters are specified, Color is ignored. These parameters were introduced by version 2.0.0.13729.

BgColor - Specifies the color of the spaces between the bars. If not specified, the spaces will be transparent. This can be useful to make the spaces in the barcode white if the page background is non-white. Note that very few scanners can scan barcodes in colors other than standard black and white.

FontSize - Specifies the size of the font used. This is not used for certain barcode types like UPC which prescribe all font sizes. This is a maximum limit on font size. AspPDF will shrink the font as necessary to make the text fit within the width of a particular barcode type.

BarWeight - Certain barcode types do not prescribe a constant width ratio between "thin" and "wide" bars, but allow this ratio to be specified. Using this parameter may improve the scannability of these barcode types with various scanners. Defaults to 2, meaning "wide" bars are twice as wide as "thin" bars.

DrawText - Some barcode types (such as UPC) usually include printed human-readable text. Others (such as Industrial 2/5) do not. This parameter allows you to override the default behavior. Can be set to True or False.

AddCheck - A few barcode types allow optional calculation and use of a check digit. Boolean parameter.

Compress - Only used by Code 128, see below. Boolean parameter.

Angle - Specifies an angle of counter-clockwise rotation (in degrees) of the barcode around its lower-left corner.

13.1.4 Supported Linear Barcode Types

(Some images shortened vertically for space)

1. UPC-A - The most common type of barcode. Used in retail applications. Requires 11 or 12 numeric digits; if 11 are given, AspPDF calculates and adds the 12th digit which is a check digit.
2. UPC-E - A shortened form of UPC-A. Requires 8 numeric digits.
3. EAN-13 - A superset of UPC-A, with one additional digit. Requires 12 or 13 numeric digits; the 13th is a check digit in the same manner as UPC-A.
4. EAN-8 - A shortened form of EAN-13. Requires 7 or 8 numeric digits; the 8th is a check digit.
5. UPC-A with supplemental - A UPC-A code, plus either a 2-digit or 5-digit supplemental code. Requires 14 numeric digits for the 2-digit version and 17 digits for the 5-digit version. If the 12th character is a space, AspPDF will calculate a check digit for that space and encode it.
6. UPC-E with supplemental - Similar to UPC-A with supplemental. Requires 10 or 13 numeric digits.
7. EAN-13 with supplemental - Similar to UPC-A with supplemental. Requires 15 or 17 numeric digits. Calculates a check digit if the 13th character is a space.
8. EAN-8 with supplemental - Similar to UPC-A with supplemental. Requires 10 or 13 numeric digits. Calculates a check digit if the 8th character is a space.
9. 2-digit supplemental - The 2-digit supplemental barcode can be drawn by itself without an accompanying main barcode.
10. 5-digit supplemental - See previous.
None of the UPC or EAN barcode types use the FontSize or BarWeight parameter. All other barcodes do use FontSize, and use BarWeight unless otherwise specified.
11. Industrial 2 of 5 - Can encode any quantity of numeric characters. AspPDF adds the start and stop bars.
12. Interleave 2 of 5 - Similar to Industrial 2 of 5 with a more compact encoding scheme.
13. Interleave 2 of 5 Special - Same as Interleave 2 of 5 but with different spacing. Does not use BarWeight.
14. DataLogic 2 of 5 - Similar to Industrial 2 of 5 but with a different compact encoding scheme.
15. Plessey - Can encode any quantity of numeric characters. AspPDF adds the start and stop bars. This image illustrates the use of the BarWeight parameter set to 3. Note that the wide bars are much thicker than the thin bars.
16. Codabar - Can encode any quantity of these characters:
0123456789-$:/.+
Requires a matched pair of start and stop characters which must be the letters A, B, C, or D. Does not use BarWeight.
17. Code 39 - A common encoding scheme for alphabetic text. Can encode numbers, uppercase letters, and these characters:
-. $/+%
AspPDF encodes lowercase characters as uppercase, and adds start and stop bars.
18. Code 11 - Can encode any quantity of numeric characters.
20. Code 39 Extended - Can encode the entire ASCII set (characters 0 to 127). Can optionally add a check digit with the AddCheck parameter.
21. Code 93 - Can encode the entire ASCII set (characters 0 to 127). Does not use BarWeight.
22. Code 128 - Can encode the entire ASCII set (characters 0 to 127). Does not use BarWeight. Must use a check digit via the AddCheck=true parameter. Automatically selects between Code-128 encoding schemes as necessary. Implements the Code-C encoding scheme to compactly encode pairs of digits; this is done if the optional parameter Compress is set to True.

NOTE: as of Version 3.1.0.30169, this barcode is replaced by a more readable Type=24 barcode described below.

23. Code 128 Raw - Provides access to advanced features of Code-128 encoding; AspPDF does not do any processing of the data passed to it, except for adding the stop bars and optionally adding the check digit. You must provide the start code character; in VB or ASP, this would be ChrW(135) for Code A, ChrW(136) for Code B, and so on. Consult a Code 128 reference for full information on specifications such as function and shift characters.
24. Code 128 Auto - An improved version of Code 128 (Type=22) described above. Can encode the entire ASCII set (characters 0 to 127). Does not use BarWeight. Ignores AddCheck and Compress parameters. Introduced in version 3.1.0.30169 to replace barcode Type=22. Uses an alternative encoding algorithm which provides better scannability than the old Type=22 barcode.
30. US Postal Code - Printed on mail by the US Postal Service. AspPDF calculates and adds a check digit. Does not use BarWeight.
31. UK/Canada Postal Code - Printed on mail by the UK and Canada mail services. Does not use BarWeight.
32. Intelligent Mail Code - Used by US Postal Service. Also known as the USPS OneCode Solution or USPS 4-State Customer Barcode. Requires 20 digits of tracking information followed by 0, 5, 9, or 11 digits of routing (zip code) information. The 2nd digit of the tracking code must be in the range 0 to 4. Introduced by AspPDF.NET 2.2. Does not use BarWeight.

The barcode functionality is demonstrated by Live Demo #8.

13.2 Two-Dimensional Barcodes

AspPDF.NET supports four popular 2-dimensional barcodes: PDF417, Data Matrix, QR Code and Aztec. (QR Code is supported as of Version 1.9.0.12551, and Aztec as of Version 3.4.0.33105.) 2D barcodes are drawn via the DrawBarcode2D method of the PdfCanvas object. This method has two required arguments: the text to be encoded, and an instance of the PdfParam object or parameter string. An overloaded version of DrawBarcode2D takes a byte array as the first argument in case binary information needs to be encoded.

The barcode's appearance is controlled via parameters passed via the 2nd argument. The only two required parameters are X and Y which specify the position of the lower-left corner of the symbol on the page. The Type parameter specifies which of the four barcode types to draw. The valid Type values are 1 (PDF417, default), 2 (Data Matrix), 3 (QR Code) and 4 (Aztec). The four 2D barcode types and their parameters are described below.

The 2D barcode functionality is demonstrated by Live Demo #9.

13.2.1 PDF417

In the context of the PDF417 barcode, "PDF" stands for Portable Data File and has nothing to do with the Adobe PDF file format. A PDF417 barcode symbol is essentially a stack of 1D barcodes. A single symbol can contain far more information than a 1D barcode (up to 1.1 KB) in a space no larger than a traditional barcode. This format allows you to store not only text and numbers but binary data as well. In addition, PDF417 has a built-in error correction system so that a symbol can be read even if it is partially damaged or destroyed. The following code snippet draws the barcode symbol shown above:

objPage.Canvas.DrawBarcode2D( "AspPDF Rules!", "X=10; Y=20" );

A PDF417 symbol has a columnar pattern. The leftmost and rightmost columns of every symbol always look the same and they are called start and stop patterns. These are abutted by row indicator columns, and the actual data columns are squeezed in the middle. The symbol above has 3 data columns. Each data column is a stack of "codewords" which are sequences of 4 bars and 4 spaces arranged on 17 available slots. That is where the number 417 comes from. A symbol can contain 1 to 30 data columns and 3 to 90 rows.

The appearance and properties of a PDF417 symbol are controlled by the following parameters (all optional):

As of Version 3.2.0.30456, the following parameters have been added to enable the creation of US Government-compliant barcodes on official USCIS forms:

Filling out PDF417 barcode-equipped government forms is covered in detail in Section 12.8 - Barcode-equipped Government Forms.

The following code snippet encodes binary data from a BLOB recordset field. It specifies the error correction level of 5 and bar width of 2:

objPage.Canvas.DrawBarcode2D( rs["blob"], "X=10; Y=20; ErrorLevel=5; BarWidth=2" );

Unlike the DrawBarcode method described in the previous subsection, the DrawBarcode2D method returns a value -- an instance of the PdfParam object populated with the barcode's width, height, number of rows, number of columns and error level. For example, the following code snippet draws a barcode and then prints out its width and height:

PdfParam objParam = objPage.Canvas.DrawBarcode2D( "data", "X=10; Y=20;" );
Response.Write( "Width=" + objParam["Width"].ToString() );
Response.Write( "Height=" + objParam["Height"].ToString() );

The number of rows, number of columns and error level are retrieved via objParam["Rows"], objParam["Columns"] and objParam["ErrorLevel"], respectively.

13.2.2 Data Matrix

Data Matrix is another very popular 2D barcode. Its symbols are usually square and sometimes rectangular. A single symbol can encode up to 2335 characters. The required parameters are X, Y, and Type (the latter must be set to 2.) This barcode uses the same error correction system as PDF417, but the error level is determined automatically, therefore the ErrorLevel parameter is not used. The symbol shown above was drawn using the following code:

objPage.Canvas.DrawBarcode2D( "DataMatrix is also supported!", "Type=2; X=10; Y=20" );

The BarWidth parameter specifies both the width and height of an individual cell of the symbol (in user units.) AspectRatio is not used since the cells are always square.

By default, the number of cell columns and rows in a symbol is determined automatically. You can specify the desired size via the Columns and Rows parameters. You must either specify both parameters, or neither. The following [Rows x Columns] combinations are valid: 10x10, 12x12, 14x14, 16x16, 18x18, 20x20, 22x22, 24x24, 26x26, 32x32, 36x36, 40x40, 44x44, 48x48, 52x52, 64x64, 72x72, 80x80, 88x88, 96x96, 104x104, 120x120, 132x132, 144x144, 8x18, 8x32, 12x26, 12x36, 16x36, and 16x48.

All the other parameters have the same meaning as with PDF417. The return values are also the same except that there is no ErrorLevel value.

13.2.3 QR Code

The QR Code barcode is used in commercial tracking applications as well as advertisements and signs aimed at mobile-phone users. It was invented in Japan in 1994. "QR" stands for "quick response." The three prominent corner boxes give the QR Code symbols a very distinctive look. The symbols are always square.

A single symbol can encode up to 7,089 numeric symbols, up to 4,296 alpha-numeric symbols, up to 2,953 binary bytes, or up to 1,817 Kanji/Kana characters. The required parameters are X, Y, and Type (the latter must be set to 3.) There are 4 error correction levels, from 0 to 3, 0 being the lowest, specified via the optional ErrorLevel parameter (0 by default.) The symbol shown above was drawn using the following code:

objPage.Canvas.DrawBarcode2D( "QR is scannable art", "Type=3; X=10; Y=20" );

By default, the size of the symbol is determined automatically. To make the symbol larger than necessary, use the Version parameter. The valid values are 0 (auto) and 1 to 40 to specify a larger size.

The BarWidth parameter specifies both the width and height of an individual cell of the symbol (in user units.) AspectRatio is not used since the cells are always square. Columns and Rows are not used since the size is determined automatically or specified via the Version parameter.

All the other parameters have the same meaning as with PDF417 and DataMatrix. The return values are also the same except that there is no ErrorLevel value.

13.2.4 Aztec

Aztec code was invented by Andrew Longacre, Jr. and Robert Hussey in 1995. It can encode 3832 digits, 3067 letters, or 1914 bytes of data. Aztec symbols have a recognizable "bull's eye" pattern in the center.

The required parameters are X, Y, and Type (the latter must be set to 4.) The symbol shown above was drawn using the following code:

objPage.Canvas.DrawBarcode2D( "Aztec has a bull's eye", "Type=4; X=10; Y=20" );

There are 4 error correction levels, from 1 to 4, specified via the optional ErrorLevel parameter (2 by default.)

Aztec symbols are always square. By default, the size of the symbol is determined automatically. To make the symbol larger than necessary, use the Version parameter. The valid values are 0 (auto) and 1 to 36 to specify a larger size. The following table maps the Version values to the corresponding symbol size. The symbols marked with an asterisk (*) in the table below are "compact" symbols, meaning they have a smaller bulls-eye pattern at the center of the symbol.

VersionSizeVersionSizeVersionSizeVersionSizeVersionSizeVersionSize
115x15*219x19*323x23*427x27*519x19623x23
727x27831x31937x371041x411145x451249x49
1353x531457x571561x611667x671771x711875x75
1979x792083x832187x872291x912395x9524101x101
25105x10526109x10927113x11328117x11729121x12130125x125
31131x13132135x13533139x13934143x14335147x14736151x151

The BarWidth parameter specifies both the width and height of an individual cell of the symbol (in user units.) AspectRatio is not used since the cells are always square. Columns and Rows are not used since the size is determined automatically or specified via the Version parameter. The return values are Width, Height, Rows and Columns.

Chapter 14: Document Stitching and Other Features Chapter 12: Form Fill-in
Search AspPDF.net

Newsletter Signup

Other Products
AspPDF
AspUpload
AspJpeg
AspEmail
AspEncrypt
AspGrid
AspUser
  This site is owned and maintained by Persits Software, Inc. Copyright © 2003 - 2014. All Rights Reserved.