Skip to content

Commit 9d0615f

Browse files
author
agent-aspose-barcode-examples
committed
feat(gs1-barcode-types): Add 34 Aspose.BarCode .NET C# examples for Gs1 Barcode Types — Aspose.BarCode for .NET 26.6.0
1 parent 60571b0 commit 9d0615f

34 files changed

Lines changed: 1049 additions & 924 deletions

File tree

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1+
// Title: Apply 10‑pixel margin to GS1 Code 128 barcode and save as JPEG
2+
// Description: Demonstrates how to generate a GS1 Code 128 barcode, add a uniform 10‑pixel margin, and export it as a JPEG image.
3+
// 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.
4+
// Prompt: Apply a 10‑pixel margin around a GS1 Code 128 barcode and save as JPEG.
5+
// Tags: gs1, code128, barcode, margin, padding, jpeg, aspose.barcode, generation
6+
17
using System;
2-
using Aspose.BarCode;
38
using Aspose.BarCode.Generation;
9+
using Aspose.BarCode;
410

511
/// <summary>
6-
/// Demonstrates generating a GS1 Code 128 barcode with a margin and saving it as a JPEG image.
12+
/// Generates a GS1 Code 128 barcode, applies a 10‑pixel margin on all sides, and saves the result as a JPEG image.
713
/// </summary>
814
class Program
915
{
1016
/// <summary>
11-
/// Entry point of the application. Generates the barcode and writes it to a file.
17+
/// Entry point of the example. Creates the barcode, configures padding, and writes the JPEG file.
1218
/// </summary>
1319
static void Main()
1420
{
15-
// Define the barcode text. For GS1 Code 128 the AI must be enclosed in parentheses.
16-
string codeText = "(01)12345678901231";
21+
// Sample GS1 Code 128 data (AI (01) for GTIN)
22+
const string codeText = "(01)12345678901231";
1723

18-
// Destination file path for the generated image.
19-
string outputPath = "gs1code128_margin.jpg";
20-
21-
// Initialize the barcode generator with the desired symbology and text.
24+
// Initialize the barcode generator with the GS1 Code 128 symbology and the sample data
2225
using (var generator = new BarcodeGenerator(EncodeTypes.GS1Code128, codeText))
2326
{
24-
// Set a uniform 10‑pixel padding (margin) on all four sides of the barcode.
27+
// Apply a uniform 10‑pixel margin (padding) around the barcode
2528
generator.Parameters.Barcode.Padding.Left.Pixels = 10f;
2629
generator.Parameters.Barcode.Padding.Top.Pixels = 10f;
27-
generator.Parameters.Barcode.Padding.Right.Pixels = 10f;
30+
generator.Parameters.Barcode.Padding.Right.Pixels = 10f;
2831
generator.Parameters.Barcode.Padding.Bottom.Pixels = 10f;
2932

30-
// Render and save the barcode as a JPEG image.
31-
generator.Save(outputPath, BarCodeImageFormat.Jpeg);
33+
// Save the generated barcode as a JPEG image file
34+
generator.Save("gs1code128.jpg");
3235
}
33-
34-
// Inform the user where the file was saved.
35-
Console.WriteLine($"Barcode saved to {outputPath}");
3636
}
3737
}
Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Title: Batch convert AI strings to GS1 DataMatrix PNG files using parallel processing
2+
// Description: Demonstrates how to encode a list of GS1 Application Identifier strings into DataMatrix barcodes and save them as PNG images in parallel.
3+
// 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.
4+
// Prompt: Batch convert a list of AI strings to GS1 DataMatrix PNG files using parallel processing.
5+
// Tags: gs1 datamatrix, batch, parallel, png, barcode generation, aspose.barcode, encode types, image output
6+
17
using System;
28
using System.Collections.Generic;
39
using System.IO;
@@ -6,59 +12,67 @@
612
using Aspose.BarCode.Generation;
713

