How to Generate GS1 Data Matrix Barcodes Using Aspose.BarCode for .NET

How to Generate GS1 Data Matrix Barcodes Using Aspose.BarCode for .NET

This article shows how to generate GS1 Data Matrix barcodes in .NET using Aspose.BarCode. GS1 Data Matrix is a global standard for 2D coding in pharma, food, medical, and supply chain management, enabling unique identification, tracking, and anti-counterfeiting.

Real-World Problem

Regulated industries require serialization and unique product identification for global trade, often enforced by GS1 standards. Manually creating compliant barcodes is error-prone, and generic tools rarely offer full GS1 Data Matrix support.

Solution Overview

Aspose.BarCode for .NET enables developers to generate GS1 Data Matrix barcodes with proper FNC1, Application Identifiers (AIs), and ECC200—ensuring full compliance for regulated workflows and serialization projects.


Prerequisites

Before you start, ensure you have:

  1. Visual Studio 2019 or later
  2. .NET 6.0 or later (or .NET Framework 4.6.2+)
  3. Aspose.BarCode for .NET installed via NuGet
  4. Basic knowledge of C#
PM> Install-Package Aspose.BarCode

Step-by-Step Implementation

Step 1: Install and Import Aspose.BarCode

Install the NuGet package and import the required namespaces:

using Aspose.BarCode.Generation;

Step 2: Create the GS1 Data Matrix Generator

Instantiate the generator for GS1 Data Matrix:

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.GS1DataMatrix, "(01)09501101530003(17)250101(10)ABC123(21)1234567");

Step 3: GS1 Code Text and Compliance

Format the data string per GS1 syntax with AIs. FNC1 is added automatically:

// Example with AIs: GTIN, Expiry Date, Batch, Serial
string gs1Text = "(01)09501101530003(17)250101(10)ABC123(21)1234567";
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.GS1DataMatrix, gs1Text);

Step 4: Customize Data Matrix Properties

Set error correction, version, and style:

generator.Parameters.Barcode.DataMatrix.DataMatrixEcc = DataMatrixEccType.ECC200;
generator.Parameters.Barcode.DataMatrix.DataMatrixVersion = DataMatrixVersion.Auto;
generator.Parameters.Barcode.XDimension.Pixels = 6;
generator.Parameters.Barcode.BarColor = Color.Black;
generator.Parameters.Barcode.BackColor = Color.White;

Step 5: Generate and Save the GS1 Data Matrix Barcode

Export the barcode to PNG, JPEG, or any supported format:

generator.Save("gs1-datamatrix.png", BarCodeImageFormat.Png);

Step 6: Complete Example

using Aspose.BarCode.Generation;
using System.Drawing; // Required for Color

class Program
{
    static void Main()
    {
        string gs1Text = "(01)09501101530003(17)250101(10)ABC123(21)1234567";
        BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.GS1DataMatrix, gs1Text);
        generator.Parameters.Barcode.DataMatrix.DataMatrixEcc = DataMatrixEccType.ECC200;
        generator.Parameters.Barcode.DataMatrix.DataMatrixVersion = DataMatrixVersion.Auto;
        generator.Parameters.Barcode.XDimension.Pixels = 6;
        generator.Parameters.Barcode.BarColor = Color.Black;
        generator.Parameters.Barcode.BackColor = Color.White;
        generator.Save("gs1-datamatrix.png", BarCodeImageFormat.Png);
        // Validate with GS1 scanner or tool
    }
} 

Use Cases and Applications

  • Pharmaceutical serialization: Compliance with DSCSA, FMD, and other regulations
  • Food traceability: Per EU and global food safety
  • Logistics and supply chain: Carton/pallet level tracking

Common Challenges and Solutions

Challenge 1: GS1 barcode not recognized? Solution: Check syntax, ensure correct AIs, use ECC200, and test with GS1 validation tools.

Challenge 2: Too much data for label? Solution: Shorten batch or serial numbers, use the right version/size.

Challenge 3: Compliance and audit? Solution: Log all generated code data, document all parameters, validate output with certified readers.


Performance Considerations

  • Batch generate codes for product lines or packaging
  • Use memory streams for integration with printing workflows
  • Adjust Data Matrix size for print/scanner compatibility

Best Practices

  1. Use ECC200 for regulatory acceptance
  2. Always validate barcode output with certified devices
  3. Format data per GS1 syntax (AIs, FNC1)
  4. Export PNG for print or digital

Advanced Scenarios

1. Batch Generate GS1 Data Matrix Barcodes

foreach (var item in pharmaProducts)
{
    BarcodeGenerator g = new BarcodeGenerator(EncodeTypes.GS1DataMatrix, item.GS1Text);
    g.Save($"gs1_{item.Serial}.png", BarCodeImageFormat.Png);
}

2. Generate GS1 Data Matrix with Custom Colors

generator.Parameters.Barcode.BarColor = Color.DarkBlue;
generator.Parameters.Barcode.BackColor = Color.WhiteSmoke;

Conclusion

With Aspose.BarCode for .NET, you can generate GS1 Data Matrix barcodes for global compliance in pharma, supply chain, and food safety. Explore more in the Aspose.BarCode API Reference .

 English