Set PDF Barcode Reader Options
IronBarcode has a variety of options in the PdfBarcodeReaderOptions
class to support customized and optimized reads for PDFs specifically. This will handle PDF passwords, choose which pages to scan, the DPI to parse the PDF in, and any additional scaling needed to improve the read quality.
// Import the IronBarcode library
using IronBarcode;
using System;
using System.Linq;
class Program
{
static void Main()
{
// Define options for reading PDF barcodes
var options = new PdfBarcodeReaderOptions
{
// Set the DPI for parsing the PDF
Dpi = 300,
// Specify which pages of the PDF to scan
PageNumbers = new[] { 1, 2, 3 },
// Set any password required to open the PDF
Password = "password123",
// Specify scaling factor to improve read quality
ScaleToX = 2,
ScaleToY = 2,
};
// Load and read the barcodes from the specified PDF
// The ReadPdfBarcodes method automatically decrypts with the given password
var results = IronBarcode.BarcodeReader.ReadPdfBarcodes("sample.pdf", options);
// Iterate through the decoded barcode results
foreach (var result in results)
{
// Print barcode type and value to the console
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Barcode Value: {result.Text}");
}
}
}
// Import the IronBarcode library
using IronBarcode;
using System;
using System.Linq;
class Program
{
static void Main()
{
// Define options for reading PDF barcodes
var options = new PdfBarcodeReaderOptions
{
// Set the DPI for parsing the PDF
Dpi = 300,
// Specify which pages of the PDF to scan
PageNumbers = new[] { 1, 2, 3 },
// Set any password required to open the PDF
Password = "password123",
// Specify scaling factor to improve read quality
ScaleToX = 2,
ScaleToY = 2,
};
// Load and read the barcodes from the specified PDF
// The ReadPdfBarcodes method automatically decrypts with the given password
var results = IronBarcode.BarcodeReader.ReadPdfBarcodes("sample.pdf", options);
// Iterate through the decoded barcode results
foreach (var result in results)
{
// Print barcode type and value to the console
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Barcode Value: {result.Text}");
}
}
}
' Import the IronBarcode library
Imports IronBarcode
Imports System
Imports System.Linq
Friend Class Program
Shared Sub Main()
' Define options for reading PDF barcodes
Dim options = New PdfBarcodeReaderOptions With {
.Dpi = 300,
.PageNumbers = { 1, 2, 3 },
.Password = "password123",
.ScaleToX = 2,
.ScaleToY = 2
}
' Load and read the barcodes from the specified PDF
' The ReadPdfBarcodes method automatically decrypts with the given password
Dim results = IronBarcode.BarcodeReader.ReadPdfBarcodes("sample.pdf", options)
' Iterate through the decoded barcode results
For Each result In results
' Print barcode type and value to the console
Console.WriteLine($"Barcode Type: {result.BarcodeType}")
Console.WriteLine($"Barcode Value: {result.Text}")
Next result
End Sub
End Class
Explanation of the Code
Using Directives:
using IronBarcode;
andusing System;
are used to include the necessary namespaces for utilizing IronBarcode functionalities and basic input/output operations in C#.
Main Class and Method:
- The
Program
class contains theMain
method, which serves as the entry point of the application.
- The
Creating
PdfBarcodeReaderOptions
:- An instance of
PdfBarcodeReaderOptions
is created to set various options for PDF processing:Dpi
: Sets the resolution for PDF parsing. Higher DPI can improve barcode detection accuracy.PageNumbers
: Specifies the PDF pages that should be scanned. This can be customized to target specific pages.Password
: If the PDF is password-protected, the correct password can be provided using this option.ScaleToX
andScaleToY
: These options allow adjusting the scale of the PDF image to help in enhancing the barcode detection accuracy.
- An instance of
Reading Barcodes:
- The
ReadPdfBarcodes
method is used to read and decode barcodes from the PDF file. It utilizes the options specified.
- The
- Iterating and Displaying Results:
- A
foreach
loop iterates over the barcode results, displaying each barcode's type and value on the console. - This ensures that all detected barcodes from the specified pages are outputted for verification or further processing.
- A