Generate 2D barcodes with XFA compression in .NET

Blog category: Barcode

June 16, 2020

Barcode data, which include recurrent sequences of symbols (e.g. a list of serial numbers, XML data, URLs, etc), can be reduced in size if the barcode data are compressed using Flate (ZIP) compression.
Reducing the size of barcode data may be necessary in case if you want:
VintaSoft Barcode .NET SDK provides 2 ways to compress data of two-dimensional barcode.

WAY 1: Compress the data by yourself and generate a two-dimensional barcode with compressed binary data. This is possible because all two-dimensional barcodes allow you to store binary data (for detailed information refer to BinaryValueItem class).

WAY 2: Generate two-dimensional barcode according to XML Forms Architecture (XFA) specification. In this case the barcode data will be compressed using the compression algorithm "DEFLATE Compressed Data Format" (RFC1951).


We will show in this article how to generate two-dimensional barcodes according to XML Forms Architecture (XFA) specification.

VintaSoft Barcode .NET SDK offers classes, which allow easily create and recognize barcodes with XFA compressed data:

Here is C# code that allows you to create an Aztec barcode with data compressed in compliance with XFA specification:
/// <summary>
/// Generates Aztec barcode with XFA compression.
/// </summary>
public void GenerateAztecBarcodeWithXfaCompression()
{
    string barcodeText =
        "https://www.vintasoft.com/vsbarcode-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsimaging-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsannotation-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vspdf-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjbig2-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjpeg2000-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdoccleanup-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsocr-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdicom-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsformsprocessing-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsoffice-dotnet-index.html";

    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // create the XFA compressed Aztec barcode symbology
    Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedAztecBarcodeSymbology xfaCompressedAztecBarcodeSymbology =
        new Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedAztecBarcodeSymbology();
    // encode barcode text using XFA compressed Aztec barcode symbology
    xfaCompressedAztecBarcodeSymbology.Encode(barcodeText, barcodeWriter.Settings);

    // get barcode as image
    using (System.Drawing.Image barcodeImage = barcodeWriter.GetBarcodeAsBitmap())
    {
        // save the barcode image to a file
        barcodeImage.Save("aztec-barcode-with-xfa-compression.png");
    }
}

Here is an image of Aztec barcode that contains a list of URLs compressed according to the XFA specification:
An image of Aztec barcode that contains a list of URLs compressed according to the XFA specification
Here is an image of Aztec barcode that contains a list of URLs in the form of uncompressed text:
An image of Aztec barcode that contains a list of URLs in the form of uncompressed text

Here is C# code that allows you to create a DataMatrix barcode with data compressed in compliance with XFA specification:
/// <summary>
/// Generates DataMatrix barcode with XFA compression.
/// </summary>
public void GenerateDataMatrixBarcodeWithXfaCompression()
{
    string barcodeText =
        "https://www.vintasoft.com/vsbarcode-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsimaging-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsannotation-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vspdf-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjbig2-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjpeg2000-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdoccleanup-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsocr-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdicom-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsformsprocessing-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsoffice-dotnet-index.html";

    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // create the XFA compressed DataMatrix barcode symbology
    Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedDataMatrixBarcodeSymbology xfaCompressedDataMatrixBarcodeSymbology =
        new Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedDataMatrixBarcodeSymbology();
    // encode barcode text using XFA compressed DataMatrix barcode symbology
    xfaCompressedDataMatrixBarcodeSymbology.Encode(barcodeText, barcodeWriter.Settings);

    // get barcode as image
    using (System.Drawing.Image barcodeImage = barcodeWriter.GetBarcodeAsBitmap())
    {
        // save the barcode image to a file
        barcodeImage.Save("dataMatrix-barcode-with-xfa-compression.png");
    }
}

Here is an image of a DataMatrix barcode that contains a list of URLs compressed according to the XFA specification:
An image of DataMatrix barcode that contains a list of URLs compressed according to the XFA specification
Here is an image of DataMatrix barcode that contains a list of URLs in the form of uncompressed text:
An image of DataMatrix barcode that contains a list of URLs in the form of uncompressed text