814
/// <summary>
9-
/// Program to generate GS1 DataMatrix barcodes from AI strings and save them as PNG files.
15+
/// Provides an entry point that batch‑processes a collection of GS1 Application Identifier strings,
16+
/// generating GS1 DataMatrix barcodes and saving each as a PNG file using parallel execution.
1017
/// </summary>
1118
class Program
1219
{
1320
/// <summary>
14-
/// Entry point. Generates PNG files for a list of GS1 AI strings.
21+
/// Main method that orchestrates the barcode generation workflow.
1522
/// </summary>
16-
/// <param name="args">Command‑line arguments (not used).</param>
17-
static void Main(string[] args)
23+
static void Main()
1824
{
19-
// Sample list of GS1 AI strings (fallback if no arguments are provided)
25+
// Define a sample list of AI (Application Identifier) strings to encode as GS1 DataMatrix.
2026
List<string> aiStrings = new List<string>
2127
{
22-
"(01)12345678901231",
23-
"(01)98765432109876",
24-
"(01)55555555555555",
25-
"(01)11111111111111",
26-
"(01)22222222222222"
28+
"(01)01234567890128(10)ABC123",
29+
"(01)09876543210987(21)XYZ789",
30+
"(01)12345678901231(17)221231",
31+
"(01)55555555555555(3103)001500",
32+
"(01)99999999999999(3102)000750"
2733
};
2834

29-
// Determine output directory for generated PNG files
30-
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "GS1DataMatrixOutput");
31-
32-
// Ensure the output directory exists
33-
if (!Directory.Exists(outputDir))
35+
// Ensure the output directory exists.
36+
string outputFolder = "OutputDataMatrix";
37+
if (!Directory.Exists(outputFolder))
3438
{
35-
Directory.CreateDirectory(outputDir);
39+
Directory.CreateDirectory(outputFolder);
3640
}
3741

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

44-
// Combine the output directory with the safe file name
45-
string outputPath = Path.Combine(outputDir, safeFileName);
46-
47-
// Generate the GS1 DataMatrix barcode for the current AI string
49+
// Create and configure the barcode generator for GS1 DataMatrix.
4850
using (var generator = new BarcodeGenerator(EncodeTypes.GS1DataMatrix, aiString))
4951
{
50-
// Optional: set resolution or other parameters if needed
51-
generator.Parameters.Resolution = 300f;
52+
// Set image size using interpolation mode for high‑quality scaling.
53+
generator.Parameters.AutoSizeMode = AutoSizeMode.Interpolation;
54+
generator.Parameters.ImageWidth.Point = 300f;
55+
generator.Parameters.ImageHeight.Point = 300f;
5256

53-
// Save the generated barcode as a PNG file
54-
generator.Save(outputPath);
57+
// Save the generated barcode as a PNG file.
58+
generator.Save(outputPath, BarCodeImageFormat.Png);
5559
}
5660

57-
// Inform the user that the file has been generated
61+
// Output the result to the console for tracking.
5862
Console.WriteLine($"Generated: {outputPath}");
5963
});
64+
}
65+
66+
// Helper method to create a file‑system‑safe name from the AI string.
67+
private static string GetSafeFileName(string input)
68+
{
69+
// Replace any characters that are invalid in file names.
70+
foreach (char c in Path.GetInvalidFileNameChars())
71+
{
72+
input = input.Replace(c, '_');
73+
}
6074

61-
// Indicate that all conversions have finished
62-
Console.WriteLine("Batch conversion completed.");
75+
// Remove parentheses and spaces that are unnecessary for the file name.
76+
return input.Replace("(", "").Replace(")", "").Replace(" ", "_");
6377
}
6478
}
Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,75 @@
1+
// Title: Batch generate GS1 Code 128 barcodes and zip them
2+
// Description: Generates multiple GS1 Code 128 barcodes as PNG files and compresses them into a single ZIP archive for easy distribution.
3+
// 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.
4+
// Prompt: Batch generate GS1 Code 128 barcodes, compress PNG outputs into a single ZIP archive for distribution.
5+
// Tags: gs1, code128, barcode, generation, png, zip, aspose.barcode
6+
17
using System;
28
using System.Collections.Generic;
39
using System.IO;
410
using System.IO.Compression;
511
using Aspose.BarCode;
612
using Aspose.BarCode.Generation;
7-
using Aspose.Drawing;
813

