Learning Objectives

In this tutorial, you’ll learn how to:

  • Generate multiple barcodes efficiently in batch operations
  • Create barcodes for lists of products or items
  • Implement sequential barcode generation for inventory systems
  • Organize and manage multiple barcodes in cloud storage
  • Optimize barcode batch generation across different programming languages

Prerequisites

Before starting this tutorial, make sure you have:

  • An Aspose Cloud account (sign up for a free trial if needed)
  • Your Client ID and Client Secret from the Aspose Cloud Dashboard
  • Basic knowledge of REST APIs
  • Completed the basic barcode generation tutorial or have equivalent knowledge
  • A development environment for your preferred language (C#, Java, PHP, Python, Node.js, or Go)

Introduction

Many applications require generating multiple barcodes for various items, products, or documents. Rather than creating each barcode individually, batch processing can significantly improve efficiency and save time.

In this tutorial, we’ll explore practical approaches to generating multiple barcodes using Aspose.BarCode Cloud API, focusing on real-world scenarios like inventory management, product cataloging, and asset tracking.

Practical Scenario: Inventory Management System

Let’s imagine you’re developing an inventory management system that needs to generate barcodes for hundreds of products. Each product has:

  • A unique product ID
  • A product name
  • A category
  • A price

Your task is to efficiently generate barcodes for all these products, ensuring each barcode:

  1. Contains the appropriate product information
  2. Is properly named for easy identification
  3. Is stored in cloud storage for access across multiple locations

Step 1: Understanding the Basic Approach

The basic approach for generating multiple barcodes involves:

  1. Preparing a list of items that need barcodes
  2. Iterating through the list and generating a barcode for each item
  3. Using appropriate naming conventions for the generated barcode files
  4. Organizing the barcodes in cloud storage

Let’s start implementing this approach in different programming languages.

Step 2: Generating Multiple Barcodes in C#

Here’s how to generate multiple barcodes in C#:

// Tutorial Code Example: Generate multiple barcodes in batch with C#
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace AsposeBarcodeCloudTutorial
{
    class Program
    {
        // Replace with your actual credentials
        const string ClientId = "YOUR_CLIENT_ID";
        const string ClientSecret = "YOUR_CLIENT_SECRET";
        const string ApiBaseUrl = "https://api.aspose.cloud/v3.0/barcode/";
        const string AuthUrl = "https://api.aspose.cloud/oauth2/token";

        // Product class to represent inventory items
        class Product
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Category { get; set; }
            public decimal Price { get; set; }

            // Format product information for the barcode text
            public string GetBarcodeText()
            {
                return $"{Id}|{Name}|{Price:C}";
            }

            // Generate a suitable filename for the barcode
            public string GetBarcodeFilename()
            {
                return $"{Category.ToLower()}-{Id}.png";
            }
        }

        static async Task Main(string[] args)
        {
            // Step 1: Get the access token
            var accessToken = await GetAccessToken();
            
            // Step 2: Load product data
            var products = GetSampleProducts();
            
            // Step 3: Generate barcodes for all products
            await GenerateBarcodesBatch(accessToken, products);
            
            Console.WriteLine("All product barcodes generated successfully!");
        }

        static async Task<string> GetAccessToken()
        {
            using (var client = new HttpClient())
            {
                // Prepare the form data for token request
                var formContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("grant_type", "client_credentials"),
                    new KeyValuePair<string, string>("client_id", ClientId),
                    new KeyValuePair<string, string>("client_secret", ClientSecret)
                });

                // Make the token request
                var response = await client.PostAsync(AuthUrl, formContent);
                response.EnsureSuccessStatusCode();
                
                // Parse the JSON response
                var jsonResponse = await response.Content.ReadAsStringAsync();
                // Simple parsing - in production, use proper JSON parsing
                var accessToken = jsonResponse.Split('"')[3];
                
                return accessToken;
            }
        }

        static List<Product> GetSampleProducts()
        {
            // In a real application, this data might come from a database
            return new List<Product>
            {
                new Product { Id = "ELEC001", Name = "Smartphone", Category = "Electronics", Price = 699.99m },
                new Product { Id = "ELEC002", Name = "Laptop", Category = "Electronics", Price = 1299.99m },
                new Product { Id = "ELEC003", Name = "Headphones", Category = "Electronics", Price = 149.99m },
                new Product { Id = "CLOTH001", Name = "T-Shirt", Category = "Clothing", Price = 19.99m },
                new Product { Id = "CLOTH002", Name = "Jeans", Category = "Clothing", Price = 49.99m },
                new Product { Id = "BOOK001", Name = "Programming Guide", Category = "Books", Price = 29.99m },
                new Product { Id = "BOOK002", Name = "Novel", Category = "Books", Price = 14.99m }
            };
        }

        static async Task GenerateBarcodesBatch(string accessToken, List<Product> products)
        {
            using (var client = new HttpClient())
            {
                // Set authorization header
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                
                // Process each product
                int count = 0;
                foreach (var product in products)
                {
                    try
                    {
                        // Build the request URL with parameters
                        string fileName = product.GetBarcodeFilename();
                        string barcodeText = product.GetBarcodeText();
                        string requestUrl = $"{ApiBaseUrl}{fileName}/generate?text={Uri.EscapeDataString(barcodeText)}" +
                                          $"&type=Code128&format=png" +
                                          $"&captionAboveText={Uri.EscapeDataString(product.Category)}" +
                                          $"&captionAboveVisible=true";
                        
                        // Make the PUT request to save to cloud
                        var response = await client.PutAsync(requestUrl, null);
                        response.EnsureSuccessStatusCode();
                        
                        count++;
                        Console.WriteLine($"Generated barcode {count}/{products.Count}: {fileName}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error generating barcode for {product.Id}: {ex.Message}");
                    }
                }
            }
        }
    }
}

