Get started with IronOCR

Start using IronOCR in your project today with a free trial.

First Step:
green arrow pointer



Read Barcode Example

Construct the IronTesseract object to perform the reading. Enable barcode reading by setting the ReadBarCodes property to true. Import the PDF document by passing it into the OcrPdfInput constructor. Then, use the Read method to perform OCR on the imported PDF document.

Now, let's perform OCR on the following PDF document:

:path=/static-assets/ocr/content-code-examples/how-to/barcodes-read-barcodes.cs
using IronOcr;
using System;

// This code snippet demonstrates how to use IronTesseract to perform Optical Character Recognition (OCR)
// on a PDF document and extract text along with any barcodes present.

// Instantiate IronTesseract for performing OCR
IronTesseract ocrTesseract = new IronTesseract();

// Enable barcode reading in the configuration
ocrTesseract.Configuration.ReadBarCodes = true;

// Load the PDF document that contains text and potentially barcodes.
// It's important to dispose of OcrPdfInput after use, hence the use of 'using'.
using (var pdfInput = new OcrPdfInput("pdfWithBarcodes.pdf"))
{
    // Perform OCR on the PDF input
    OcrResult ocrResult = ocrTesseract.Read(pdfInput);

    // Output the extracted text from the PDF
    Console.WriteLine("Extracted text:");
    Console.WriteLine(ocrResult.Text);

    // Output all detected barcodes in the PDF
    Console.WriteLine("Extracted barcodes:");
    foreach (var barcode in ocrResult.Barcodes)
    {
        Console.WriteLine(barcode.Value);
    }
}
Imports IronOcr

Imports System



' This code snippet demonstrates how to use IronTesseract to perform Optical Character Recognition (OCR)

' on a PDF document and extract text along with any barcodes present.



' Instantiate IronTesseract for performing OCR

Private ocrTesseract As New IronTesseract()



' Enable barcode reading in the configuration

ocrTesseract.Configuration.ReadBarCodes = True



' Load the PDF document that contains text and potentially barcodes.

' It's important to dispose of OcrPdfInput after use, hence the use of 'using'.

Using pdfInput = New OcrPdfInput("pdfWithBarcodes.pdf")

	' Perform OCR on the PDF input

	Dim ocrResult As OcrResult = ocrTesseract.Read(pdfInput)



	' Output the extracted text from the PDF

	Console.WriteLine("Extracted text:")

	Console.WriteLine(ocrResult.Text)



	' Output all detected barcodes in the PDF

	Console.WriteLine("Extracted barcodes:")

	For Each barcode In ocrResult.Barcodes

		Console.WriteLine(barcode.Value)

	Next barcode

End Using
$vbLabelText   $csharpLabel
Reading result

As you can see, multiple barcode values that are also included in the extracted text are displayed below the barcodes.

Read QR Code Example

Similar to reading a barcode, the ReadBarCodes property must be set to true. Besides changing the file path, no other changes are necessary in the code. Now, let's perform OCR on the PDF document that has QR codes:

:path=/static-assets/ocr/content-code-examples/how-to/barcodes-read-qr-codes.cs
using IronOcr;
using System;

// The IronTesseract class is part of the IronOcr library which provides OCR capabilities.
// This code demonstrates how to use IronTesseract to read text and barcodes from a PDF file.

