|
| 1 | +// Title: Batch Mailmark Barcode Generation to BMP Files |
| 2 | +// Description: Demonstrates generating Mailmark barcodes for a list of customer records and saving each barcode as an individual BMP image. |
| 3 | +// Category-Description: This example belongs to the Aspose.BarCode generation category, focusing on complex barcode types such as Mailmark. It showcases the use of Aspose.BarCode.Generation.ComplexBarcodeGenerator and Aspose.BarCode.ComplexBarcode.MailmarkCodetext to create barcodes programmatically. Developers often need to batch‑process records, embed specific data fields, and export barcodes to image formats for printing or archival purposes. |
| 4 | +// Prompt: Batch generate Mailmark barcodes from customer records, saving each as separate BMP files. |
| 5 | +// Tags: mailmark, barcode, generation, bmp, aspose.barcode, complexbarcode |
| 6 | + |
1 | 7 | using System; |
2 | 8 | using System.Collections.Generic; |
3 | 9 | using System.IO; |
|
6 | 12 | using Aspose.BarCode.ComplexBarcode; |
7 | 13 |
|
8 | 14 | /// <summary> |
9 | | -/// Demonstrates generation of Mailmark barcodes using Aspose.BarCode library. |
| 15 | +/// Generates Mailmark barcodes for a collection of customer records and saves each barcode as a BMP file. |
10 | 16 | /// </summary> |
11 | 17 | class Program |
12 | 18 | { |
13 | 19 | /// <summary> |
14 | | - /// Entry point of the application. Generates a set of Mailmark barcodes and saves them as BMP files. |
| 20 | + /// Entry point of the application. Creates an output folder, iterates over sample records, |
| 21 | + /// builds Mailmark codetext, generates the barcode, and writes it to disk. |
15 | 22 | /// </summary> |
16 | 23 | static void Main() |
17 | 24 | { |
18 | | - // -------------------------------------------------------------------- |
19 | | - // Prepare output directory where barcode images will be saved. |
20 | | - // -------------------------------------------------------------------- |
| 25 | + // Define the output directory and ensure it exists |
21 | 26 | string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "MailmarkBarcodes"); |
22 | 27 | if (!Directory.Exists(outputDir)) |
23 | 28 | { |
24 | 29 | Directory.CreateDirectory(outputDir); |
25 | 30 | } |
26 | 31 |
|
27 | | - // -------------------------------------------------------------------- |
28 | | - // Sample customer records (limited to a safe batch size). |
29 | | - // Each record is represented by a MailmarkCodetext instance. |
30 | | - // -------------------------------------------------------------------- |
31 | | - var customers = new List<MailmarkCodetext> |
| 32 | + // Sample customer records (limited to 5 for safe execution) |
| 33 | + var records = new List<(int ItemID, string DestinationPostCodePlusDPS)> |
32 | 34 | { |
33 | | - new MailmarkCodetext { Format = 4, VersionID = 1, Class = "0", SupplychainID = 384224, ItemID = 16563762, DestinationPostCodePlusDPS = "EF61AH8T " }, |
34 | | - new MailmarkCodetext { Format = 4, VersionID = 1, Class = "1", SupplychainID = 384224, ItemID = 16563763, DestinationPostCodePlusDPS = "EF61AH8T " }, |
35 | | - new MailmarkCodetext { Format = 4, VersionID = 1, Class = "2", SupplychainID = 384224, ItemID = 16563764, DestinationPostCodePlusDPS = "EF61AH8T " }, |
36 | | - new MailmarkCodetext { Format = 4, VersionID = 1, Class = "3", SupplychainID = 384224, ItemID = 16563765, DestinationPostCodePlusDPS = "EF61AH8T " }, |
37 | | - new MailmarkCodetext { Format = 4, VersionID = 1, Class = "4", SupplychainID = 384224, ItemID = 16563766, DestinationPostCodePlusDPS = "EF61AH8T " } |
| 35 | + (16563762, "EF61AH8T "), |
| 36 | + (16563763, "EF61AH8T "), |
| 37 | + (16563764, "EF61AH8T "), |
| 38 | + (16563765, "EF61AH8T "), |
| 39 | + (16563766, "EF61AH8T ") |
38 | 40 | }; |
39 | 41 |
|
40 | | - // -------------------------------------------------------------------- |
41 | | - // Iterate over each customer record, generate the barcode, and save it. |
42 | | - // -------------------------------------------------------------------- |
43 | 42 | int index = 1; |
44 | | - foreach (var mailmark in customers) |
| 43 | + foreach (var record in records) |
45 | 44 | { |
46 | | - // Build a unique file name for each barcode image. |
47 | | - string fileName = $"Mailmark_{index:D3}.bmp"; |
48 | | - string filePath = Path.Combine(outputDir, fileName); |
49 | | - |
50 | | - try |
| 45 | + // Build the Mailmark codetext with required fields for each record |
| 46 | + var mailmark = new MailmarkCodetext |
51 | 47 | { |
52 | | - // Generate the barcode using the ComplexBarcodeGenerator. |
53 | | - using (var generator = new ComplexBarcodeGenerator(mailmark)) |
54 | | - { |
55 | | - // Save the generated barcode as a BMP file. |
56 | | - generator.Save(filePath, BarCodeImageFormat.Bmp); |
57 | | - } |
| 48 | + Format = 4, // 4‑state (unspecified/default) |
| 49 | + VersionID = 1, |
| 50 | + Class = "0", // Test/Null class |
| 51 | + SupplychainID = 384224, |
| 52 | + ItemID = record.ItemID, |
| 53 | + DestinationPostCodePlusDPS = record.DestinationPostCodePlusDPS |
| 54 | + }; |
58 | 55 |
|
59 | | - Console.WriteLine($"Generated: {filePath}"); |
60 | | - } |
61 | | - catch (Exception ex) |
| 56 | + // Generate the barcode and save it as a BMP image |
| 57 | + using (var generator = new ComplexBarcodeGenerator(mailmark)) |
62 | 58 | { |
63 | | - // Log any errors that occur during barcode generation. |
64 | | - Console.WriteLine($"Failed to generate barcode for record {index}: {ex.Message}"); |
| 59 | + string filePath = Path.Combine(outputDir, $"Mailmark_{index}.bmp"); |
| 60 | + generator.Save(filePath, BarCodeImageFormat.Bmp); |
| 61 | + Console.WriteLine($"Saved Mailmark barcode to: {filePath}"); |
65 | 62 | } |
66 | 63 |
|
67 | 64 | index++; |
|
0 commit comments