-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-generate-barcodes-from-json-array-using-each-element-as-codetext-and-saving-each-as-jpeg.cs
More file actions
79 lines (71 loc) · 3.22 KB
/
Copy pathbatch-generate-barcodes-from-json-array-using-each-element-as-codetext-and-saving-each-as-jpeg.cs
File metadata and controls
79 lines (71 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.Json;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
/// <summary>
/// Generates barcode images from a JSON array of code texts using Aspose.BarCode.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application. Parses JSON, creates output directory,
/// generates barcodes, and saves them as JPEG files.
/// </summary>
static void Main()
{
// --------------------------------------------------------------------
// Sample JSON array of code texts. Replace with actual JSON if needed.
// --------------------------------------------------------------------
string json = "[\"ABC123\",\"DEF456\",\"GHI789\",\"JKL012\",\"MNO345\"]";
// --------------------------------------------------------------
// Deserialize the JSON string into a List<string>.
// --------------------------------------------------------------
List<string> codeTexts;
try
{
codeTexts = JsonSerializer.Deserialize<List<string>>(json);
}
catch (Exception ex)
{
// Output error message if JSON parsing fails and exit.
Console.WriteLine($"Failed to parse JSON: {ex.Message}");
return;
}
// --------------------------------------------------------------
// Validate that we have at least one code text to process.
// --------------------------------------------------------------
if (codeTexts == null || codeTexts.Count == 0)
{
Console.WriteLine("No code texts found in the JSON array.");
return;
}
// --------------------------------------------------------------
// Prepare the output directory for barcode images.
// --------------------------------------------------------------
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes");
Directory.CreateDirectory(outputDir);
// --------------------------------------------------------------
// Iterate over each code text, generate a barcode, and save it.
// --------------------------------------------------------------
for (int i = 0; i < codeTexts.Count; i++)
{
string codeText = codeTexts[i];
string fileName = $"barcode_{i + 1}.jpeg";
string outputPath = Path.Combine(outputDir, fileName);
// Create a BarcodeGenerator for Code128 with the current text.
using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, codeText))
{
// Save the generated barcode as a JPEG image.
generator.Save(outputPath, BarCodeImageFormat.Jpeg);
}
// Inform the user about the saved file.
Console.WriteLine($"Saved barcode for \"{codeText}\" to \"{outputPath}\"");
}
// --------------------------------------------------------------
// Indicate that the barcode generation process has finished.
// --------------------------------------------------------------
Console.WriteLine("Barcode generation completed.");
}
}