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
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
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.