Read Barcode from PDF in C#

We can generate and add barcodes to PDF documents as described in my previous post. In certain cases, we may need to detect and read the barcodes embedded into PDF documents programmatically. It helps to decode embedded information in the form of barcodes and QR codes from PDF documents such as invoices, receipts, or reports. In this article, we will learn how to read a barcode from a PDF document using C#.

The article shall cover the following topics:

  1. C# API to Read Barcode from PDF
  2. Read Barcode from PDF
  3. Convert PDF to Image and Read Barcodes
  4. Read Barcodes from PDF using PngDevice
  5. Extract and Read Barcode from PDF
  6. Find and Read Barcode Image from PDF

C# API to Read Barcode from PDF

We will follow a two-step procedure to read barcodes from PDF documents. Firstly, we will be using the Aspose.PDF for .NET API to load a PDF document, then we will render its pages to raster images. After that, we will read the barcode from the rendered images using the Aspose.BarCode for .NET API.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.BarCode
PM> Install-Package Aspose.PDF

Read Barcode from PDF using C#

The Document class of the Aspose.PDF API represents a PDF document. The ConvertToPNGMemoryStream() function of the API renders a PDF page into a PNG memory stream. The BarCodeReader class of the Aspose.BarCode API enables us to perform ReadBarCodes operations to detect barcodes. The BarCodeResult class stores detected barcode information such as barcode type, code text, region and other parameters.

We can read barcode images embedded on any of the pages of the PDF document by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, loop through all the pages and render them to the memory stream.
  3. Then, create an instance of the BarCodeReader class with a stream object.
  4. After that, call the ReadBarCodes() method to get the BarCodeResult object.
  5. Finally, show the barcode information.

The following code example shows how to read a barcode from a PDF document using C#.

Codetext found: Aspose.Barcode Pdf417 Example
Symbology: Pdf417
-------------------------------
Codetext found: Aspose.Barcode QR Example
Symbology: QR
-------------------------------
Codetext found: Aspose.Barcode DataMatrix Example
Symbology: DataMatrix
-------------------------------

Please download the input PDF document with Barcodes used in this blog post.

Convert PDF to Image and Read Barcodes using C#

We can read barcodes from the PDF document by converting PDF pages into images using the PdfConverter class. It allows converting each page of a PDF file to an image, and then we will read the barcode information from the converted image.

We can read barcodes from converted PDF pages by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, create an instance of the PdfConverter class.
  3. Optionally, set rendering options such as BarcodeOptimization.
  4. Then, set the image resolution.
  5. Next, specify StartPage and EndPage to render a range of pages into images.
  6. Then, call the DoConvert() method to render selected pages into images.
  7. Next, save the image to stream in a loop.
  8. Then, create an instance of the BarCodeReader class with the stream object.
  9. After that, call the ReadBarCodes() method to get the BarCodeResult object.
  10. Finally, show the barcode information.

The following code example shows how to convert PDF pages into images and read barcodes using C#.

Read Barcodes from PDF using PngDevice in C#

Here goes another method similar to the previous one. The only difference is that in this method, we will convert the pages of the PDF document into images using the PngDevice class of the API. It allows converting pages of the PDF document into PNG images.

We can read barcodes from converted PDF pages as PNG images by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, create an instance of the PngDevice class.
  3. Then, loop through all the pages and call the Process() method to render into the stream.
  4. Next, create an instance of the BarCodeReader class with the stream object.
  5. After that, call the ReadBarCodes() method to get the BarCodeResult object.
  6. Finally, show the barcode information.

The following code example shows how to convert PDF pages into PNG images and read barcodes using C#.

Extract and Read Barcode from PDF using C#

We can also recognize barcode images embedded on PDF pages using the PdfExtractor class. It allows extracting images from PDF, and then we will read the barcode information from the extracted image.

We can read barcodes from the extracted images by following the steps given below:

  1. Firstly, create an instance of the PdfExtractor class.
  2. Next, bind the input PDF document using the BindPdf() method.
  3. Then, set the page range for image extraction.
  4. Next, call the ExtractImage() method to extract the images.
  5. Then, save the image to stream in a loop.
  6. Next, create an instance of the BarCodeReader class with the stream object.
  7. After that, call the ReadBarCodes() method to get the BarCodeResult object.
  8. Finally, show the barcode information.

The following code example shows how to extract and read a barcode image from a PDF document using C#.

Find and Read Barcode Images from PDF using C#

We can also find and extract barcode images from PDF documents using the ImagePlacementAbsorber class. It represents an absorber object of image placement objects. It performs a search of image usages and provides access to search results via the ImagePlacements collection. This method helps recognize barcodes with original resolution. It may not recognize vector formats correctly.

We can find and read barcodes from the PDF documents by following the steps given below:

  1. Firstly, load a PDF document using the Document class.
  2. Next, create an instance of the ImagePlacementAbsorber class.
  3. Then, call the Visit() method for each page in a loop.
  4. Next, loop through all the images found in the ImagePlacements collection.
  5. Then, save the image to the memory stream.
  6. Next, create an instance of the BarCodeReader class with the stream object.
  7. After that, call the ReadBarCodes() method to get the BarCodeResult object.
  8. Finally, show the barcode information.

The following code example shows how to find and read barcode images from a PDF using C#.

Get a Free License

You can get a free temporary license to try the libraries without evaluation limitations.

Conclusion

In this article, we have learned how to read barcodes from PDF documents in C#. We have also seen various approaches and methods to extract images from a PDF document and decode the embedded barcodes programmatically. Besides, you can learn more about the APIs using the documentation for Aspose.BarCode and the documentation for Aspose.PDF. In case of any ambiguity, please feel free to contact us on our forum.

See Also