Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
// Title: Apply 10‑pixel margin to GS1 Code 128 barcode and save as JPEG
// Description: Demonstrates how to generate a GS1 Code 128 barcode, add a uniform 10‑pixel margin, and export it as a JPEG image.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, illustrating the use of BarcodeGenerator, EncodeTypes, and barcode padding parameters. Typical use cases include creating GS1‑compliant barcodes for product labeling with custom margins for better readability. Developers often need to adjust padding, size, and output format when integrating barcodes into documents or images.
// Prompt: Apply a 10‑pixel margin around a GS1 Code 128 barcode and save as JPEG.
// Tags: gs1, code128, barcode, margin, padding, jpeg, aspose.barcode, generation
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, illustrating the use of BarcodeGenerator, EncodeTypes, and BarCodeImageFormat classes. Typical use cases include creating printable barcodes with custom padding for layout requirements. Developers often need to adjust margins to fit design constraints or scanning guidelines.
/// Prompt: Apply a 10‑pixel margin around a GS1 Code 128 barcode and save as JPEG.
/// Tags: gs1, code128, margin, jpeg, aspose.barcode, barcodegenerator

using System;
using Aspose.BarCode.Generation;
using Aspose.BarCode;

/// <summary>
/// Generates a GS1 Code 128 barcode, applies a 10‑pixel margin on all sides, and saves the result as a JPEG image.
/// Demonstrates generating a GS1 Code 128 barcode with a 10‑pixel margin and saving it as a JPEG image.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the example. Creates the barcode, configures padding, and writes the JPEG file.
/// Entry point that creates the barcode, applies padding, and writes the image to disk.
/// </summary>
static void Main()
{
// Sample GS1 Code 128 data (AI (01) for GTIN)
const string codeText = "(01)12345678901231";
// GS1 Code 128 codetext: AI (01) with a 14‑digit GTIN
string codeText = "(01)00123456789012";

// Initialize the barcode generator with the GS1 Code 128 symbology and the sample data
// Output JPEG file path
string outputPath = "gs1code128_margin.jpg";

// Initialize the barcode generator for GS1 Code 128
using (var generator = new BarcodeGenerator(EncodeTypes.GS1Code128, codeText))
{
// Apply a uniform 10‑pixel margin (padding) around the barcode
generator.Parameters.Barcode.Padding.Left.Pixels = 10f;
generator.Parameters.Barcode.Padding.Top.Pixels = 10f;
generator.Parameters.Barcode.Padding.Right.Pixels = 10f;
// Apply a uniform 10‑pixel margin on all sides via padding
generator.Parameters.Barcode.Padding.Left.Pixels = 10f;
generator.Parameters.Barcode.Padding.Top.Pixels = 10f;
generator.Parameters.Barcode.Padding.Right.Pixels = 10f;
generator.Parameters.Barcode.Padding.Bottom.Pixels = 10f;

// Save the generated barcode as a JPEG image file
generator.Save("gs1code128.jpg");
// Save the generated barcode as a JPEG image
generator.Save(outputPath, BarCodeImageFormat.Jpeg);
}

// Inform the user where the file was saved
Console.WriteLine($"Barcode saved to {outputPath}");
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Title: Batch convert AI strings to GS1 DataMatrix PNG files using parallel processing
// Description: Demonstrates how to encode a list of GS1 Application Identifier strings into DataMatrix barcodes and save them as PNG images in parallel.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, focusing on GS1 DataMatrix encoding. It showcases the use of BarcodeGenerator, EncodeTypes, and image format classes to create high‑resolution PNG files. Developers often need to batch‑process multiple barcode values efficiently, and this pattern illustrates parallel execution with safe file naming.
// Title: Batch conversion of AI strings to GS1 DataMatrix PNG files using parallel processing
// Description: Demonstrates how to generate GS1 DataMatrix barcodes from a list of Application Identifier (AI) strings and save them as PNG images in parallel.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, illustrating the use of the BarcodeGenerator class with EncodeTypes.GS1DataMatrix. It shows typical scenarios such as bulk barcode creation for inventory or logistics, where developers need to efficiently produce multiple barcode images with proper file naming. The snippet highlights parallel processing with Parallel.ForEach to speed up large‑scale barcode generation tasks.
// Prompt: Batch convert a list of AI strings to GS1 DataMatrix PNG files using parallel processing.
// Tags: gs1 datamatrix, batch, parallel, png, barcode generation, aspose.barcode, encode types, image output
// Tags: gs1datamatrix, barcode generation, parallel processing, png output, aspose.barcode, encode types, bulk conversion

using System;
using System.Collections.Generic;
Expand All @@ -12,67 +12,55 @@
using Aspose.BarCode.Generation;

/// <summary>
/// Provides an entry point that batch‑processes a collection of GS1 Application Identifier strings,
/// generating GS1 DataMatrix barcodes and saving each as a PNG file using parallel execution.
/// Provides an entry point for generating GS1 DataMatrix barcodes from a collection of AI strings
/// and saving them as PNG files using parallel processing.
/// </summary>
class Program
{
/// <summary>
/// Main method that orchestrates the barcode generation workflow.
/// Main method that orchestrates the batch barcode generation.
/// </summary>
static void Main()
{
// Define a sample list of AI (Application Identifier) strings to encode as GS1 DataMatrix.
// Define a sample list of GS1 AI strings (each must contain AI (01) with 14 digits)
List<string> aiStrings = new List<string>
{
"(01)01234567890128(10)ABC123",
"(01)09876543210987(21)XYZ789",
"(01)12345678901231(17)221231",
"(01)55555555555555(3103)001500",
"(01)99999999999999(3102)000750"
"(01)00123456789012", // GTIN-12 padded to 14 digits
"(01)01234567890123", // GTIN-13 padded to 14 digits
"(01)12345678901231", // GTIN-14 with valid check digit
"(01)00012345678905", // GTIN-12 padded
"(01)00001234567890" // GTIN-13 padded
};

// Ensure the output directory exists.
string outputFolder = "OutputDataMatrix";
// Prepare the output directory for generated PNG files
string outputFolder = Path.Combine(Directory.GetCurrentDirectory(), "GS1DataMatrixOutput");
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputFolder);
}

// Process each AI string in parallel to improve performance on multi‑core systems.
Parallel.ForEach(aiStrings, aiString =>
// Perform barcode generation in parallel to improve performance
Parallel.ForEach(aiStrings, (codeText) =>
{
// Generate a file‑system‑safe name from the AI string (remove invalid characters).
string safeFileName = GetSafeFileName(aiString) + ".png";
// Create a safe file name by stripping characters illegal in file names
string safeFileName = codeText.Replace("(", "").Replace(")", "").Replace(" ", "") + ".png";
string outputPath = Path.Combine(outputFolder, safeFileName);

// Create and configure the barcode generator for GS1 DataMatrix.
using (var generator = new BarcodeGenerator(EncodeTypes.GS1DataMatrix, aiString))
// Initialize the barcode generator for GS1 DataMatrix with the current AI string
using (var generator = new BarcodeGenerator(EncodeTypes.GS1DataMatrix, codeText))
{
// Set image size using interpolation mode for high‑quality scaling.
generator.Parameters.AutoSizeMode = AutoSizeMode.Interpolation;
generator.Parameters.ImageWidth.Point = 300f;
generator.Parameters.ImageHeight.Point = 300f;
// Optional: adjust module size if required
// generator.Parameters.Barcode.XDimension.Point = 2f;

// Save the generated barcode as a PNG file.
// Save the generated barcode as a PNG image
generator.Save(outputPath, BarCodeImageFormat.Png);
}

// Output the result to the console for tracking.
Console.WriteLine($"Generated: {outputPath}");
// Log the successful generation of the file
Console.WriteLine($"Generated {outputPath}");
});
}

// Helper method to create a file‑system‑safe name from the AI string.
private static string GetSafeFileName(string input)
{
// Replace any characters that are invalid in file names.
foreach (char c in Path.GetInvalidFileNameChars())
{
input = input.Replace(c, '_');
}

// Remove parentheses and spaces that are unnecessary for the file name.
return input.Replace("(", "").Replace(")", "").Replace(" ", "_");
// Indicate that the batch process has finished
Console.WriteLine("Batch conversion completed.");
}
}
Original file line number Diff line number Diff line change
@@ -1,75 +1,78 @@
// Title: Batch generate GS1 Code 128 barcodes and zip them
// Description: Generates multiple GS1 Code 128 barcodes as PNG files and compresses them into a single ZIP archive for easy distribution.
// Category-Description: This example belongs to the Aspose.BarCode generation category, demonstrating how to use the BarcodeGenerator class with EncodeTypes.GS1Code128 to create barcodes, customize parameters (e.g., checksum display), and save them in PNG format. It also shows how to package the generated images using System.IO.Compression.ZipArchive. Developers working with product identification, inventory, or logistics often need to produce GS1-compliant barcodes in bulk and deliver them as a single archive.
// Title: Batch generate GS1 Code 128 barcodes and package them into a ZIP archive
// Description: Demonstrates creating multiple GS1‑128 (GS1 Code 128) barcodes from GTIN‑14 values, saving each as a PNG, and compressing all images into a single ZIP file for easy distribution.
// Category-Description: This example belongs to the Aspose.BarCode generation category, showcasing how to use the BarcodeGenerator class with EncodeTypes.GS1Code128. Typical use cases include bulk creation of product barcodes for inventory, labeling, or e‑commerce platforms, where developers need to automate image output and bundle results for downstream processing. The code illustrates setting visual parameters, exporting to PNG, and using .NET's ZipArchive to create a distributable archive.
// Prompt: Batch generate GS1 Code 128 barcodes, compress PNG outputs into a single ZIP archive for distribution.
// Tags: gs1, code128, barcode, generation, png, zip, aspose.barcode
// Tags: gs1,code128,barcode,generation,png,zip,compression,aspose.barcode

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using Aspose.BarCode;
using Aspose.BarCode.Generation;