class Program
{
    static void Main()
    {
        // Instantiate IronTesseract - this is the engine that performs the OCR operation.
        var ocrTesseract = new IronTesseract();

        // Enable barcode reading by modifying the Configuration property of the IronTesseract instance.
        // This allows the OCR engine to also detect and read barcodes in the PDF.
        ocrTesseract.Configuration.ReadBarCodes = true;

        // Define an input PDF file that contains QR codes or barcodes.
        // OcrPdfInput is a class that handles PDF requests for IronTesseract.
        using (var imageInput = new OcrPdfInput("pdfWithQrCodes.pdf"))
        {
            try
            {
                // Perform OCR on the provided PDF file.
                // This reads the text and barcodes from the PDF and stores the result in an OcrResult object.
                OcrResult ocrResult = ocrTesseract.Read(imageInput);

                // Output the extracted text from the PDF.
                Console.WriteLine("Extracted text:");
                Console.WriteLine(ocrResult.Text);

                // Output the barcodes detected in the PDF.
                Console.WriteLine("Extracted barcodes:");
                foreach (var barcode in ocrResult.Barcodes)
                {
                    // Each 'barcode' object contains properties such as 'Value' which represents the decoded barcode text.
                    Console.WriteLine(barcode.Value);
                }
            }
            catch (Exception ex)
            {
                // If an error occurs during the OCR process, output the exception message.
                Console.WriteLine("Error during OCR process: " + ex.Message);
            }
        }
    }
}
Imports IronOcr

Imports System



' The IronTesseract class is part of the IronOcr library which provides OCR capabilities.

' This code demonstrates how to use IronTesseract to read text and barcodes from a PDF file.



Friend Class Program

	Shared Sub Main()

		' Instantiate IronTesseract - this is the engine that performs the OCR operation.

		Dim ocrTesseract = New IronTesseract()



		' Enable barcode reading by modifying the Configuration property of the IronTesseract instance.

		' This allows the OCR engine to also detect and read barcodes in the PDF.

		ocrTesseract.Configuration.ReadBarCodes = True



		' Define an input PDF file that contains QR codes or barcodes.

		' OcrPdfInput is a class that handles PDF requests for IronTesseract.

		Using imageInput = New OcrPdfInput("pdfWithQrCodes.pdf")

			Try

				' Perform OCR on the provided PDF file.

				' This reads the text and barcodes from the PDF and stores the result in an OcrResult object.

				Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)



				' Output the extracted text from the PDF.

				Console.WriteLine("Extracted text:")

				Console.WriteLine(ocrResult.Text)



				' Output the barcodes detected in the PDF.

				Console.WriteLine("Extracted barcodes:")

				For Each barcode In ocrResult.Barcodes

					' Each 'barcode' object contains properties such as 'Value' which represents the decoded barcode text.

					Console.WriteLine(barcode.Value)

				Next barcode

			Catch ex As Exception

				' If an error occurs during the OCR process, output the exception message.

				Console.WriteLine("Error during OCR process: " & ex.Message)

			End Try

		End Using

	End Sub

End Class
$vbLabelText   $csharpLabel
Reading result

Frequently Asked Questions

What is IronOCR?

IronOCR is a C# library that allows developers to read and decode barcodes and QR codes using OCR technology. It can be downloaded from NuGet.

How do I set up IronOCR for barcode and QR code reading?

To set up IronOCR, download and set up a trial license from the NuGet website. Then, enable barcode reading by setting the ReadBarCodes property to true.

How can I perform OCR on a PDF document with barcodes?

Construct the IronTesseract object, enable barcode reading, import the PDF document using the OcrPdfInput constructor, and use the Read method to perform OCR. Output the detected text and barcode values from the result.

Is there a difference between reading barcodes and QR codes with IronOCR?

There is no difference in the process; both functionalities require setting the ReadBarCodes property to true. The file path is the only change needed between reading barcodes and QR codes.

What types of documents can IronOCR process?

IronOCR can process both printed and digital documents, allowing for automation and data extraction from a wide range of sources.

Can IronOCR output both text and barcode values?

Yes, IronOCR can output both the detected text and barcode values from the documents it processes.

What is the ReadBarCodes property in IronOCR?

The ReadBarCodes property in IronOCR is a setting that, when set to true, enables the library to read and decode barcodes and QR codes from documents.

How do I import a PDF document in IronOCR?

To import a PDF document in IronOCR, pass the document into the OcrPdfInput constructor before using the Read method to perform OCR.

Chaknith related to Read QR Code Example
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.