October 22, 2024
Chicago 12, Melborne City, USA
pdf

Read numbers under barcode (not barcode stripes itself) in .NET 8


I need help about reading numbers under barcode (not stripes) from PDF file. My idea was to convert page to image and then read it.

This is example of barcode that is located in the top right part of PDF document.
(https://i.sstatic.net/jtJl89rF.png)

I tried ZXing to try and read numbers under barcode, from image at least, but I had no luck.

 public static string ReadBarcodeFromImage(string filePath)
 {

     BarcodeReader barcodeReader = new BarcodeReader()
     {
         AutoRotate = true,
         Options =
         {
             TryHarder = true,
             TryInverted = true,  
         }
     };

     var bitmap = (Bitmap)Image.FromFile(filepath);
     var result = barcodeReader.Decode(bitmap);
     return result?.Text;
 }

Then I tried to convert pdf page to image, using PdfiumViewer, and read it but also had no luck:

public string ReadBarcodeFromPdf(string pdfPath)
{
        using (var document = PdfiumViewer.PdfDocument.Load(pdfPath))
        {

            // Render the first page
            using (var image = document.Render(0, 300, 300, true))
            {
                //ZXing
                BarcodeReader barcodeReader = new BarcodeReader()
                {
                    AutoRotate = true,
                    Options =
                    {
                        TryHarder = true,
                        TryInverted = true,
                    }
                };

                var result = barcodeReader.Decode((Bitmap)image);
                return result?.Text;
            }
        }
}



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video