How to Read Barcodes and QR Codes Using OCR in C#

In this tutorial, we delve into the process of reading barcodes and QR codes from PDF documents using Iron OCR in C#. The tutorial begins with the installation of Iron OCR in the project using a package manager. We then proceed to import Iron OCR and set up the necessary configurations, including the license key, to unlock the library's full features. The tutorial explains how to initialize the Iron Tesseract OCR engine and configure it to read barcodes using the OCR PDF input class. By loading a PDF document containing barcodes, the OCR extracts and prints the barcode data in the console. Similarly, the tutorial covers the extraction of QR codes from another PDF document. The step-by-step guide illustrates how Iron OCR simplifies the integration of barcode and QR code reading functionalities into C# applications. The tutorial concludes by encouraging developers to explore the capabilities of Iron OCR by signing up for a trial and downloading the package. This resource is ideal for developers seeking to enhance their applications with robust code reading features.

// This code sample demonstrates the process of installing and using Iron OCR to read barcodes and QR codes from PDFs in C#.

// First, install Iron OCR via NuGet Package Manager:
// Use the Package Manager Console and run the following command:
// Install-Package IronOcr

// Import the IronOCR namespace
using IronOcr;

class Program
{
    static void Main()
    {
        // Set the license key for Iron OCR
        var Ocr = new IronTesseract();

        // Initialize the OCR engine
        // Setting the language to English
        Ocr.Language = OcrLanguage.English;

        // Load a PDF document and automatically recognize all barcodes and QR codes
        using (var input = new OcrInput())
        {
            // Load the PDF with the path to the document containing barcodes
            input.AddPdf("path_to_barcode_pdf.pdf");

            // Extract barcode content
            var results = Ocr.Read(input);

            // Print all recognized barcode and QR code data
            foreach (var barcode in results.Barcodes)
            {
                Console.WriteLine($"Type: {barcode.Type}, Value: {barcode.Value}");
            }
        }

        // Similarly, to extract QR codes from another PDF document, perform the following:
        using (var qrInput = new OcrInput())
        {
            // Load the PDF with the path to the document containing QR codes
            qrInput.AddPdf("path_to_qr_pdf.pdf");

            // Extract QR code content
            var qrResults = Ocr.Read(qrInput);

            // Print all recognized QR code data
            foreach (var qr in qrResults.Barcodes)
            {
                Console.WriteLine($"Type: {qr.Type}, Value: {qr.Value}");
            }
        }
    }
}
// This code sample demonstrates the process of installing and using Iron OCR to read barcodes and QR codes from PDFs in C#.

// First, install Iron OCR via NuGet Package Manager:
// Use the Package Manager Console and run the following command:
// Install-Package IronOcr

// Import the IronOCR namespace
using IronOcr;

class Program
{
    static void Main()
    {
        // Set the license key for Iron OCR
        var Ocr = new IronTesseract();

        // Initialize the OCR engine
        // Setting the language to English
        Ocr.Language = OcrLanguage.English;

        // Load a PDF document and automatically recognize all barcodes and QR codes
        using (var input = new OcrInput())
        {
            // Load the PDF with the path to the document containing barcodes
            input.AddPdf("path_to_barcode_pdf.pdf");

            // Extract barcode content
            var results = Ocr.Read(input);

            // Print all recognized barcode and QR code data
            foreach (var barcode in results.Barcodes)
            {
                Console.WriteLine($"Type: {barcode.Type}, Value: {barcode.Value}");
            }
        }

        // Similarly, to extract QR codes from another PDF document, perform the following:
        using (var qrInput = new OcrInput())
        {
            // Load the PDF with the path to the document containing QR codes
            qrInput.AddPdf("path_to_qr_pdf.pdf");

            // Extract QR code content
            var qrResults = Ocr.Read(qrInput);

            // Print all recognized QR code data
            foreach (var qr in qrResults.Barcodes)
            {
                Console.WriteLine($"Type: {qr.Type}, Value: {qr.Value}");
            }
        }
    }
}
' This code sample demonstrates the process of installing and using Iron OCR to read barcodes and QR codes from PDFs in C#.

' First, install Iron OCR via NuGet Package Manager:
' Use the Package Manager Console and run the following command:
' Install-Package IronOcr

' Import the IronOCR namespace
Imports IronOcr

Friend Class Program
	Shared Sub Main()
		' Set the license key for Iron OCR
		Dim Ocr = New IronTesseract()

		' Initialize the OCR engine
		' Setting the language to English
		Ocr.Language = OcrLanguage.English

		' Load a PDF document and automatically recognize all barcodes and QR codes
		Using input = New OcrInput()
			' Load the PDF with the path to the document containing barcodes
			input.AddPdf("path_to_barcode_pdf.pdf")

			' Extract barcode content
			Dim results = Ocr.Read(input)

			' Print all recognized barcode and QR code data
			For Each barcode In results.Barcodes
				Console.WriteLine($"Type: {barcode.Type}, Value: {barcode.Value}")
			Next barcode
		End Using

		' Similarly, to extract QR codes from another PDF document, perform the following:
		Using qrInput = New OcrInput()
			' Load the PDF with the path to the document containing QR codes
			qrInput.AddPdf("path_to_qr_pdf.pdf")

			' Extract QR code content
			Dim qrResults = Ocr.Read(qrInput)

			' Print all recognized QR code data
			For Each qr In qrResults.Barcodes
				Console.WriteLine($"Type: {qr.Type}, Value: {qr.Value}")
			Next qr
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: How to Read Barcodes and QR Codes

Kannaopat Udonpant
Software Engineer
Before becoming a Software Engineer, Kannapat completed a Environmental Resources PhD from Hokkaido University in Japan. While pursuing his degree, Kannapat also became a member of the Vehicle Robotics Laboratory, which is part of the Department of Bioproduction Engineering. In 2022, he leveraged his C# skills to join Iron Software's engineering team, where he focuses on IronPDF. Kannapat values his job because he learns directly from the developer who writes most of the code used in IronPDF. In addition to peer learning, Kannapat enjoys the social aspect of working at Iron Software. When he's not writing code or documentation, Kannapat can usually be found gaming on his PS5 or rewatching The Last of Us.
< PREVIOUS
How to use Computer Vision to Find Text in C#
NEXT >
How to use Tesseract OCR for .NET on Windows