-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeasure-memory-consumption-while-recognizing-barcodes-in-large-multi-page-pdf-document.cs
More file actions
84 lines (73 loc) · 3.51 KB
/
Copy pathmeasure-memory-consumption-while-recognizing-barcodes-in-large-multi-page-pdf-document.cs
File metadata and controls
84 lines (73 loc) · 3.51 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
80
81
82
83
84
using System;
using System.IO;
using System.Diagnostics;
using Aspose.BarCode;
using Aspose.BarCode.BarCodeRecognition;
using Aspose.Pdf;
using Aspose.Pdf.Facades;
/// <summary>
/// Demonstrates barcode recognition in PDF pages using Aspose libraries.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Accepts an optional PDF file path argument, processes up to four pages,
/// converts each page to an image, and reads any barcodes present.
/// </summary>
/// <param name="args">Command‑line arguments; first argument may be a PDF file path.</param>
static void Main(string[] args)
{
// Determine PDF file path: use first argument if supplied, otherwise default to "sample.pdf".
string pdfPath = args.Length > 0 ? args[0] : "sample.pdf";
// Verify that the file exists before proceeding.
if (!File.Exists(pdfPath))
{
Console.WriteLine($"File not found: {pdfPath}");
return;
}
// Open the PDF document within a using block to ensure proper disposal.
using (var pdfDoc = new Document(pdfPath))
{
// Process a maximum of four pages to stay within evaluation limits.
int pagesToProcess = Math.Min(pdfDoc.Pages.Count, 4);
// Iterate over each page to be processed.
for (int page = 1; page <= pagesToProcess; page++)
{
// Create a PdfConverter for the current page.
using (var pdfConverter = new PdfConverter(pdfDoc))
{
// Enable barcode optimization for better recognition performance.
pdfConverter.RenderingOptions.BarcodeOptimization = true;
// Restrict conversion to the current page only.
pdfConverter.StartPage = page;
pdfConverter.EndPage = page;
// Perform the conversion.
pdfConverter.DoConvert();
// Store the resulting image in a memory stream.
using (var imageStream = new MemoryStream())
{
pdfConverter.GetNextImage(imageStream);
imageStream.Position = 0; // Reset stream position for reading.
// Capture memory usage before barcode recognition.
long memoryBefore = Process.GetCurrentProcess().PrivateMemorySize64;
// Initialize the barcode reader for all supported types.
using (var reader = new BarCodeReader(imageStream, DecodeType.AllSupportedTypes))
{
// Enumerate and output each detected barcode.
foreach (var result in reader.ReadBarCodes())
{
Console.WriteLine($"Page {page}: Type={result.CodeTypeName}, Text={result.CodeText}");
}
}
// Capture memory usage after barcode recognition.
long memoryAfter = Process.GetCurrentProcess().PrivateMemorySize64;
long diffKB = (memoryAfter - memoryBefore) / 1024;
// Output memory consumption details.
Console.WriteLine($"Page {page}: Memory before={memoryBefore / 1024} KB, after={memoryAfter / 1024} KB, diff={diffKB} KB");
}
}
}
}
}
}