-
Notifications
You must be signed in to change notification settings - Fork 1
2.7 PDF417 Barcode
PDF417 barcode support software is based on article PDF417 Barcode Encoder Class Library and Demo App. The PDF417 barcode documentation and specification can be found in the following websites. Wikipedia provides a good introduction to PDF417. The PDF417 standard can be purchased from the ISO organization at this website. An early version of the specifications can be downloaded from this website for free. I strongly recommend that you download this document if you want to fully understand the encoding options.
The PDF417 barcode encodes array of bytes into an image of black and white bars. Encoding Unicode text requires converting Unicode characters into bytes. The decoder must do the reverse process to recover the text. The bytes are converted to codewords. This conversion process compresses the bytes into codewords. The encoder adds error correction codewords for error detection and recovery. Once the total number of data codewords and error correction codewords is known , the encoder divides the codewords into data rows and data columns. The final step is the creation of a black and white image.
Adding PDF417 barcode to your PDF document must follow the steps below.
- Create
Pdf417Encoder
object. - Set encoding options. All encoding options have default values.
- Encode a data string or a data bytes array.
- Check the image width and height or the number of data columns and data rows to make sure image size is appropriate for * your application. If it is not, adjust the layout.
- Create
PdfImage
. - Draw the barcode image with
PdfContent.DrawImage
Example of PDF417 Barcode Drawing
private void DrawPdf417Barcode()
{
// save graphics state
Contents.SaveGraphicsState();
// create PDF417 barcode
Pdf417Encoder Pdf417 = new Pdf417Encoder();
string ArticleLink = "http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version";
// encode text
Pdf417.Encode(ArticleLink);
Pdf417.WidthToHeightRatio(2.5);
// convert Pdf417 to black and white image
PdfImage BarcodeImage = new PdfImage(Document, Pdf417);
// draw image
Contents.DrawImage(BarcodeImage, 1.1, 5.2, 2.5);
// restore graphics sate
Contents.RestoreGraphicsState();
return;
}
PDF417 Barcode Object
Create PDF417 barcode object. This object can be reused serially to produce multiple barcodes.
// create PDF417 barcode
Pdf417Encoder Pdf417 = new Pdf417Encoder();
Set optional parameters to control the encoding process.
Encoding control
The PDF417 encoder encodes Input bytes into codewords. There are three types of codewords: byte, text and numeric. The program has an algorithm to divide the data input into these three types to compress the data. The default is Auto
. However, you can restrict the encoding to only bytes or only text and bytes.
Pdf417.EncodingControl = Pdf417EncodingControl.Auto;
// or
Pdf417.EncodingControl = Pdf417EncodingControl.ByteOnly;
// or
Pdf417.EncodingControl = Pdf417EncodingControl.TextAndByte;
Error Correction Level
The PDF417 adds error correction codewords to detect errors and correct them. More error correction codewords improves the reliability of the barcode. However, it makes the barcode bigger. Error correction level allows you to control the quality of the barcode. The ErrorCorrectionLevel
enumeration has two types of values. Fixed levels from 0 to 8. And levels that are recommended values based on the number of data codewords. The default value in ErrorCorrectionLevel.AutoNormal
. For more details look at Table 6 and Table 7 in the PDF417 Specification.
Pdf417.ErrorCorrection = ErrorCorrectionLevel.Level_0; // up to Level_8
// or
Pdf417.ErrorCorrection = ErrorCorrectionLevel.AutoNormal;
// or
Pdf417.ErrorCorrection = ErrorCorrectionLevel.AutoLow; // one less than normal
// or
Pdf417.ErrorCorrection = ErrorCorrectionLevel.AutoMedium; // one more than normal
// or
Pdf417.ErrorCorrection = ErrorCorrectionLevel.AutoHigh; // two more than normal
Narrow Bar Width
The width in pixels of a narrow barcode bar. The default is 2. If this value is changed, the program makes sure that RowHeight
is at least three times that value. And that QuiteZone
is at least twice that value.
Pdf417.NarrowBarWidth = value;
Row Height
The height in pixels of one row. This value must be greater than or equal to 3 times the NarrowBarWidth
value. The default is 6.
Pdf417.RowHeight = value;
Quiet Zone
The width of the quiet zone all around the barcode. The quiet zone is white. This value must be greater than or equal to 2 times the NarrowBarWidth
value. The default is 4.
Pdf417.QuietZone = value;
Default Data Columns
The default data columns value. The value must be in the range of 1 to 30. The default is 3. After the input data is encoded, the software sets the number of data columns to the default data columns and calculates the number of data rows. If the number of data rows exceeds the maximum allowed (90), the software sets the number of rows to the maximum allowed and recalculate the number of data columns. If the result is greater than the maximum columns allowed, an exception is thrown.
Pdf417.DefaultDataColumns = value;
Global Label ID Character Set
Set Global Label ID Character Set to ISO 8859 standard. The n can be 1 to 9 or 13 or 15. If the string is null, the default of ISO-8859-1 is used. Language support is defined here.
Pdf417.GlobalLabelIDCharacterSet = "ISO-8859-n";
Global Label ID User Defined
Set Global Label ID User Defined value. The default is not used. I did not find any reference explaining the usage of this value. User defined value must be between 810900 and 811799.
Pdf417.GlobalLabelIDUserDefined = UserDefinedValue;
Global Label ID General Purpose
Set Global Label ID General Purpose value. The default is not used. I did not find any reference explaining the usage of this value. User defined value must be between 900 and 810899.
Pdf417.GlobalLabelIDGeneralPurpose = UserDefinedValue;
Encoding data There are two encoding methods. One accepts text string as an input and the other one accepts byte array as an input.
Pdf417.Encode(string StringData);
// or
Pdf417.Encode(byte[] BinaryData);
The barcode was designed for binary data. Therefore, the first method above must encode the string from 16 bit characters to byte array. The encode method with string input has the following conversion logic. The Global Label ID Character Set property control the conversion. It is done in two steps. Step one string to UTF8 and step two UTF8 to ISO-8859-n. If you want to encode Hebrew you should set the character set to ISO-8859-8.
public void Encode(string StringData)
{
// convert string to UTF8 byte array
byte[] UtfBytes = Encoding.UTF8.GetBytes(StringData);
// convert UTF8 byte array to ISO-8859-n byte array
Encoding ISO = Encoding.GetEncoding(_GlobalLabelIDCharacterSet ?? "ISO-8859-1");
byte[] IsoBytes = Encoding.Convert(Encoding.UTF8, ISO, UtfBytes);
// call the encode binary data method
Encode(IsoBytes);
Return;
}
Final Barcode Layout
After the data was encoded, you can check the layout of the barcode by examining these values: ImageWidth
, ImageHeight
, DataColumns
or DataRows
. If you want to readjust the width and the height of the barcode you can used one of the methods below. In addition, you can readjust the optional parameters: NarrowBarWidth
, RowHeight
or QuietZone
values.
Width to Height Ratio
This method will calculate the number of data rows and data columns to achieve a desired width to height ratio. The ratio includes the quiet zone. Check the return value for success.
bool Pdf417.WidthToHeightRatio(double Ratio);
Set Data Columns This method will calculate the number of data rows based on the desired number of data columns. Check the return value for success.
bool Pdf417.SetDataColumns(int Columns);
Set Data Rows
This method will calculate the number of data columns based on the desired number of data rows. Check the return value for success.
bool Pdf417.SetDataRows(int Rows);
Create PDF Document Image Resource
In this step we create a PDF document image resource from the PDF417 barcode.
PdfImage BarcodeImage = new PdfImage(Document, Pdf417);
Draw the Barcode
The last step is adding the barcode to the content of a PDF document’s page.
// PosX and PosY are the page coordinates in user units.
// Width is the width of the barcode in user units.
// The height of the barcode is calculated to preserve aspect ratio.
// Height = Width * BarcodeImage.ImageHeight / BarcodeImage.ImageWidth
Contents.DrawImage(BarcodeImage, PosX, PosY, Width);
For coding examples please review 3.8 Draw PDF417 Barcode, ArticleExample.cs and OtherExample.cs source code.
This page is a copy from https://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library by Uzi Granot. The article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL). All rights to the texts and source code remain with Uzi Granot.