Generate PDF417 Barcodes in C# .NET

PSPDFKit GdPicture.NET Library enables you to generate one-dimensional (1D or linear) and two-dimensional (2D) barcodes.

GdPicture.NET supports all 1D barcode formats and the following 2D barcode formats:

  • Aztec Code
  • Data Matrix
  • MaxiCode
  • Micro QR
  • PDF417
  • QR

To generate a PDF417 barcode, follow these steps:

  1. Create a GdPictureImaging object.

  2. Set the encoding mode of the barcode with the BarcodePDF417EncodingMode enumeration, and store it in a variable. Use BarcodePDF417EncodingModeUndefined to let the engine determine the most appropriate encoding mode for the input data.

  3. Set the error correction level with the BarcodePDF417ErrorCorrectionLevel enumeration, and store it in a variable. Use BarcodePDF417ErrorCorrectionLevelAuto to let the engine determine the error correction level.

  4. Compute the size of the barcode with the BarcodePDF417GetSize method of the GdPictureImaging object. This method takes the following parameters:

    • The data encoded in the barcode.

    • The encoding mode.

    • The error correction level.

    • The number of rows in the barcode. Use 0 to let the engine determine the number of rows or a value from 3 to 90.

    • The number of columns in the barcode. Use 0 to let the engine determine the number of columns or a value from 3 to 90.

    • The number of modules considered quiet zone around the barcode. The recommended value is 4 or more.

    • The size of each module in pixels. The recommended value is 4 or more.

    • The height of rows in pixels. The recommended value is 10 or more.

    • An output parameter that defines the width of the barcode.

    • An output parameter that defines the height of the barcode.

  5. Create an empty GdPicture image with the CreateNewGdPictureImage method of the GdPictureImaging object. This method takes the following parameters:

    • The width of the image in pixels.

    • The height of the image in pixels.

    • The bit depth of the image.

    • The background color of the image. Use the ARGB method of the GdPictureImaging object to specify the color with the ARGB standard. For example, for a white background, set gdpictureImaging.ARGB(255, 255, 255).

  6. Write the barcode to the empty image with the BarcodePDF417Write method of the GdPictureImaging object. This method takes the following parameters:

    • The image ID of the newly created image.

    • The data encoded in the barcode.

    • The encoding mode.

    • The error correction level.

    • The number of rows in the barcode. Use 0 to let the engine determine the number of rows or a value from 3 to 90.

    • The number of columns in the barcode. Use 0 to let the engine determine the number of columns or a value from 3 to 90.

    • The number of modules considered quiet zone around the barcode. The recommended value is 4 or more.

    • The size of each module in pixels. The recommended value is 4 or more.

    • The height of rows in pixels. The recommended value is 10 or more.

    • The distance between the left side of the image and the barcode in pixels.

    • The distance between the top side of the image and the barcode in pixels.

    • The angle of the barcode.

    • The color of the symbols on the barcode. Use the ARGB method of the GdPictureImaging object to specify the color with the ARGB standard. For example, for black, set gdpictureImaging.ARGB(0, 0, 0).

    • The background color of the barcode. Use the ARGB method of the GdPictureImaging object to specify the color with the ARGB standard. For example, for white, set gdpictureImaging.ARGB(255, 255, 255).

  7. Save the image as a PNG file with the SaveAsPNG method of the GdPictureImaging object.

  8. Release unnecessary resources.

The example below generates a PDF417 barcode:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Set the encoding mode of the barcode.
BarcodePDF417EncodingMode encodingMode = BarcodePDF417EncodingMode.BarcodePDF417EncodingModeUndefined;
// Set the error correction level.
BarcodePDF417ErrorCorrectionLevel errorCorrectionLevel = BarcodePDF417ErrorCorrectionLevel.BarcodePDF417ErrorCorrectionLevelAuto;
// Set the number of rows and columns in the barcode.
int rows = 0;
int columns = 0;
// Compute the size of the barcode.
gdpictureImaging.BarcodePDF417GetSize("0123456789", encodingMode, ref errorCorrectionLevel, ref rows, ref columns, 4, 8, 20, out int width, out int height);
// Create an empty GdPicture image.
int imageID = gdpictureImaging.CreateNewGdPictureImage(width, height, 32, gdpictureImaging.ARGB(255, 255, 255));
// Write the barcode to the empty image.
gdpictureImaging.BarcodePDF417Write(imageID, "0123456789", encodingMode, errorCorrectionLevel, rows, columns, 4, 8, 20, 0, 0, 0, gdpictureImaging.ARGB(0, 0, 0), gdpictureImaging.ARGB(255, 255, 255));
// Save the image as a PNG file.
gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png");
// Release unnecessary resources.
gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Set the encoding mode of the barcode.
    Dim encodingMode As BarcodePDF417EncodingMode = BarcodePDF417EncodingMode.BarcodePDF417EncodingModeUndefined
    ' Set the error correction level.
    Dim errorCorrectionLevel As BarcodePDF417ErrorCorrectionLevel = BarcodePDF417ErrorCorrectionLevel.BarcodePDF417ErrorCorrectionLevelAuto
    ' Set the number of rows and columns in the barcode.
    Dim rows = 0
    Dim columns = 0
    ' Compute the size of the barcode.
    Dim width As Integer = Nothing, height As Integer = Nothing
    gdpictureImaging.BarcodePDF417GetSize("0123456789", encodingMode, errorCorrectionLevel, rows, columns, 4, 8, 20, width, height)
    ' Create an empty GdPicture image.
    Dim imageID As Integer = gdpictureImaging.CreateNewGdPictureImage(width, height, 32, gdpictureImaging.ARGB(255, 255, 255))
    ' Write the barcode to the empty image.
    gdpictureImaging.BarcodePDF417Write(imageID, "0123456789", encodingMode, errorCorrectionLevel, rows, columns, 4, 8, 20, 0, 0, 0, gdpictureImaging.ARGB(0, 0, 0), gdpictureImaging.ARGB(255, 255, 255))
    ' Save the image as a PNG file.
    gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png")
    ' Release unnecessary resources.
    gdpictureImaging.ReleaseGdPictureImage(imageID)
End Using
Used Methods and Properties

Related Topics