We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

barcodeReader.Read() unable to read the barcode from pdf file in attachment

The pdf file in the attachment consist of a barcode which is not getting processed by barcodeReader.Read() function.

Aspose.BarCode Version=6.9.0.0
Aspose.PDF version 11.2.0.0

I checked with online decoder , which gives result as the barcode is valid
Please see screenshot from online barcode reader where its reading the barcode as Code128 and in the exact format expected

Also please check the attached Barcode128_New.pdf for barcode. It will be really helpful if you let us know what’s wrong with this barcode as it is not getting processed & what we need to do to resolve this issue?Barcode128_New.pdf (731.1 KB)
BarcodeDecodeSreenshot.png (131.2 KB)

@pranalisargar,

Thanks for the sample document and screenshot.

Please notice, I am able to reproduce the issue as you mentioned by using the following sample code via Aspose.PDF and Aspose.BarCode for .NET v22.6.0 with your sample document. I found it could not detect the barcode from the extracted image (by Aspose.PDF). Here is the sample code that I am using with your sample PDF document:
e.g.
Sample code:

            string filename = "e:\\test2\\Barcode128_New.pdf";
            try
            {
                // Bind the pdf document
                Aspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor();
                pdfExtractor.BindPdf(filename);

                // Set page range for image extraction
                pdfExtractor.StartPage = 1;
                pdfExtractor.EndPage = 1;


                // Extract the images
                pdfExtractor.ExtractImage();

                // Save images to stream in a loop
                while (pdfExtractor.HasNextImage())
                {

                    // Save image to stream
                    MemoryStream imageStream = new MemoryStream();
                    pdfExtractor.GetNextImage(imageStream);
                    imageStream.Position = 0;
                    int found = 0;

                    // Recognize the barcode from the image stream above
                    using (Aspose.BarCode.BarCodeRecognition.BarCodeReader reader = new Aspose.BarCode.BarCodeRecognition.BarCodeReader(imageStream, Aspose.BarCode.BarCodeRecognition.DecodeType.AllSupportedTypes))
                    {
                        reader.QualitySettings = Aspose.BarCode.BarCodeRecognition.QualitySettings.MaxBarCodes;

                        Console.WriteLine(reader.ReadBarCodes().Length);
                        foreach (Aspose.BarCode.BarCodeRecognition.BarCodeResult barcodeResult in reader.ReadBarCodes())
                        {
                            Console.WriteLine("Codetext found: " + barcodeResult.CodeType + ", Symbology: " + barcodeResult.CodeText);


                        }

                    }
                 }
            }
            catch (Exception ee)
            {
                Console.WriteLine("Error Message: " + ee.Message);
                Console.WriteLine("Error Stack Trace: " + ee.StackTrace);

            } 

I have logged a ticket with an id “BARCODENET-38265” for your issue. We will look into it soon.

Once we have an update on it, we will let you know.

In some cases, it is possible to extract embedded barcode images from the source PDF document and then decode them. This can be done using class ImagePlacementAbsorber. The advantage of this method is that it allows recognizing barcodes with original resolution. The drawback is that barcode images in vector formats cannot be extracted as raster ones. Moreover, there is a risk that a single barcode will be split into several unreadable parts as a result of extraction. This approach is recommended for advanced users of the Aspose library.

Using PdfConverter
This example demonstrates how to read barcodes from a multi-page PDF document using class PdfConverter. Setting resolution to 300 dpi provides the most accurate recognition results.

I tried using ImagePlacementAbsorber class but an exception of type ‘System.NotImplementedException: Not supported image type’ occurred at imagePlacement.Save for a given pdf file as shown in below screenshot,
image.png (60.0 KB)

ImagePlacementAbsorber does not help to recognize the barcode because pdf file does not have any barcode image. The barcode is represented as Vector graphics and it does not count as image.

In this way you have to render pdf page to raster image and then recognize it.

If you want to use ImagePlacementAbsorber with raster images in pdf files, you can use this construction, without image format definition:

imagePlacement.Save(ms);

This code works well

using (Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(@"d:\save\rec\Barcode128_New.pdf"))
{
    Aspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter(pdfDoc);
    pdfConverter.RenderingOptions.BarcodeOptimization = true;

    //set resolution to the page
    //300 dpi is standard resolution
    pdfConverter.Resolution = new Aspose.Pdf.Devices.Resolution(300);

    //set all pages to render into images
    pdfConverter.StartPage = 1;//starts from 1
    pdfConverter.EndPage = pdfConverter.Document.Pages.Count;

    //render selected pages to the images
    pdfConverter.DoConvert();
    while (pdfConverter.HasNextImage())
    {
        //render current page to memory stream as png image
        MemoryStream ms = new MemoryStream();
        pdfConverter.GetNextImage(ms, ImageFormat.Png);
        ms.Position = 0;

        //recognize Pdf417, QR and DataMatrix barcode types from rendered image of the page
        BarCodeReader reader = new BarCodeReader(ms, DecodeType.AllSupportedTypes);
        foreach (BarCodeResult result in reader.ReadBarCodes())
            Console.WriteLine($"Barcode type:{result.CodeTypeName}, Barcode Data:{result.CodeText}");
    }
}