Step 3: Implementing in Python

Let’s implement the same functionality in Python:

# Tutorial Code Example: Generate multiple barcodes in batch with Python
import requests
import time
import urllib.parse

# Replace with your actual credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
auth_url = "https://api.aspose.cloud/oauth2/token"
api_base_url = "https://api.aspose.cloud/v3.0/barcode/"

# Product class to represent inventory items
class Product:
    def __init__(self, id, name, category, price):
        self.id = id
        self.name = name
        self.category = category
        self.price = price
    
    def get_barcode_text(self):
        """Format product information for the barcode text"""
        return f"{self.id}|{self.name}|${self.price:.2f}"
    
    def get_barcode_filename(self):
        """Generate a suitable filename for the barcode"""
        return f"{self.category.lower()}-{self.id}.png"

def get_access_token():
    """Get OAuth2 access token"""
    payload = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
    }
    
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
    }
    
    response = requests.post(auth_url, data=payload, headers=headers)
    response.raise_for_status()
    
    return response.json()['access_token']

def get_sample_products():
    """Create a list of sample products"""
    # In a real application, this data might come from a database
    return [
        Product("ELEC001", "Smartphone", "Electronics", 699.99),
        Product("ELEC002", "Laptop", "Electronics", 1299.99),
        Product("ELEC003", "Headphones", "Electronics", 149.99),
        Product("CLOTH001", "T-Shirt", "Clothing", 19.99),
        Product("CLOTH002", "Jeans", "Clothing", 49.99),
        Product("BOOK001", "Programming Guide", "Books", 29.99),
        Product("BOOK002", "Novel", "Books", 14.99)
    ]

def generate_barcodes_batch(access_token, products):
    """Generate barcodes for all products"""
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    # Process each product
    count = 0
    for product in products:
        try:
            # Build the request URL with parameters
            file_name = product.get_barcode_filename()
            barcode_text = urllib.parse.quote(product.get_barcode_text())
            category = urllib.parse.quote(product.category)
            
            request_url = f"{api_base_url}{file_name}/generate"
            params = {
                'text': product.get_barcode_text(),
                'type': 'Code128',
                'format': 'png',
                'captionAboveText': product.category,
                'captionAboveVisible': 'true'
            }
            
            # Make PUT request to save to cloud
            response = requests.put(request_url, params=params, headers=headers)
            response.raise_for_status()
            
            count += 1
            print(f"Generated barcode {count}/{len(products)}: {file_name}")
            
            # Small delay to avoid overwhelming the API
            time.sleep(0.2)
            
        except Exception as e:
            print(f"Error generating barcode for {product.id}: {str(e)}")