914
/// <summary>
10-
/// Demonstrates generating GS1 Code 128 barcodes, saving them as PNG files,
11-
/// packaging them into a ZIP archive, and cleaning up temporary files.
15+
/// Demonstrates batch creation of GS1 Code 128 barcodes and compression of the resulting PNG files into a ZIP archive.
1216
/// </summary>
1317
class Program
1418
{
1519
/// <summary>
16-
/// Entry point of the application.
17-
/// Generates barcode images from sample data, zips them, and outputs the archive path.
20+
/// Entry point of the example. Generates barcode images from predefined GS1 data strings,
21+
/// saves them as PNG files, and archives them into a single ZIP file.
1822
/// </summary>
1923
static void Main()
2024
{
21-
// Sample GS1 Code 128 data (AI format)
22-
var dataList = new List<string>
25+
// Define sample GS1 Code 128 data strings using Application Identifier (AI) format.
26+
List<string> gs1Data = new List<string>
2327
{
24-
"(01)12345678901231",
25-
"(01)98765432109876",
26-
"(01)55555555555555",
27-
"(01)11111111111111",
28-
"(01)22222222222222"
28+
"(01)12345678901231", // GTIN only
29+
"(01)98765432109876(10)ABC123", // GTIN + Batch/Lot
30+
"(01)55555555555555(21)SN001", // GTIN + Serial Number
31+
"(01)11111111111111(17)230101", // GTIN + Expiration Date
32+
"(01)22222222222222(3103)001500" // GTIN + Net weight (kg)
2933
};
3034

31-
// Directory to store temporary PNG files
32-
string outputDir = Path.Combine(Path.GetTempPath(), "Gs1Barcodes");
33-
if (!Directory.Exists(outputDir))
34-
{
35-
// Create the directory if it does not exist
36-
Directory.CreateDirectory(outputDir);
37-
}
35+
// Create an output directory for the generated PNG files.
36+
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes");
37+
Directory.CreateDirectory(outputDir);
3838

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

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

53-
// Save the generated barcode directly as a PNG file
52+
// Save the generated barcode as a PNG file.
5453
generator.Save(filePath);
5554
}
56-
57-
// Keep track of the generated file path for later zipping
58-
pngFiles.Add(filePath);
5955
}
6056

61-
// Path for the final ZIP archive containing all PNGs
62-
string zipPath = Path.Combine(outputDir, "Gs1Barcodes.zip");
63-
64-
// Ensure any existing ZIP archive is removed before creating a new one
65-
if (File.Exists(zipPath))
66-
{
67-
File.Delete(zipPath);
68-
}
57+
// Define the path for the ZIP archive that will contain all generated PNG files.
58+
string zipPath = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes.zip");
6959

70-
// Create a ZIP archive and add each PNG file as an entry
71-
using (var zip = ZipFile.Open(zipPath, ZipArchiveMode.Create))
60+
// Create the ZIP archive and add each PNG file as an entry.
61+
using (var zipStream = new FileStream(zipPath, FileMode.Create))
62+
using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, leaveOpen: false))
7263
{
73-
foreach (var png in pngFiles)
64+
foreach (string file in Directory.GetFiles(outputDir, "*.png"))
7465
{
75-
// Add the PNG file to the archive using its file name only
76-
zip.CreateEntryFromFile(png, Path.GetFileName(png));
66+
string entryName = Path.GetFileName(file);
67+
archive.CreateEntryFromFile(file, entryName);
7768
}
7869
}
7970

80-
// Clean up temporary PNG files (optional)
81-
foreach (var png in pngFiles)
82-
{
83-
File.Delete(png);
84-
}
85-
86-
// Inform the user where the ZIP archive was created
87-
Console.WriteLine($"Generated ZIP archive at: {zipPath}");
71+
// Output summary information to the console.
72+
Console.WriteLine($"Generated {gs1Data.Count} GS1 Code 128 barcodes in '{outputDir}'.");
73+
Console.WriteLine($"Compressed into ZIP archive: {zipPath}");
8874
}
8975
}

0 commit comments

Comments
 (0)