Issue with Barcode Reading from pdf document through Aspose.BarCode (23.8.1.0)

Hi Support Team,

Currently, we are working on a project where we need to read barcodes from PDF documents. Unfortunately, despite our best efforts and thorough implementation, we have not been successful in reading barcodes from PDF files using Aspose.BarCode. Please find the attached pdf file for your reference.

00000002.PDF (8.9 KB)

However, we have successfully able to read barcode reading from PDF documents using the ActivePDF Toolkit library. This indicates that the PDF documents themselves are not the issue, but rather there may be a problem or limitation with Aspose.BarCode when it comes to PDF barcode reading.

We followed the below link to read the barcode from pdf document.

Code

using (Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(inputPath))
{
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, System.Drawing.Imaging.ImageFormat.Png);
                        ms.Position = 0;

                        //recognize Pdf417, QR and DataMatrix barcode types from rendered image of the page
                        BarCodeReader reader = new BarCodeReader(ms, DecodeType.Pdf417, DecodeType.QR, DecodeType.DataMatrix);
                        foreach (BarCodeResult result in reader.ReadBarCodes())
                            MessageBox.Show("BarCode CodeText: " + result.CodeText);
                    }
                }

We have already replaced all other functionalities with Aspose libraries, and this barcode reading issue is the only obstacle we are facing. Given our preference to standardize our implementation with Aspose, we are keen to resolve this issue and ensure that Aspose.BarCode meets our requirements for barcode reading from PDF files.

We kindly request your assistance in investigating and resolving this matter. If there are any specific configurations, workarounds, or updates that can address this issue, we would greatly appreciate your guidance.

Thanks,
Saurabh

@sranjan50,

I checked your PDF file which has barcode with poor quality. I think you may try using MaxBarCodes mode to decode barcodes that otherwise are unreadable, see the documents with example codes on how you can adopt some measures and presets to improve recognition for your reference.
https://docs.aspose.com/barcode/net/recognition-modes-and-presets/#preset-maxbarcodes-for-debugging
https://docs.aspose.com/barcode/net/recognition-specifics/

Hope, this helps a bit.

Hi @amjad.sahi,

After meticulously reviewing the reference you mentioned and trying each of suggested methods, we regret to inform you that still we are facing same issue i.e. Parameter is not valid.

We have attempted multiple approaches, but unfortunately, result remains the same.

We kindly request your assistance in conducting in-more depth analysis to identify the root cause of problem and appropriate solution for this.

Please find the below approaches performed to achieve this.

Approach 1:
using (BarCodeReader read = new BarCodeReader(inputPath, DecodeType.Code39Standard, DecodeType.Code39Extended))
{
read.QualitySettings.AllowSaltAndPaperFiltering = true;
MessageBox.Show($“Barcodes read: {read.ReadBarCodes().Length}”);
foreach (BarCodeResult result in read.FoundBarCodes)
MessageBox.Show($"{result.CodeTypeName}:{result.CodeText}");
}

Approach 2:
using (BarCodeReader read = new BarCodeReader(inputPath, DecodeType.Code39Standard, DecodeType.Code39Extended))
{
read.QualitySettings.AllowSaltAndPaperFiltering = false;
MessageBox.Show($“Barcodes read: {read.ReadBarCodes().Length}”);
foreach (BarCodeResult result in read.FoundBarCodes)
MessageBox.Show($"{result.CodeTypeName}:{result.CodeText}");
}

Approach 3:
using (BarCodeReader read = new BarCodeReader(inputPath, DecodeType.Code128,
DecodeType.Code39Extended, DecodeType.Planet, DecodeType.QR,
DecodeType.MicroQR, DecodeType.Pdf417, DecodeType.DataMatrix, DecodeType.Aztec))
{
read.QualitySettings = QualitySettings.MaxBarCodes;
foreach (BarCodeResult result in read.FoundBarCodes)
MessageBox.Show("BarCode CodeText: " + result.CodeText);
}

Approach 4:
using (Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(inputPath))
{
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, System.Drawing.Imaging.ImageFormat.Png);
                        ms.Position = 0;

                        //recognize Pdf417, QR and DataMatrix barcode types from rendered image of the page
                        BarCodeReader reader = new BarCodeReader(ms, DecodeType.Pdf417, DecodeType.QR, DecodeType.DataMatrix);
                        foreach (BarCodeResult result in reader.ReadBarCodes())
                            MessageBox.Show("BarCode CodeText: " + result.CodeText);
                    }
                }