if __name__ == "__main__":
    # Get access token
    token = get_access_token()
    
    # Load product data
    products = get_sample_products()
    
    # Generate barcodes for all products
    generate_barcodes_batch(token, products)
    
    print("All product barcodes generated successfully!")

Step 4: Organizing Barcodes by Category

For better organization, you might want to group barcodes by category or other criteria. Here’s how to implement that by creating folder structures in cloud storage:

// Adding to the previous C# example:

static async Task GenerateBarcodesInCategories(string accessToken, List<Product> products)
{
    using (var client = new HttpClient())
    {
        // Set authorization header
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        
        // Group products by category
        var productsByCategory = products.GroupBy(p => p.Category);
        
        foreach (var category in productsByCategory)
        {
            Console.WriteLine($"Generating barcodes for category: {category.Key}");
            
            // Process each product in this category
            foreach (var product in category)
            {
                try
                {
                    // Build the request URL with parameters
                    // Note the folder structure in the filename
                    string folderPath = $"{category.Key.ToLower()}/";
                    string fileName = $"{product.Id}.png";
                    string fullPath = folderPath + fileName;
                    
                    string barcodeText = product.GetBarcodeText();
                    string requestUrl = $"{ApiBaseUrl}{fullPath}/generate?text={Uri.EscapeDataString(barcodeText)}" +
                                      $"&type=Code128&format=png";
                    
                    // Make the PUT request to save to cloud
                    var response = await client.PutAsync(requestUrl, null);
                    response.EnsureSuccessStatusCode();
                    
                    Console.WriteLine($"Generated barcode: {fullPath}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error generating barcode for {product.Id}: {ex.Message}");
                }
            }
        }
    }
}

Step 5: Sequential Barcode Generation

For some applications like ticket systems or serial numbering, you might need to generate sequential barcodes:

# Adding to the previous Python example:

def generate_sequential_barcodes(access_token, prefix, start_number, count, digits=5):
    """Generate sequential barcodes with incrementing numbers"""
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    for i in range(count):
        try:
            # Generate sequential number with leading zeros
            sequence_num = start_number + i
            formatted_num = f"{sequence_num:0{digits}d}"
            
            # Create barcode text and filename
            barcode_text = f"{prefix}-{formatted_num}"
            file_name = f"sequential/{barcode_text}.png"
            
            request_url = f"{api_base_url}{file_name}/generate"
            params = {
                'text': barcode_text,
                'type': 'Code128',
                'format': 'png'
            }
            
            # Make PUT request to save to cloud
            response = requests.put(request_url, params=params, headers=headers)
            response.raise_for_status()
            
            print(f"Generated sequential barcode {i+1}/{count}: {barcode_text}")
            
            # Small delay to avoid overwhelming the API
            time.sleep(0.2)
            
        except Exception as e:
            print(f"Error generating sequential barcode {sequence_num}: {str(e)}")

# Usage example:
# generate_sequential_barcodes(token, "TICKET", 1000, 100)  # Generates TICKET-01000 through TICKET-01099

Step 6: Working with Data from External Sources

In real-world applications, you’ll often need to generate barcodes from data stored in external sources like CSV files, databases, or Excel sheets. Here’s an example using a CSV file:

# Example for generating barcodes from CSV data
import csv

