Search code examples
c#sdkbarcode

Generating a PDF417 barcode from a Byte array in c#


I need a way of generating PDF417 barcodes from byte arrays.

I have found numerous SDKs that can generate barcodes but all of them expect a string as the data instead of a byte array, unfortunately this isn't a possibility since I need to conform to a pre-existing standard

Thanks in advance

Code that I have tried, as requested by Wyatt Earp.

    /// <summary>
    /// Writes the barcode data to a specified location
    /// </summary>
    /// <param name="data">Data of the barcode</param>
    /// <param name="Location">Location to save barcode</param>
    public void Write(byte[] data, string Location)
    {
        ///* Keep Automation Barcode Creator
        KeepAutomation.Barcode.Crystal.BarCode KABarcode = new KeepAutomation.Barcode.Crystal.BarCode();
        KABarcode.Symbology = KeepAutomation.Barcode.Symbology.PDF417;
        KABarcode.PDF417DataMode = KeepAutomation.Barcode.PDF417DataMode.Auto;
        KABarcode.CodeToEncode = data;
        KABarcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
        KABarcode.generateBarcodeToImageFile(Location);
        //*/

        ///* BarcodeLib Creator
        BarcodeLib.Barcode.PDF417 barcodeLibBar = new BarcodeLib.Barcode.PDF417();
        barcodeLibBar.Data = data;
        var BarLibImage = barcodeLibBar.drawBarcode();
        BarLibImage.Save(Location);
        //*/

        ///* PQScan.Barcode Creator
        PQScan.BarcodeCreator.Barcode PQScanBarcode = new PQScan.BarcodeCreator.Barcode();
        PQScanBarcode.BarType = PQScan.BarcodeCreator.BarCodeType.PDF417;
        PQScanBarcode.Data = data;
        PQScanBarcode.PictureFormat = System.Drawing.Imaging.ImageFormat.Png;
        var PQScanImage = PQScanBarcode.CreateBarcode();
        PQScanImage.Save(Location);
        //*/
    }          

None of these will build since they all expect strings as the barcode data, I need to give them a byte[]

Unfortunately I already deleted the code from the other SDK's but all of them tend to follow the same pattern.

Comprehensive list of SDK's I have tried are:

  • SharpPdf417
  • ZXing
  • PQScan.BarcodeCreator
  • TBarcode
  • KeepAutomation.Barcode
  • BarcodeLib

All of these SDKs only accept a string as the barcode data, I need the byte array that is read to be the byte array that is inputted into this void,

I am unable to give you the exact byte array, but it has a size of 454 and uses several different encoding methods throughout the byte array.


Solution

  • Thanks, but Since I needed to keep it in the format it already was in Wyatt Earps Answer didn't work for me but it does seem like it would work for others.

    For me I managed to find that Aspose.Barcode had a feature that would allow me to generate barcodes straight from the byte array instead of having to convert it.

    Below is the code I use incase it is of interest to anyone

        /// <summary>
        /// Writes the barcode data to a specified location
        /// </summary>
        /// <param name="data">Byte data of barcode</param>
        /// <param name="Location">Location to save barcode</param>
        public void Write(byte[] data, string location)
        {
            //Define the barcode builder with properties
            BarCodeBuilder builder = new BarCodeBuilder()
            {
                SymbologyType = Symbology.Pdf417,
                Rows = 30
            };
    
            //Set Data
            builder.SetBinaryCodeText(data);
    
            //Generate Barcode
            var barcodeBitmap = builder.GenerateBarCodeImage();
    
            //Save it to disk
            barcodeBitmap.Save(location);
        }