How to Read Barcodes From PDFs in C#

In this tutorial, we explore how to read barcodes in PDF documents using the Iron Barcode library from Iron Software. Assuming you have Iron Barcode installed, we begin by including its namespace in our program. We then create a list of integers to indicate the PDF pages we wish to scan for barcodes, selecting pages one, two, and three. Next, we instantiate the PDFBarcodeReaderOptions class, passing our page list to the constructor while setting the DPI to 150 to adjust resolution and the scale to 3.5 for barcode size. Other settings include setting the reading speed to 'detailed' for precision, specifying expected barcode types as QR codes, and allowing detection of multiple barcodes within the PDF. To perform the barcode reading, we use the ReadPDF method, providing the path to the PDF and our configured options. The program loops through the detected barcodes, printing each unique barcode to the console. This feature is highly useful for efficiently extracting barcode data from PDF documents. The tutorial concludes with an encouragement to explore Iron Software's trial subscription for further insights.

using IronBarCode; // Include the Iron Barcode library

// Create a list to specify the pages of the PDF we want to scan
var pagesToScan = new List<int> { 1, 2, 3 };

// Configure the options for reading barcodes from the PDF
var options = new PDFBarcodeReaderOptions()
{
    Pages = pagesToScan,           // Set pages to scan
    DPI = 150,                     // Set dots per inch for image resolution
    BarcodeScale = 3.5,            // Set the scale to improve barcode recognition
    ReadingSpeed = ReadingSpeed.Detailed, // Use detailed reading for higher accuracy
    ExpectedBarcodes = BarcodeEncoding.QR, // Specify the type of barcode to detect
    ReadMultibarcodeTypes = true   // Allow detection of multiple barcode types
};

// Path to the PDF file
string pdfPath = "path/to/your/pdf/document.pdf";

// Detect and read barcodes from the PDF
BarcodeResult[] results = BarcodeReader.ReadPDF(pdfPath, options);

// Loop through and print each detected barcode
foreach (var result in results)
{
    Console.WriteLine($"Detected Barcode: {result.Text}"); // Output barcode text to console
}
using IronBarCode; // Include the Iron Barcode library

// Create a list to specify the pages of the PDF we want to scan
var pagesToScan = new List<int> { 1, 2, 3 };

// Configure the options for reading barcodes from the PDF
var options = new PDFBarcodeReaderOptions()
{
    Pages = pagesToScan,           // Set pages to scan
    DPI = 150,                     // Set dots per inch for image resolution
    BarcodeScale = 3.5,            // Set the scale to improve barcode recognition
    ReadingSpeed = ReadingSpeed.Detailed, // Use detailed reading for higher accuracy
    ExpectedBarcodes = BarcodeEncoding.QR, // Specify the type of barcode to detect
    ReadMultibarcodeTypes = true   // Allow detection of multiple barcode types
};

// Path to the PDF file
string pdfPath = "path/to/your/pdf/document.pdf";

// Detect and read barcodes from the PDF
BarcodeResult[] results = BarcodeReader.ReadPDF(pdfPath, options);

// Loop through and print each detected barcode
foreach (var result in results)
{
    Console.WriteLine($"Detected Barcode: {result.Text}"); // Output barcode text to console
}
Imports IronBarCode ' Include the Iron Barcode library

' Create a list to specify the pages of the PDF we want to scan
Private pagesToScan = New List(Of Integer) From {1, 2, 3}

' Configure the options for reading barcodes from the PDF
Private options = New PDFBarcodeReaderOptions() With {
	.Pages = pagesToScan,
	.DPI = 150,
	.BarcodeScale = 3.5,
	.ReadingSpeed = ReadingSpeed.Detailed,
	.ExpectedBarcodes = BarcodeEncoding.QR,
	.ReadMultibarcodeTypes = True
}

' Path to the PDF file
Private pdfPath As String = "path/to/your/pdf/document.pdf"

' Detect and read barcodes from the PDF
Private results() As BarcodeResult = BarcodeReader.ReadPDF(pdfPath, options)

' Loop through and print each detected barcode
For Each result In results
	Console.WriteLine($"Detected Barcode: {result.Text}") ' Output barcode text to console
Next result
$vbLabelText   $csharpLabel

Further Reading: How to read Barcodes from PDF Documents

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Read Barcodes From Streams in C#
NEXT >
How to Read Barcodes From Images in C#