def generate_barcodes_from_csv(access_token, csv_file_path):
    """Generate barcodes based on data from a CSV file"""
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    # Read data from CSV file
    products = []
    with open(csv_file_path, 'r', newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            # Assuming CSV has columns: id, name, category, price
            products.append(Product(
                row['id'], 
                row['name'], 
                row['category'], 
                float(row['price'])
            ))
    
    print(f"Loaded {len(products)} products from CSV file")
    
    # Generate barcodes for all products
    generate_barcodes_batch(access_token, products)

Step 7: Performance Optimization for Large Batches

When generating a large number of barcodes, consider these optimization techniques:

Parallel Processing in C#

// Adding to the previous C# example:
using System.Collections.Concurrent;
using System.Threading.Tasks;

static async Task GenerateBarcodesParallel(string accessToken, List<Product> products, int maxParallelism = 5)
{
    using (var semaphore = new SemaphoreSlim(maxParallelism))
    {
        var tasks = new List<Task>();
        var errors = new ConcurrentBag<string>();
        
        foreach (var product in products)
        {
            await semaphore.WaitAsync();
            
            tasks.Add(Task.Run(async () =>
            {
                try
                {
                    using (var client = new HttpClient())
                    {
                        // Set authorization header
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                        
                        // Build the request URL with parameters
                        string fileName = product.GetBarcodeFilename();
                        string barcodeText = product.GetBarcodeText();
                        string requestUrl = $"{ApiBaseUrl}{fileName}/generate?text={Uri.EscapeDataString(barcodeText)}" +
                                          $"&type=Code128&format=png";
                        
                        // Make the PUT request to save to cloud
                        var response = await client.PutAsync(requestUrl, null);
                        response.EnsureSuccessStatusCode();
                        
                        Console.WriteLine($"Generated barcode: {fileName}");
                    }
                }
                catch (Exception ex)
                {
                    errors.Add($"Error generating barcode for {product.Id}: {ex.Message}");
                }
                finally
                {
                    semaphore.Release();
                }
            }));
        }
        
        await Task.WhenAll(tasks);
        
        if (errors.Any())
        {
            Console.WriteLine("Errors occurred during barcode generation:");
            foreach (var error in errors)
            {
                Console.WriteLine(error);
            }
        }
    }
}

Batch Processing in Python

# Adding to the previous Python example:
import concurrent.futures

def generate_barcodes_batch_parallel(access_token, products, max_workers=5):
    """Generate barcodes in parallel"""
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    def generate_single_barcode(product):
        try:
            # Build the request URL with parameters
            file_name = product.get_barcode_filename()
            
            request_url = f"{api_base_url}{file_name}/generate"
            params = {
                'text': product.get_barcode_text(),
                'type': 'Code128',
                'format': 'png'
            }
            
            # Make PUT request to save to cloud
            response = requests.put(request_url, params=params, headers=headers)
            response.raise_for_status()
            
            return (True, file_name)
            
        except Exception as e:
            return (False, f"Error generating barcode for {product.id}: {str(e)}")
    
    # Use thread pool for parallel execution
    with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
        futures = {executor.submit(generate_single_barcode, product): product for product in products}
        
        for future in concurrent.futures.as_completed(futures):
            product = futures[future]
            success, result = future.result()
            
            if success:
                print(f"Generated barcode: {result}")
            else:
                print(result)

Troubleshooting Common Issues

Issue: Rate limiting when generating too many barcodes

  • Symptom: HTTP 429 Too Many Requests errors
  • Solution: Implement delay between requests or reduce parallelism

Issue: Memory consumption with large batches

  • Possible cause: Loading too many products in memory at once
  • Solution: Process products in smaller chunks or batches

Issue: Difficulty managing many barcode files

  • Possible cause: Flat file structure without organization
  • Solution: Implement folder structures based on categories or other criteria

What You’ve Learned

Congratulations! In this tutorial, you’ve learned how to:

  • Generate multiple barcodes efficiently in batch operations
  • Create barcodes for product inventories and item lists
  • Implement sequential barcode generation for numbered systems
  • Organize barcodes by category in cloud storage
  • Optimize performance for large-scale barcode generation
  • Process data from external sources like CSV files
  • Implement parallel processing for faster batch generation

Further Practice

To reinforce your learning:

  1. Create a CSV with sample product data and generate barcodes from it
  2. Implement a batch process that generates barcodes with different symbologies based on category
  3. Create a system that generates sequential barcodes for tickets or invoices
  4. Build a small web application that allows uploading a product list and generating barcodes for all items

Next Steps

Now that you’ve mastered generating multiple barcodes, you might want to explore how to control barcode image settings like resolution and format:

Tutorial: Set Barcode Image Resolution and Format

Helpful Resources

Have questions about this tutorial? Feel free to post them on our support forum.