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,37 @@
// 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

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

/// <summary>
/// Demonstrates generating a GS1 Code 128 barcode with a margin and saving it as a JPEG image.
/// Generates a GS1 Code 128 barcode, applies a 10‑pixel margin on all sides, and saves the result as a JPEG image.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application. Generates the barcode and writes it to a file.
/// Entry point of the example. Creates the barcode, configures padding, and writes the JPEG file.
/// </summary>
static void Main()
{
// Define the barcode text. For GS1 Code 128 the AI must be enclosed in parentheses.
string codeText = "(01)12345678901231";
// Sample GS1 Code 128 data (AI (01) for GTIN)
const string codeText = "(01)12345678901231";

// Destination file path for the generated image.
string outputPath = "gs1code128_margin.jpg";

// Initialize the barcode generator with the desired symbology and text.
// Initialize the barcode generator with the GS1 Code 128 symbology and the sample data
using (var generator = new BarcodeGenerator(EncodeTypes.GS1Code128, codeText))
{
// Set a uniform 10‑pixel padding (margin) on all four sides of the barcode.
// 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;
generator.Parameters.Barcode.Padding.Right.Pixels = 10f;
generator.Parameters.Barcode.Padding.Bottom.Pixels = 10f;

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

// 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,3 +1,9 @@
// 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.
// 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

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

/// <summary>
/// Program to generate GS1 DataMatrix barcodes from AI strings and save them as PNG files.
/// 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.
/// </summary>
class Program
{
/// <summary>
/// Entry point. Generates PNG files for a list of GS1 AI strings.
/// Main method that orchestrates the barcode generation workflow.
/// </summary>
/// <param name="args">Command‑line arguments (not used).</param>
static void Main(string[] args)
static void Main()
{
// Sample list of GS1 AI strings (fallback if no arguments are provided)
// Define a sample list of AI (Application Identifier) strings to encode as GS1 DataMatrix.
List<string> aiStrings = new List<string>
{
"(01)12345678901231",
"(01)98765432109876",
"(01)55555555555555",
"(01)11111111111111",
"(01)22222222222222"
"(01)01234567890128(10)ABC123",
"(01)09876543210987(21)XYZ789",
"(01)12345678901231(17)221231",
"(01)55555555555555(3103)001500",
"(01)99999999999999(3102)000750"
};

// Determine output directory for generated PNG files
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "GS1DataMatrixOutput");

// Ensure the output directory exists
if (!Directory.Exists(outputDir))
// Ensure the output directory exists.
string outputFolder = "OutputDataMatrix";
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputDir);
Directory.CreateDirectory(outputFolder);
}

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

// Combine the output directory with the safe file name
string outputPath = Path.Combine(outputDir, safeFileName);

// Generate the GS1 DataMatrix barcode for the current AI string
// Create and configure the barcode generator for GS1 DataMatrix.
using (var generator = new BarcodeGenerator(EncodeTypes.GS1DataMatrix, aiString))
{
// Optional: set resolution or other parameters if needed
generator.Parameters.Resolution = 300f;
// 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;

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

// Inform the user that the file has been generated
// Output the result to the console for tracking.
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, '_');
}

// Indicate that all conversions have finished
Console.WriteLine("Batch conversion completed.");
// Remove parentheses and spaces that are unnecessary for the file name.
return input.Replace("(", "").Replace(")", "").Replace(" ", "_");
}
}
Original file line number Diff line number Diff line change
@@ -1,89 +1,75 @@
// 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.
// 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

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

/// <summary>
/// Demonstrates generating GS1 Code 128 barcodes, saving them as PNG files,
/// packaging them into a ZIP archive, and cleaning up temporary files.
/// Demonstrates batch creation of GS1 Code 128 barcodes and compression of the resulting PNG files into a ZIP archive.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Generates barcode images from sample data, zips them, and outputs the archive path.
/// 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.
/// </summary>
static void Main()
{
// Sample GS1 Code 128 data (AI format)
var dataList = new List<string>
// Define sample GS1 Code 128 data strings using Application Identifier (AI) format.
List<string> gs1Data = new List<string>
{
"(01)12345678901231",
"(01)98765432109876",
"(01)55555555555555",
"(01)11111111111111",
"(01)22222222222222"
"(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)
};

// Directory to store temporary PNG files
string outputDir = Path.Combine(Path.GetTempPath(), "Gs1Barcodes");
if (!Directory.Exists(outputDir))
{
// Create the directory if it does not exist
Directory.CreateDirectory(outputDir);
}
// Create an output directory for the generated PNG files.
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes");
Directory.CreateDirectory(outputDir);

// Generate PNG images for each data string
var pngFiles = new List<string>();
foreach (var data in dataList)
// Iterate over each GS1 data string and generate a corresponding barcode image.
for (int i = 0; i < gs1Data.Count; i++)
{
// Build a safe file name by removing parentheses and spaces
string fileName = $"barcode_{data.Replace("(", "").Replace(")", "").Replace(" ", "")}.png";
string codeText = gs1Data[i];
string fileName = $"barcode_{i + 1}.png";
string filePath = Path.Combine(outputDir, fileName);

// Create a barcode generator for GS1 Code 128
using (var generator = new BarcodeGenerator(EncodeTypes.GS1Code128, data))
// Initialize the barcode generator with GS1 Code 128 symbology and the current data string.
using (var generator = new BarcodeGenerator(EncodeTypes.GS1Code128, codeText))
{
// Optional: show checksum in human‑readable text
// Ensure the checksum is always displayed (optional visual requirement).
generator.Parameters.Barcode.ChecksumAlwaysShow = true;

// Save the generated barcode directly as a PNG file
// Save the generated barcode as a PNG file.
generator.Save(filePath);
}

// Keep track of the generated file path for later zipping
pngFiles.Add(filePath);
}

// Path for the final ZIP archive containing all PNGs
string zipPath = Path.Combine(outputDir, "Gs1Barcodes.zip");

// Ensure any existing ZIP archive is removed before creating a new one
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
// Define the path for the ZIP archive that will contain all generated PNG files.
string zipPath = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes.zip");

// Create a ZIP archive and add each PNG file as an entry
using (var zip = ZipFile.Open(zipPath, ZipArchiveMode.Create))
// 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 (var png in pngFiles)
foreach (string file in Directory.GetFiles(outputDir, "*.png"))
{
// Add the PNG file to the archive using its file name only
zip.CreateEntryFromFile(png, Path.GetFileName(png));
string entryName = Path.GetFileName(file);
archive.CreateEntryFromFile(file, entryName);
}
}

// Clean up temporary PNG files (optional)
foreach (var png in pngFiles)
{
File.Delete(png);
}

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