How to read Barcodes from PDF Documents

by Hairil Hasyimi Bin Omar

How to Read Barcode From PDF in C#

  1. Install the barcode library to process barcode files.
  2. Create PdfBarcodeReaderOptions if required.
  3. Use the ReadPdf method from BarcodeReader to read barcodes from PDFs.
  4. Specify additional barcode reading options using BarcodeReaderOption.
  5. Extract barcode values.


<

Read Barcodes from PDF Documents directly

Apart from IronBarcodes ability to read barcodes from images, IronBarcode also takes pride in its ability to read barcodes from PDF documents. This saves users from the hassle of converting PDF documents into images before feeding it into IronBarcode for read. Since PDF documents are more intricate and different from image, a different read method should also be used, and that is BarcodeReader.ReadPdf() method. This method accepts various types of PDF document input, including :

  • byte [] array : PDF document as byte array.
  • IEnumerable: PDF documents as byte arrays stored in a collection.
  • MemoryStream : PDF documents as MemoryStream type.
  • IEnumerable: PDF documents as collection of MemoryStream
  • String : PDF document path string. If the PDF document is already copied into the project, this would be the name of the PDF document in string.
  • IEnumerable: PDF document path/name strings stored in a collection.

Apart from the types of input mentioned above, BarcodeReader.ReadPdf() also accepts PdfBarcodeReaderOptions for a more advanced/improved reading which we will discuss in the next subtopic. Now, let us see the code snippet below that demonstrates the use BarcodeReader.ReadPdf() method to read barcodes in PDF documents.

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-1.cs
using IronBarCode;
using System;
using System.Collections.Generic;

List<String> docs = new List<String>();
docs.Add(@"pdf_a.pdf");
docs.Add(@"pdf_b.pdf");

var myBarcode = BarcodeReader.ReadPdf(docs);   //can also accept individual PDF document file path as argument

foreach (var value in myBarcode)
{
    Console.WriteLine(value.ToString());
}
Imports IronBarCode
Imports System
Imports System.Collections.Generic

Private docs As New List(Of String)()
docs.Add("pdf_a.pdf")
docs.Add("pdf_b.pdf")

Dim myBarcode = BarcodeReader.ReadPdf(docs) 'can also accept individual PDF document file path as argument

For Each value In myBarcode
	Console.WriteLine(value.ToString())
Next value
VB   C#

From the code snippet above, we can see that, to read barcodes from using IronBarcode, we can simply add the file path string of the PDF document into BarcodeReader.ReadPdf() method to read the barcode value, and store the result in a variable. If you wish to print the values of all the barcodes found in the PDF document onto the console, just use foreach loop to iterate and print every element found in the variable by calling ToString() method on them. On top of that, the code snippet above also demonstrate using a collection of PDF document names as an argument in BarcodeReader.ReadPdf() .

But what if the barcodes in the PDF document were unable to be read? What if the performance is so slow? This is where advance PDF barcode reading takes place, in which we will manipulate PdfBarcodeReaderOptions to improve reading quality, accuracy and performance.

Setting PDF Barcode Reader Options

Same as reading barcodes from image, reading barcodes from PDF document also allows users to tweak or adjust the properties in the barcode reader, called PdfBarcodeReaderOptions. Adjusting the properties in PdfBarcodeReaderOptions will greatly help in reading quality, accuracy, and also performance. All the adjustable properties in BarcodeReaderOptions are inherited in PdfBarcodeReaderOptions, with some additional properties for PDF documents. As a start, users can specify the page number or collection of page numbers from the PDF document in which they want the PdfBarcodeReaderOptions to apply, when instantiate a new instance of PdfBarcodeReaderOptions. The code snippet below demonstrates

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-2.cs
using IronBarCode;
using System.Collections.Generic;

List<int> pageNumber = new List<int>() { 1, 2, 3 };

PdfBarcodeReaderOptions PdfOptions = new PdfBarcodeReaderOptions(pageNumber)  // can also use individual page number as argument
{
    // Properties of PDF Barcode reader options
};
Imports IronBarCode
Imports System.Collections.Generic

Private pageNumber As New List(Of Integer)() From {1, 2, 3}