/// <summary>
/// Demonstrates batch creation of GS1 Code 128 barcodes and compression of the resulting PNG files into a ZIP archive.
/// Demonstrates batch generation of GS1 Code 128 barcodes and zipping the PNG outputs.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the example. Generates barcode images from predefined GS1 data strings,
/// saves them as PNG files, and archives them into a single ZIP file.
/// Entry point that creates barcodes for a set of GTIN‑14 values, saves them as PNG images,
/// and stores them in a ZIP archive.
/// </summary>
static void Main()
{
// Define sample GS1 Code 128 data strings using Application Identifier (AI) format.
List<string> gs1Data = new List<string>
// Define a collection of sample GTIN‑14 values (14 digits, leading zeros preserved)
string[] gtins = new string[]
{
"(01)12345678901231", // GTIN only
"(01)98765432109876(10)ABC123", // GTIN + Batch/Lot
"(01)55555555555555(21)SN001", // GTIN + Serial Number
"(01)11111111111111(17)230101", // GTIN + Expiration Date
"(01)22222222222222(3103)001500" // GTIN + Net weight (kg)
"00123456789012",
"01234567890123",
"12345678901234",
"23456789012345",
"34567890123456"
};

// Create an output directory for the generated PNG files.
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes");
Directory.CreateDirectory(outputDir);
// Target path for the resulting ZIP archive
string zipPath = "GS1Code128Barcodes.zip";

// Iterate over each GS1 data string and generate a corresponding barcode image.
for (int i = 0; i < gs1Data.Count; i++)
// Create the ZIP archive and add each generated PNG as an entry
using (FileStream zipFile = new FileStream(zipPath, FileMode.Create))
using (ZipArchive archive = new ZipArchive(zipFile, ZipArchiveMode.Create))
{
string codeText = gs1Data[i];
string fileName = $"barcode_{i + 1}.png";
string filePath = Path.Combine(outputDir, fileName);
int index = 1; // Simple counter for naming entries

// Initialize the barcode generator with GS1 Code 128 symbology and the current data string.
using (var generator = new BarcodeGenerator(EncodeTypes.GS1Code128, codeText))
foreach (string gtin in gtins)
{
// Ensure the checksum is always displayed (optional visual requirement).
generator.Parameters.Barcode.ChecksumAlwaysShow = true;
// GS1 Code 128 requires the Application Identifier (01) followed by a 14‑digit GTIN
string codeText = $"(01){gtin}";

// Save the generated barcode as a PNG file.
generator.Save(filePath);
}
}
// Initialise the barcode generator with the GS1 Code 128 symbology and the prepared text
using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.GS1Code128, codeText))
{
// Optional visual settings: module size (X‑dimension) and bar height
generator.Parameters.Barcode.XDimension.Point = 2f;
generator.Parameters.Barcode.BarHeight.Point = 50f;

// Define the path for the ZIP archive that will contain all generated PNG files.
string zipPath = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes.zip");
// Render the barcode to a memory stream in PNG format
using (MemoryStream ms = new MemoryStream())
{
generator.Save(ms, BarCodeImageFormat.Png);
ms.Position = 0; // Reset stream position before copying

// Create the ZIP archive and add each PNG file as an entry.
using (var zipStream = new FileStream(zipPath, FileMode.Create))
using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, leaveOpen: false))
{
foreach (string file in Directory.GetFiles(outputDir, "*.png"))
{
string entryName = Path.GetFileName(file);
archive.CreateEntryFromFile(file, entryName);
// Create a new entry in the ZIP archive for this barcode image
ZipArchiveEntry entry = archive.CreateEntry($"barcode_{index}.png", CompressionLevel.Optimal);
using (Stream entryStream = entry.Open())
{
// Copy the PNG data into the ZIP entry
ms.CopyTo(entryStream);
}
}
}

index++;
}
}

// Output summary information to the console.
Console.WriteLine($"Generated {gs1Data.Count} GS1 Code 128 barcodes in '{outputDir}'.");
Console.WriteLine($"Compressed into ZIP archive: {zipPath}");
// Inform the user where the ZIP archive was created
Console.WriteLine($"ZIP archive created: {Path.GetFullPath(zipPath)}");
}
}
Loading
Loading