Here is C# code that allows you to create a PDF417 barcode with data compressed in compliance with XFA specification:
/// <summary>
/// Generates PDF417 barcode with XFA compression.
/// </summary>
public void GeneratePdf417BarcodeWithXfaCompression()
{
    string barcodeText =
        "https://www.vintasoft.com/vsbarcode-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsimaging-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsannotation-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vspdf-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjbig2-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjpeg2000-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdoccleanup-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsocr-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdicom-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsformsprocessing-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsoffice-dotnet-index.html";

    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // create the XFA compressed PDF417 barcode symbology
    Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedPDF417BarcodeSymbology xfaCompressedPdf417BarcodeSymbology =
        new Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedPDF417BarcodeSymbology();
    // encode barcode text using XFA compressed PDF417 barcode symbology
    xfaCompressedPdf417BarcodeSymbology.Encode(barcodeText, barcodeWriter.Settings);

    // get barcode as image
    using (System.Drawing.Image barcodeImage = barcodeWriter.GetBarcodeAsBitmap())
    {
        // save the barcode image to a file
        barcodeImage.Save("pdf417-barcode-with-xfa-compression.png");
    }
}

Here is an image of PDF417 barcode that contains a list of URLs compressed according to the XFA specification:
An image of PDF417 barcode that contains a list of URLs compressed according to the XFA specification
Here is an image of PDF417 barcode that contains a list of URLs in the form of uncompressed text:
An image of PDF417 barcode that contains a list of URLs in the form of uncompressed text

Here is C# code that allows you to create a QR Code barcode with data compressed in compliance with XFA specification:
/// <summary>
/// Generates QR barcode with XFA compression.
/// </summary>
public void GenerateQrCodeBarcodeWithXfaCompression()
{
    string barcodeText =
        "https://www.vintasoft.com/vsbarcode-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsimaging-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsannotation-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vspdf-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjbig2-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjpeg2000-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdoccleanup-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsocr-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdicom-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsformsprocessing-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsoffice-dotnet-index.html";

    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // create the XFA compressed QR barcode symbology
    Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedQRCodeBarcodeSymbology xfaCompressedQrCodeBarcodeSymbology =
        new Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedQRCodeBarcodeSymbology();
    // encode barcode text using XFA compressed QR barcode symbology
    xfaCompressedQrCodeBarcodeSymbology.Encode(barcodeText, barcodeWriter.Settings);

    // get barcode as image
    using (System.Drawing.Image barcodeImage = barcodeWriter.GetBarcodeAsBitmap())
    {
        // save the barcode image to a file
        barcodeImage.Save("qr-barcode-with-xfa-compression.png");
    }
}

Here is an image of QR barcode that contains a list of URLs compressed according to the XFA specification:
An image of QR barcode that contains a list of URLs compressed according to the XFA specification
Here is an image of QR barcode that contains a list of URLs in the form of uncompressed text:
An image of QR barcode that contains a list of URLs in the form of uncompressed text


Here is C# code that allows you to recognize two-dimensional barcodes generated in compliance with XFA specification:
/// <summary>
/// Recognizes 2D barcode with XFA compressed data.
/// </summary>
/// <param name="barcodeImageFile">A path to a barcode image file.</param>
public void Recognize2DBarcodeWithXfaCompressedData(string barcodeImageFile)
{
    // create barcode reader
    using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
    {
        // specify that reader must search for XFA compressed Aztec barcodes
        reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.XFACompressedAztec);
        // specify that reader must search for XFA compressed DataMatrix barcodes
        reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.XFACompressedDataMatrix);
        // specify that reader must search for XFA compressed PDF417 barcodes
        reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.XFACompressedPDF417);
        // specify that reader must search for XFA compressed QR barcodes
        reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.XFACompressedQRCode);

        // specify that reader must search for horizontal barcodes only
        reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal;

        // read barcodes from image file
        Vintasoft.Barcode.IBarcodeInfo[] barcodeInfos = reader.ReadBarcodes(barcodeImageFile);

        // if barcodes are not detected
        if (barcodeInfos.Length == 0)
        {
            System.Console.WriteLine("Barcodes are not found.");
        }
        // if barcodes are detected
        else
        {
            // get information about recognized barcodes

            System.Console.WriteLine(string.Format("{0} barcode(s) found:", barcodeInfos.Length));
            System.Console.WriteLine();
            for (int i = 0; i < barcodeInfos.Length; i++)
            {
                Vintasoft.Barcode.IBarcodeInfo barcodeInfo = barcodeInfos[i];
                System.Console.WriteLine(string.Format("[{0}:{1}]", i + 1, barcodeInfo.BarcodeType));
                System.Console.WriteLine(string.Format("Value:      {0}", barcodeInfo.Value));
                System.Console.WriteLine(string.Format("Region:     {0}", barcodeInfo.Region));
                System.Console.WriteLine();
            }
        }
    }
    System.Console.WriteLine();
    System.Console.WriteLine();
}


It should be noted that Flate compression (ZIP) can increase the amount of data if there is little data redundancy or if the data stream is too short.


Share this article