Private PdfOptions As New PdfBarcodeReaderOptions(pageNumber)
VB   C#

Now let us discover the additional properties in PdfBarcodeReaderOptions available to be manipulate, aside from those available in BarcodeReaderOptions

DPI

Users are able to specify the DPI or Dots Per Inch of the barcode image in a PDF document. This will help in reading a low quality barcode image in a PDF document. This property can be set using Integer value.

PageNumbers

If users know beforehand, the page number that contains the barcode that needs to be read in a PDF document, users can specify them in this property. Doing this will greatly improve the reading performance of IronBarcode, especially PDF documents that have many pages, since IronBarcode would not need to read all pages or pages that does not have barcodes to be read. This property is 1-based, meaning the first page of the PDF document is 1 instead of 0.

Password

As the name suggest, this property enable users to work with encrypted PDF files that require password input in order to access the contents of PDF document. Kindly note however, IronBarcode will not be able to give the PDF documents password. This property will accept String input.

Scale

This property enable users to control the scale factor for scaling width and height when converting to Image. This property accepts Integer as value and the default value for this property is 3.5. Setting this property will help in reading small barcodes present in a PDF document as scaling up will zoom the PDF document.

Advanced Barcode Read from PDF Document

Now that we know the properties in PdfBarcodeReaderOptions available for adjustments and tweaks, let us see how to apply them in the project for reading barcodes in PDF document.

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-3.cs
using IronBarCode;
using System;
using System.Collections.Generic;

List<int> pageNumber = new List<int>() { 1, 2, 3 };

PdfBarcodeReaderOptions PdfOptions = new PdfBarcodeReaderOptions(pageNumber)
{
    DPI = 150,
    //PageNumbers = pageNumber,      //this property is not needed if page numbers has been specified as the argument in PdfBarcodeReaderOptions
    Password = "barcode",
    Scale = 3.5,
    //properties below are some of the properties inherited from BarcodeReaderOptions
    Speed = ReadingSpeed.Detailed,
    ExpectBarcodeTypes = BarcodeEncoding.Code93,
    ExpectMultipleBarcodes = true
};

var myBarcode = BarcodeReader.ReadPdf(@"pdf_a_filepath.pdf", PdfOptions);
foreach (var value in myBarcode)
{
    Console.WriteLine(value.ToString());
}
Imports IronBarCode
Imports System
Imports System.Collections.Generic

Private pageNumber As New List(Of Integer)() From {1, 2, 3}

Private PdfOptions As New PdfBarcodeReaderOptions(pageNumber) With {
	.DPI = 150,
	.Password = "barcode",
	.Scale = 3.5,
	.Speed = ReadingSpeed.Detailed,
	.ExpectBarcodeTypes = BarcodeEncoding.Code93,
	.ExpectMultipleBarcodes = True
}

Private myBarcode = BarcodeReader.ReadPdf("pdf_a_filepath.pdf", PdfOptions)
For Each value In myBarcode
	Console.WriteLine(value.ToString())
Next value
VB   C#

The code snippet above demonstrates how to implement PdfBarcodeReaderOptions properties in IronBarcode. The PdfBarcodeReaderOptions first needs to be initialized with a variable name before accessing and adjusting the properties. In the code snippet also, we can see that the list of page number of the PDF document was used as argument when initializing PdfBarcodeReaderOptions. This specifies the page number that we want the settings of PdfBarcodeReader to apply. Users can also specify the PDF page number in the PdfBarcodeReaderOptions property as PageNumbers.

On the other hand, we can also see that we can use properties from BarcodeReaderOptions such as ExpectMultipleBarcodes and ExpectBarcodeTypes in PdfBarcodeReaderOptions since they are inherited from the original class. This will greatly help in overall reading performance and accuracy. To apply the set properties of PdfBarcodeReaderOptions in the barcode read, input the variable name of the PdfBarcodeReaderOptions class we created as the second argument in BarcodeReader.ReadPdf() method, with PDF document to be read file path as the first argument.

Hairil related to Advanced Barcode Read from PDF Document

Hairil Hasyimi Bin Omar

Software Engineer

Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree in Chemical and Process Engineering.