Live Chat

Recognize Barcode from Pdf Document

Skip to end of metadata
Go to start of metadata
In this article, we will recognize barcode from a pdf document using Aspose.BarCode for .NET and Aspose.Pdf for .NET.

We will follow the below steps for reading barcode from the pdf document:

  1. Extract image from pdf document using Aspose.Pdf.Kit for .NET
  2. Save the extracted image as stream
  3. Pass the image in form of stream to Aspose.BarCode for .NET
  4. Read barcode from the image

Use PdfExtractor class and bind it to the pdf document using PdfExtractor.BindPdf() method. Specify start and end page numbers and then call PdfExtractor.ExtractImage() method to get the images for the mentioned pages.

In a while loop, call PdfExtractor.GetNextImage() method and save the image to stream. Initialize Aspose.BarCodeRecognition.BarCodeReader class with the stream and symbology type and then call BarCodeReader.Read() method to read the barcodes from the stream (image).

The sample code snippet is given below:

[C#]
try
{
    // set the license for Aspose.BarCode for .NET and Aspose.Pdf.Kit for .NET components
    Aspose.BarCodeRecognition.License licenceBarCodeRecognition = new Aspose.BarCodeRecognition.License();
    licenceBarCodeRecognition.SetLicense(@"Aspose.Total.lic");

    Aspose.Pdf.Kit.License licensePdfKit = new Aspose.Pdf.Kit.License();
    licensePdfKit.SetLicense(@"Aspose.Total.lic");

    // bind the pdf document
    PdfExtractor pdfExtractor = new PdfExtractor();
    pdfExtractor.BindPdf(@"document.pdf");
    // set page range for image extraction
    pdfExtractor.StartPage = 1;
    pdfExtractor.EndPage = 1;
    // extract the images
    Console.WriteLine("Extracting images.....");
    pdfExtractor.ExtractImage();
    // save images to stream in a loop
    while (pdfExtractor.HasNextImage())
    {
        Console.WriteLine("Getting next image....");
        // save image to stream
        MemoryStream imageStream = new MemoryStream();
        pdfExtractor.GetNextImage(imageStream);
        imageStream.Position = 0;

        Console.WriteLine("Recognizing barcode....");
        // recognize the barcode from the image stream above
        BarCodeReader barcodeReader = new BarCodeReader(imageStream, BarCodeReadType.Code39Standard);
        while(barcodeReader.Read())
        {
            Console.WriteLine("Codetext found: " + barcodeReader.GetCodeText() + ", Symbology: " + barcodeReader.GetReadType().ToString());
        }
        // close the reader
        barcodeReader.Close();
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
 
[VB.NET]
Try
   ' set the license for Aspose.BarCode for .NET and Aspose.Pdf.Kit for .NET components
   Dim licenceBarCodeRecognition As Aspose.BarCodeRecognition.License = New Aspose.BarCodeRecognition.License()
   licenceBarCodeRecognition.SetLicense("Aspose.Total.lic")

   Dim licensePdfKit As Aspose.Pdf.Kit.License = New Aspose.Pdf.Kit.License()
	licensePdfKit.SetLicense("Aspose.Total.lic")

   ' bind the pdf document
   Dim pdfExtractor As PdfExtractor = New PdfExtractor()
   pdfExtractor.BindPdf("document.pdf")
   ' set page range for image extraction
   pdfExtractor.StartPage = 1
   pdfExtractor.EndPage = 1
   ' extract the images
   Console.WriteLine("Extracting images.....")
   pdfExtractor.ExtractImage()
   ' save images to stream in a loop
   Do While pdfExtractor.HasNextImage()
      Console.WriteLine("Getting next image....")
      ' save image to stream
      Dim imageStream As MemoryStream = New MemoryStream()
      pdfExtractor.GetNextImage(imageStream)
      imageStream.Position = 0

      Console.WriteLine("Recognizing barcode....")
      ' recognize the barcode from the image stream above
      Dim barcodeReader As BarCodeReader = New BarCodeReader(imageStream, BarCodeReadType.Code39Standard)
      Do While barcodeReader.Read()
         Console.WriteLine("Codetext found: " & barcodeReader.GetCodeText() & ", Symbology: " & barcodeReader.GetReadType().ToString())
      Loop
      ' close the reader
      barcodeReader.Close()
   Loop
Catch ex As Exception
   Console.WriteLine(ex.Message)
End Try
 
The evaluation version of Aspose.BarCode for .NET can only recognize Code39 barcodes. If the image contains barcode of other than Code39 symbology type, a valid license must be set. For getting temporary license for 30 days, please visit http://www.aspose.com/corporate/purchase/temporary-license.aspx for more details.
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.