Approach 5:
using (Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(inputPath))
{
//process all PDF pages in the document, page numeration starts from 1
for (int i = 1; i <= pdfDoc.Pages.Count; ++i)
{
//create an image extractor and bind it to the page
Aspose.Pdf.ImagePlacementAbsorber imagePlacementAbsorber = new Aspose.Pdf.ImagePlacementAbsorber();
imagePlacementAbsorber.Visit(pdfDoc.Pages[i]);

                        //extract all images from the PDF page
                        foreach (Aspose.Pdf.ImagePlacement imagePlacement in imagePlacementAbsorber.ImagePlacements)
                        {
                            //convert an image from the pdf page to the stream
                            MemoryStream ms = new MemoryStream();
                            imagePlacement.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                            ms.Position = 0;

                            //recognize Pdf417, QR, and DataMatrix barcode types from extracted images from the page
                            BarCodeReader reader = new BarCodeReader(ms, DecodeType.Pdf417, DecodeType.QR, DecodeType.DataMatrix);
                            foreach (BarCodeResult result in reader.ReadBarCodes())
                                MessageBox.Show("BarCode CodeText: " + result.CodeText);
                        }
                    }
                }

Approach 6:
using (Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(inputPath))
{
//create png device with 300 dpi standard resolution
Aspose.Pdf.Devices.PngDevice pngDevice = new Aspose.Pdf.Devices.PngDevice(new Aspose.Pdf.Devices.Resolution(300));

                    //proceed all pdf pages, pages start from 1
                    for (int i = 1; i <= pdfDoc.Pages.Count; ++i)
                    {
                        //render pdf page to the stream
                        MemoryStream ms = new MemoryStream();
                        pngDevice.Process(pdfDoc.Pages[i], ms);
                        ms.Position = 0;

                        //recognize Pdf417, QR and DataMatrix barcode types from rendered image of the page
                        BarCodeReader reader = new BarCodeReader(ms, DecodeType.Pdf417, DecodeType.QR, DecodeType.DataMatrix);
                        foreach (BarCodeResult result in reader.ReadBarCodes())
                            MessageBox.Show("BarCode CodeText: " + result.CodeText);
                    }
                }

Approach 7:
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(inputPath);
TextAbsorber textAbsorber = new TextAbsorber();
pdfDocument.Pages.Accept(textAbsorber);
string pdfText = textAbsorber.Text;

                Bitmap bitmap = new Bitmap(1, 1);

                Graphics graphics = Graphics.FromImage(bitmap);
                SizeF size = graphics.MeasureString(pdfText, new System.Drawing.Font("Arial", 12)); // Adjust font and size as needed
                bitmap.Dispose();

                bitmap = new Bitmap((int)size.Width, (int)size.Height);
                graphics = Graphics.FromImage(bitmap);

                graphics.Clear(Color.White);
                graphics.DrawString(pdfText, new System.Drawing.Font("Arial", 12), Brushes.Black, PointF.Empty);

                using (var barcodeReader = new BarCodeReader())
                {
                    barcodeReader.SetBarCodeImage(bitmap);
                    foreach (BarCodeResult result in barcodeReader.ReadBarCodes())
                        MessageBox.Show("BarCode CodeText: " + result.CodeText);
                }

Approach 8:

using (BarCodeReader reader = new BarCodeReader(inputPath, DecodeType.Code39Standard, DecodeType.Code128))
{
foreach (BarCodeResult result in reader.ReadBarCodes())
MessageBox.Show("BarCode CodeText: " + result.CodeText);
}

Thanks,
Saurabh

@sranjan50,

Thank you for providing additional details after trying different measures. We need to thoroughly investigate and evaluate your issue in order to find the best solution or alternative/workaround (if possible) to address it.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): BARCODENET-38754

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

It is not a bug because provided in pdf document barcode is Code128 but you set for recognition Pdf417, QR and DataMatrix.

In case you set AllSupportedTypes or Code128 (for better performance) to recognition barcode types, everything is recognized well. Also I advice to set BarCodeReader.QualitySettings.llowOneDWipedBarsRestoration to helps with wiped bar recognition. It improves recognition quality but sometimes the engine with this options can recognize barcode on some noise.

Here is the code which recognizes the barcode well:

using (Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(inputPath))
{
    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, System.Drawing.Imaging.ImageFormat.Png);
        ms.Position = 0;

        //recognize with all possible barcode types(slow)
        //BarCodeReader reader = new BarCodeReader(ms, DecodeType.AllSupportedTypes);
        //recognize with selected type
        BarCodeReader reader = new BarCodeReader(ms, DecodeType.Code128);
        //can helps with corrupted 1D barcodes
        //reader.QualitySettings.AllowOneDWipedBarsRestoration = true;
        foreach (BarCodeResult result in reader.ReadBarCodes())
            MessageBox.Show("BarCode CodeText: " + result.CodeText);
    }
}

Thanks @alexander.gavriluk for the solution, Now, I am able to read the barcode from pdf document.

DecodeType as code128 is faster than AllSupportedType. Does Code128 work for all type of barcode formats?

Code128 works for Code 128 barcode type

AllSupportedType includes all of these barcode types. Currently we support 80+ barcode types and not all are in the provided table (we will update the documentation some later).