Skip to content

Commit f005104

Browse files
author
agent-aspose-barcode-examples
committed
Add generated examples for barcode-reading-properties
1 parent c763441 commit f005104

5 files changed

Lines changed: 286 additions & 81 deletions

barcode-reading-properties/download-image-from-aws-s3-bucket-and-read-pdf417-linked-state-metadata.cs

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,68 @@
11
using System;
22
using System.IO;
33
using Aspose.BarCode;
4+
using Aspose.BarCode.Generation;
45
using Aspose.BarCode.BarCodeRecognition;
6+
using Aspose.Drawing;
7+
using Aspose.Drawing.Imaging;
58

6-
namespace AsposeBarcodeS3Example
9+
class Program
710
{
8-
class Program
11+
static void Main()
912
{
10-
static void Main(string[] args)
11-
{
12-
// Path to the image file containing the PDF417 barcode.
13-
// Replace with the actual path where the image is stored.
14-
string localFilePath = "downloaded_image.png";
13+
string imagePath = "sample_pdf417.png";
1514

16-
if (!File.Exists(localFilePath))
15+
if (!File.Exists(imagePath))
16+
{
17+
using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Pdf417, "LINKED_SAMPLE_DATA"))
1718
{
18-
Console.WriteLine($"File not found: {localFilePath}");
19-
return;
20-
}
19+
// Enable linked mode
20+
generator.Parameters.Barcode.Pdf417.IsLinked = true;
2121

22-
// Read PDF417 barcode and extract linked state metadata
23-
try
24-
{
25-
using (BarCodeReader reader = new BarCodeReader(localFilePath, DecodeType.Pdf417))
22+
// Add some macro metadata (optional, just for demonstration)
23+
generator.Parameters.Barcode.Pdf417.MacroPdf417FileID = 42;
24+
generator.Parameters.Barcode.Pdf417.MacroPdf417SegmentsCount = 1;
25+
generator.Parameters.Barcode.Pdf417.MacroPdf417SegmentID = 0;
26+
generator.Parameters.Barcode.Pdf417.MacroPdf417Addressee = "John Doe";
27+
generator.Parameters.Barcode.Pdf417.MacroPdf417Sender = "Acme Corp";
28+
29+
using (Bitmap bitmap = generator.GenerateBarCodeImage())
2630
{
27-
foreach (BarCodeResult result in reader.ReadBarCodes())
28-
{
29-
Console.WriteLine("Barcode Type: " + result.CodeTypeName);
30-
Console.WriteLine("Barcode Text: " + result.CodeText);
31-
32-
// Access PDF417 extended parameters
33-
if (result.Extended?.Pdf417 != null)
34-
{
35-
Console.WriteLine("IsLinked: " + result.Extended.Pdf417.IsLinked);
36-
Console.WriteLine("IsCode128Emulation: " + result.Extended.Pdf417.IsCode128Emulation);
37-
Console.WriteLine("MacroPdf417FileID: " + result.Extended.Pdf417.MacroPdf417FileID);
38-
Console.WriteLine("MacroPdf417SegmentID: " + result.Extended.Pdf417.MacroPdf417SegmentID);
39-
Console.WriteLine("MacroPdf417SegmentsCount: " + result.Extended.Pdf417.MacroPdf417SegmentsCount);
40-
}
41-
else
42-
{
43-
Console.WriteLine("No PDF417 extended parameters available.");
44-
}
45-
46-
Console.WriteLine(new string('-', 40));
47-
}
31+
bitmap.Save(imagePath, ImageFormat.Png);
4832
}
4933
}
50-
catch (Exception ex)
34+
35+
Console.WriteLine($"Sample barcode image generated at '{imagePath}'.");
36+
}
37+
38+
if (!File.Exists(imagePath))
39+
{
40+
Console.WriteLine($"Error: Image file '{imagePath}' not found.");
41+
return;
42+
}
43+
44+
using (BarCodeReader reader = new BarCodeReader(imagePath, DecodeType.Pdf417))
45+
{
46+
foreach (BarCodeResult result in reader.ReadBarCodes())
5147
{
52-
Console.WriteLine("Error during barcode recognition: " + ex.Message);
48+
Console.WriteLine($"Barcode Type : {result.CodeTypeName}");
49+
Console.WriteLine($"Code Text : {result.CodeText}");
50+
51+
var pdf417 = result.Extended.Pdf417;
52+
Console.WriteLine($"IsLinked : {pdf417.IsLinked}");
53+
Console.WriteLine($"IsCode128Emulation : {pdf417.IsCode128Emulation}");
54+
Console.WriteLine($"IsReaderInitialization : {pdf417.IsReaderInitialization}");
55+
Console.WriteLine($"MacroPdf417FileID : {pdf417.MacroPdf417FileID}");
56+
Console.WriteLine($"MacroPdf417SegmentsCount : {pdf417.MacroPdf417SegmentsCount}");
57+
Console.WriteLine($"MacroPdf417SegmentID : {pdf417.MacroPdf417SegmentID}");
58+
Console.WriteLine($"MacroPdf417Addressee : {pdf417.MacroPdf417Addressee}");
59+
Console.WriteLine($"MacroPdf417Sender : {pdf417.MacroPdf417Sender}");
60+
Console.WriteLine($"MacroPdf417Terminator : {pdf417.MacroPdf417Terminator}");
61+
Console.WriteLine($"MacroPdf417Checksum : {pdf417.MacroPdf417Checksum}");
62+
Console.WriteLine($"MacroPdf417FileName : {pdf417.MacroPdf417FileName}");
63+
Console.WriteLine($"MacroPdf417FileSize : {pdf417.MacroPdf417FileSize}");
64+
Console.WriteLine($"MacroPdf417TimeStamp : {pdf417.MacroPdf417TimeStamp}");
65+
Console.WriteLine();
5366
}
5467
}
5568
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.BarCode.BarCodeRecognition;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
// Azure Blob parameters (placeholders for real implementation)
10+
string connectionString = "YourAzureBlobConnectionString";
11+
string containerName = "your-container";
12+
string blobName = "sample.png";
13+
14+
// Local fallback file path
15+
string localFilePath = "sample.png";
16+
17+
// Azure Blob download code (commented out – Azure.Storage.Blobs not available in this environment)
18+
/*
19+
// Requires Azure.Storage.Blobs NuGet package
20+
var blobClient = new BlobClient(connectionString, containerName, blobName);
21+
using (var downloadStream = new MemoryStream())
22+
{
23+
blobClient.DownloadTo(downloadStream);
24+
downloadStream.Position = 0;
25+
using (var fileStream = new FileStream(localFilePath, FileMode.Create, FileAccess.Write))
26+
{
27+
downloadStream.CopyTo(fileStream);
28+
}
29+
}
30+
*/
31+
32+
// Verify that the image file exists before processing
33+
if (!File.Exists(localFilePath))
34+
{
35+
Console.WriteLine($"Image file '{localFilePath}' not found.");
36+
return;
37+
}
38+
39+
// Read all supported barcodes from the image
40+
using (var reader = new BarCodeReader(localFilePath, DecodeType.AllSupportedTypes))
41+
{
42+
foreach (var result in reader.ReadBarCodes())
43+
{
44+
Console.WriteLine($"BarCode Type: {result.CodeTypeName}");
45+
Console.WriteLine($"BarCode CodeText: {result.CodeText}");
46+
}
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.BarCode;
4+
using Aspose.BarCode.BarCodeRecognition;
5+
using Aspose.Pdf;
6+
using Aspose.Pdf.Facades;
7+
using Aspose.Drawing.Imaging;
8+
9+
class Program
10+
{
11+
static void Main()
12+
{
13+
// Path to the password‑protected PDF (or image container) and the password.
14+
string pdfPath = "protected.pdf";
15+
string password = "mySecret";
16+
17+
// Verify the file exists.
18+
if (!File.Exists(pdfPath))
19+
{
20+
Console.WriteLine($"File not found: {pdfPath}");
21+
return;
22+
}
23+
24+
// Open the encrypted PDF using the supplied password.
25+
using (Document pdfDoc = new Document(pdfPath, password))
26+
{
27+
// Prepare the PDF converter for rendering pages to images.
28+
using (PdfConverter pdfConverter = new PdfConverter(pdfDoc))
29+
{
30+
// Enable barcode optimization for better detection.
31+
pdfConverter.RenderingOptions.BarcodeOptimization = true;
32+
33+
// Process each page individually.
34+
for (int pageNumber = 1; pageNumber <= pdfDoc.Pages.Count; pageNumber++)
35+
{
36+
pdfConverter.StartPage = pageNumber;
37+
pdfConverter.EndPage = pageNumber;
38+
pdfConverter.DoConvert();
39+
40+
using (MemoryStream imageStream = new MemoryStream())
41+
{
42+
// Render the current page to the stream as PNG.
43+
pdfConverter.GetNextImage(imageStream);
44+
imageStream.Position = 0;
45+
46+
// Use BarCodeReader to detect all supported barcodes in the rendered image.
47+
using (BarCodeReader reader = new BarCodeReader(imageStream, DecodeType.AllSupportedTypes))
48+
{
49+
// Optional: improve detection on difficult images.
50+
reader.QualitySettings.InverseImage = InverseImageMode.Enabled;
51+
52+
// Perform recognition.
53+
BarCodeResult[] results = reader.ReadBarCodes();
54+
55+
// Output results for the current page.
56+
if (results.Length == 0)
57+
{
58+
Console.WriteLine($"Page {pageNumber}: No barcodes found.");
59+
}
60+
else
61+
{
62+
Console.WriteLine($"Page {pageNumber}: Detected {results.Length} barcode(s).");
63+
foreach (BarCodeResult result in results)
64+
{
65+
Console.WriteLine($" Type: {result.CodeTypeName}");
66+
Console.WriteLine($" Text: {result.CodeText}");
67+
Console.WriteLine($" Confidence: {result.Confidence}");
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.BarCode.BarCodeRecognition;
4+
using Aspose.Pdf;
5+
using Aspose.Pdf.Facades;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
// Path to the encrypted PDF and its password.
12+
const string pdfPath = "sample_encrypted.pdf";
13+
const string pdfPassword = "password";
14+
15+
// Verify that the PDF file exists.
16+
if (!File.Exists(pdfPath))
17+
{
18+
Console.WriteLine($"File not found: {pdfPath}");
19+
return;
20+
}
21+
22+
// Open the encrypted PDF document using the provided password.
23+
using (var pdfDoc = new Document(pdfPath, pdfPassword))
24+
{
25+
// Initialize the PDF converter.
26+
using (var pdfConverter = new PdfConverter(pdfDoc))
27+
{
28+
// Enable barcode optimization for faster rendering.
29+
pdfConverter.RenderingOptions.BarcodeOptimization = true;
30+
31+
// Iterate through each page of the PDF.
32+
for (int pageNumber = 1; pageNumber <= pdfDoc.Pages.Count; pageNumber++)
33+
{
34+
pdfConverter.StartPage = pageNumber;
35+
pdfConverter.EndPage = pageNumber;
36+
pdfConverter.DoConvert();
37+
38+
// Render the current page to an image stream.
39+
using (var pageImageStream = new MemoryStream())
40+
{
41+
pdfConverter.GetNextImage(pageImageStream);
42+
pageImageStream.Position = 0;
43+
44+
// Read barcodes from the rendered image.
45+
using (var reader = new BarCodeReader(pageImageStream, DecodeType.AllSupportedTypes))
46+
{
47+
foreach (var result in reader.ReadBarCodes())
48+
{
49+
Console.WriteLine($"Page {pageNumber}: Type = {result.CodeTypeName}, Text = {result.CodeText}");
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)