How to Create PDF417 Barcode in C#

The PDF417 barcode, also known as Portable Data File 417 or PDF417 Truncated, is a two-dimensional (2D), high-density symbology capable of encoding text, numbers, files and actual data bytes.

Compaction Modes

The data is encoded using one of three compaction modes: Text compaction mode, Binary compaction mode, and Numeric compaction mode.

  • Text: It allows encoding all printable ASCII characters, i.e. values from 32 to 126 inclusive in accordance with ISO/IEC 646, as well as selected control characters such as TAB (horizontal tab ASCII 9), LF (NL line feed, new line ASCII 10) and CR (carriage return ASCII 13).
  • Binary: It allows encoding all 256 possible 8-bit byte values. This includes all ASCII characters value from 0 to 127 inclusive and provides for international character set support.
  • Numeric: It allows efficient encoding of numeric data strings.
  • Auto: It switches between Text, Binary and Numeric modes in order to minimize the number of codewords to be encoded.

PDF417 Error Correction Levels

Error correction allows the symbol to endure some damage without causing loss of data. The error correction level depends on the amount of data that needs to be encoded, the size and the amount of symbol damage that could occur. The error correction levels range from 0 to 8.

EC Level 0 1 2 3 4 5 6 7 8
EC Codewords Generated 2 4 6 8 16 32 64 128 512
Data Codewords 1-40 41-160 161-320 321-863
Data Bytes Encoded 1-56 57-192 193-384 385-1035

Following code snippets show how to create a PDF417 barcode image using Spire.Barcode.

Step 1: Create an instance of BarcodeSetting class.

BarcodeSettings settings = new BarcodeSettings();

Step 2: Set the barode type as Pdf417 and set the data to be encoded.

settings.Type = BarCodeType.Pdf417;
settings.Data = "123456789";

Step 3: Set the data mode as numeric.

settings.Pdf417DataMode = Pdf417DataMode.Numeric;

Step 4: Set the error correction level as level 2.

settings.Pdf417ECL = Pdf417ECL.Level2;

Step 5: Initialize an instance of BarcodeGenerator and generate an image based on the settings.

BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();   

Step 6: Save the image in .png format.

image.Save("PDF417Code.png");

Output:

How to Create PDF417 Barcode in C#

Full Code:

using Spire.Barcode;
using System.Drawing;

namespace PDF417Code
{
    class Program
    {
        static void Main(string[] args)
        {
            BarcodeSettings settings = new BarcodeSettings();
            settings.Type = BarCodeType.Pdf417;
            settings.Data = "123456789";
            settings.Data2D = "123456789";
            settings.Pdf417DataMode = Pdf417DataMode.Numeric;
            settings.Pdf417ECL = Pdf417ECL.Level2;
            BarCodeGenerator generator = new BarCodeGenerator(settings);
            Image image = generator.GenerateImage();
            image.Save("PDF417Code.png");
        }
    }
}