Read Barcode from PDF in C#

We can generate and add barcodes to PDF documents as described in my previous post. Sometimes you need to detect and read those barcodes programmatically, for example from invoices, receipts, or reports. This article shows how to read a barcode from a PDF document using C#.

The article covers these 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 process. First, use the Aspose.PDF for .NET API to load a PDF and render its pages to raster images. Then, use the Aspose.BarCode for .NET API to read barcodes from those images.

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 represents a PDF file. Its ConvertToPNGMemoryStream() method renders a page to a PNG memory stream. The BarCodeReader class performs ReadBarCodes operations, and the BarCodeResult class stores the detected barcode type, text, region, and other details.

Steps to read barcodes from any PDF page:

  1. Load the PDF with the Document class.
  2. Loop through all pages and render each to a memory stream.
  3. Create a BarCodeReader instance using the stream.
  4. Call ReadBarCodes() to obtain BarCodeResult objects.
  5. Display the barcode information.

The code example below demonstrates 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#

You can also read barcodes by converting PDF pages to images with the PdfConverter class, then scanning the images.

Steps:

  1. Load the PDF with the Document class.
  2. Create a PdfConverter instance.
  3. (Optional) Set rendering options such as BarcodeOptimization.
  4. Set the image resolution.
  5. Define StartPage and EndPage to limit the conversion range.
  6. Call DoConvert() to render the selected pages.
  7. Save each image to a stream in a loop.
  8. Create a BarCodeReader with the stream.
  9. Call ReadBarCodes() to get BarCodeResult objects.
  10. Display the barcode information.

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

Read Barcodes from PDF using PngDevice in C#

This method is similar to the previous one but uses the PngDevice class to render PDF pages directly to PNG images.

Steps:

  1. Load the PDF with the Document class.
  2. Create a PngDevice instance.
  3. Loop through all pages and call Process() to render each page to a stream.
  4. Create a BarCodeReader with the stream.
  5. Call ReadBarCodes() to obtain BarCodeResult objects.
  6. Display the barcode information.

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

Extract and Read Barcode from PDF using C#

The PdfExtractor class can extract images from a PDF, after which you can read any barcode present in those images.

Steps:

  1. Create a PdfExtractor instance.
  2. Bind the input PDF with BindPdf().
  3. Set the page range for extraction.
  4. Call ExtractImage() to pull out the images.
  5. Save each image to a stream in a loop.
  6. Create a BarCodeReader with the stream.
  7. Call ReadBarCodes() to get BarCodeResult objects.
  8. Display the barcode information.

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

Find and Read Barcode Images from PDF using C#

The ImagePlacementAbsorber class searches for image placements in a PDF and provides access to them via the ImagePlacements collection. This method preserves the original resolution of barcode images, though it may not handle vector formats perfectly.

Steps:

  1. Load the PDF with the Document class.
  2. Create an ImagePlacementAbsorber instance.
  3. Call Visit() for each page.
  4. Iterate through the ImagePlacements collection.
  5. Save each found image to a memory stream.
  6. Create a BarCodeReader with the stream.
  7. Call ReadBarCodes() to obtain BarCodeResult objects.
  8. Display the barcode information.

The code example below demonstrates 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 covered how to read barcodes from PDF documents in C#. We explored several approaches to extract images from a PDF and decode embedded barcodes programmatically. For more details, refer to the documentation for Aspose.BarCode and the documentation for Aspose.PDF. If you have questions, feel free to post on our forum.

See Also