VintaSoft Barcode .NET SDK v10.0
Read barcodes from PDF document in WPF

Barcodes can be read from PDF document by 3 ways:


Example: Here is an example that shows how to read barcodes from image resources of PDF document using built-in Vintasoft.WpfBarcode.PdfImageViewer class of VintaSoft Barcode .NET SDK:
Private Shared Sub ReadBarcodesFromPdfDocument(fileName As String, barcodes As BarcodeType)
        ' create the PdfImageViewer object and open PDF document
        Using pdfImageViewer As New PdfImageViewer(fileName)
                ' create the barcode reader
                Using reader As New BarcodeReader()
                        ' set barcode reader settings
                        reader.Settings.ScanBarcodeTypes = barcodes

                        ' for each page of PDF document
                        For i As Integer = 0 To pdfImageViewer.PageCount - 1
                                ' get names of all image resources of page i
                                Dim imageNames As String() = pdfImageViewer.GetImageNames(i)
                                ' for each image resource of page i
                                For k As Integer = 0 To imageNames.Length - 1
                                        Console.WriteLine(String.Format("Page {0}, image {1}: ", i, imageNames(k)))
                                        Dim barcodeImage As BitmapSource
                                        ' get image of image resource
                                        Try
                                                barcodeImage = pdfImageViewer.GetImage(i, imageNames(k))
                                        Catch e As Exception
                                                ' not supported image format
                                                Console.WriteLine(e.Message)
                           Continue For
                                        End Try

                                        ' read barcodes from image
                                        Dim barcodesInfo As IBarcodeInfo() = reader.ReadBarcodes(barcodeImage)

                                        ' output information about barcodes
                                        If barcodesInfo.Length = 0 Then
                                                Console.WriteLine("No barcodes found.")
                                        Else
                                                For j As Integer = 0 To barcodesInfo.Length - 1
                                                        Console.WriteLine(String.Format("[{0}] {1}", barcodesInfo(j).BarcodeType, barcodesInfo(j).Value))
                                                Next
                                        End If
                                Next
                        Next
                End Using
        End Using
End Sub
              
static void ReadBarcodesFromPdfDocument(string fileName, BarcodeType barcodes)
{
    // create the PdfImageViewer object and open PDF document
    using (PdfImageViewer pdfImageViewer = new PdfImageViewer(fileName))
    {
        // create the barcode reader
        using (BarcodeReader reader = new BarcodeReader())
        {
            // set barcode reader settings
            reader.Settings.ScanBarcodeTypes = barcodes;

            // for each page of PDF document
            for (int i = 0; i < pdfImageViewer.PageCount; i++)
            {
                // get names of all image resources of page i
                string[] imageNames = pdfImageViewer.GetImageNames(i);
                // for each image resource of page i
                for (int k = 0; k < imageNames.Length; k++)
                {
                    Console.WriteLine(string.Format("Page {0}, image {1}: ", i, imageNames[k]));
                    BitmapSource barcodeImage;
                    // get image of image resource
                    try
                    {
                        barcodeImage = pdfImageViewer.GetImage(i, imageNames[k]);
                    }
                    catch (Exception e)
                    {
                        // not supported image format
                        Console.WriteLine(e.Message);
                        continue;
                    }

                    // read barcodes from image
                    IBarcodeInfo[] barcodesInfo = reader.ReadBarcodes(barcodeImage);

                    // output information about barcodes
                    if (barcodesInfo.Length == 0)
                    {
                        Console.WriteLine("No barcodes found.");
                    }
                    else
                    {
                        for (int j = 0; j < barcodesInfo.Length; j++)
                            Console.WriteLine(string.Format("[{0}] {1}", barcodesInfo[j].BarcodeType,
                                                                         barcodesInfo[j].Value));
                    }
                }
            }
        }
    }
}
                


Example: Here is an example that shows how to read barcodes from image resources of PDF document using VintaSoft Imaging .NET SDK:
' Important: You need Vintasoft.WpfBarcode.dll,
'                     Vintasoft.Imaging.dll,
'                     Vintasoft.Imaging.Wpf.dll,
'                     Vintasoft.Imaging.Pdf.dll assemblies
'                     to run this code.
Public Shared Sub ReadBarcodesFromPDFDocumentResources(pdfFilename As String)
        ' open PDF document
        Using document As New PdfDocument(pdfFilename)
                ' for each PDF page
                For i As Integer = 0 To document.Pages.Count - 1
                        ' get image resources of PDF page
                        Dim imageResources As PdfImageResource() = document.Pages(i).GetImages()

                        ' for each image resources
                        For j As Integer = 0 To imageResources.Length - 1
                                ' get image resource
                                Using imageResource As VintasoftImage = imageResources(j).GetImage()
                                        ' get image of image resource
                                        Dim bitmap As BitmapSource = VintasoftImageConverter.ToBitmapSource(imageResource)

                                        Console.WriteLine(String.Format("[Page {0}, resource {1}]", i + 1, j + 1))
                                        ' read barcodes from image
                                        ReadBarcodesFromBitmapSource(bitmap)
                                End Using
                        Next
                Next
        End Using
End Sub

Public Shared Sub ReadBarcodesFromBitmapSource(barcodeImage As BitmapSource)
        ' create barcode reader
        Using reader As New BarcodeReader()
                ' specify that reader must search for Code39, Code39Extended,
                ' Code128, SSCC18 and DataMatrix barcodes
                reader.Settings.ScanBarcodeTypes = BarcodeType.Code39 Or BarcodeType.Code128 Or BarcodeType.DataMatrix
                reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.Code39Extended)
                reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.SSCC18)

                ' specify that reader must search for horizontal and vertical barcodes only
                reader.Settings.ScanDirection = ScanDirection.Horizontal Or ScanDirection.Vertical

                ' use Automatic Recognition
                reader.Settings.AutomaticRecognition = True

                ' expected one barcode
                reader.Settings.ExpectedBarcodes = 1

                ' scan interval - 5 pixels
                reader.Settings.ScanInterval = 5

                ' read barcodes from image
                Dim infos As IBarcodeInfo() = reader.ReadBarcodes(barcodeImage)

                ' if barcodes are not detected
                If infos.Length = 0 Then
                        Console.WriteLine("No barcodes found.")
                Else
                        ' if barcodes are detected
                        ' get information about extracted barcodes

                        Console.WriteLine(String.Format("{0} barcodes found:", infos.Length))
                        Console.WriteLine()
                        For i As Integer = 0 To infos.Length - 1
                                Dim info As IBarcodeInfo = infos(i)
                                Console.WriteLine(String.Format("[{0}:{1}]", i + 1, info.BarcodeType))
                                Console.WriteLine(String.Format("Value:      {0}", info.Value))
                                Console.WriteLine(String.Format("Region:     {0}", info.Region))
                                Console.WriteLine()
                        Next
                End If
        End Using
End Sub
              
// Important: You need Vintasoft.WpfBarcode.dll,
//                     Vintasoft.Imaging.dll,
//                     Vintasoft.Imaging.Wpf.dll,
//                     Vintasoft.Imaging.Pdf.dll assemblies
//                     to run this code.
public static void ReadBarcodesFromPDFDocumentResources(string pdfFilename)
{
    // open PDF document
    using (PdfDocument document = new PdfDocument(pdfFilename))
    {
        // for each PDF page
        for (int i = 0; i < document.Pages.Count; i++)
        {
            // get image resources of PDF page
            PdfImageResource[] imageResources = document.Pages[i].GetImages();

            // for each image resources
            for (int j = 0; j < imageResources.Length; j++)
            {
                // get image resource
                using (VintasoftImage imageResource = imageResources[j].GetImage())
                {
                    // get image of image resource
                    BitmapSource bitmap = VintasoftImageConverter.ToBitmapSource(imageResource);

                    Console.WriteLine(string.Format("[Page {0}, resource {1}]", i + 1, j + 1));
                    // read barcodes from image
                    ReadBarcodesFromBitmapSource(bitmap);
                }
            }
        }
    }
}

public static void ReadBarcodesFromBitmapSource(BitmapSource barcodeImage)
{
    // create barcode reader
    using (BarcodeReader reader = new BarcodeReader())
    {
        // specify that reader must search for Code39, Code39Extended,
        // Code128, SSCC18 and DataMatrix barcodes
        reader.Settings.ScanBarcodeTypes =
            BarcodeType.Code39 |
            BarcodeType.Code128 |
            BarcodeType.DataMatrix;
        reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.Code39Extended);
        reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.SSCC18);

        // specify that reader must search for horizontal and vertical barcodes only
        reader.Settings.ScanDirection = ScanDirection.Horizontal | ScanDirection.Vertical;

        // use Automatic Recognition
        reader.Settings.AutomaticRecognition = true;

        // expected one barcode
        reader.Settings.ExpectedBarcodes = 1;

        // scan interval - 5 pixels
        reader.Settings.ScanInterval = 5;

        // read barcodes from image
        IBarcodeInfo[] infos = reader.ReadBarcodes(barcodeImage);

        // if barcodes are not detected
        if (infos.Length == 0)
        {
            Console.WriteLine("No barcodes found.");
        }
        // if barcodes are detected
        else
        {
            // get information about extracted barcodes

            Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
            Console.WriteLine();
            for (int i = 0; i < infos.Length; i++)
            {
                IBarcodeInfo info = infos[i];
                Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType));
                Console.WriteLine(string.Format("Value:      {0}", info.Value));
                Console.WriteLine(string.Format("Region:     {0}", info.Region));
                Console.WriteLine();
            }
        }
    }
}
                


Example: Here is an example that shows how to read barcodes from rasterized page of PDF document using VintaSoft Imaging .NET SDK:
' Important: You need Vintasoft.WpfBarcode.dll,
'                     Vintasoft.Imaging.dll,
   '                     Vintasoft.Imaging.Wpf.dll,
   '                     Vintasoft.Imaging.Pdf.dll assemblies
'                     to run this code.
Public Shared Sub ReadBarcodesFromVectorPDFDocument(pdfFilename As String)
        ' create the image collection
        Using pdfPages As New ImageCollection()
                ' add PDF document to the image collection
                pdfPages.Add(pdfFilename)

                ' set the rendering settings if necessary
                pdfPages.SetRenderingSettings(New RenderingSettings(New Resolution(200, 200)))

                ' for each PDF page
                For Each image As VintasoftImage In pdfPages
                        ' get page image
                        Dim pageImage As BitmapSource = VintasoftImageConverter.ToBitmapSource(image)
                        ' read barcodes from image
                        ReadBarcodesFromBitmapSource(pageImage)
                Next

                ' clear image collection
                pdfPages.ClearAndDisposeItems()
        End Using
End Sub

Public Shared Sub ReadBarcodesFromBitmapSource(barcodeImage As BitmapSource)
        ' create barcode reader
        Dim reader As New BarcodeReader()

        ' specify that reader must search for Code39, Code39Extended,
        ' Code128, SSCC18 and DataMatrix barcodes
        reader.Settings.ScanBarcodeTypes = BarcodeType.Code39 Or BarcodeType.Code128 Or BarcodeType.DataMatrix
        reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.Code39Extended)
        reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.SSCC18)

        ' specify that reader must search for horizontal and vertical barcodes only
        reader.Settings.ScanDirection = ScanDirection.Horizontal Or ScanDirection.Vertical

        ' use Automatic Recognition
        reader.Settings.AutomaticRecognition = True

        ' read barcodes from image
        Dim infos As IBarcodeInfo() = reader.ReadBarcodes(barcodeImage)

        ' if barcodes are not detected
        If infos.Length = 0 Then
                Console.WriteLine("No barcodes found.")
        Else
                ' if barcodes are detected
                ' get information about extracted barcodes

                Console.WriteLine(String.Format("{0} barcodes found:", infos.Length))
                Console.WriteLine()
                For i As Integer = 0 To infos.Length - 1
                        Dim info As IBarcodeInfo = infos(i)
                        Console.WriteLine(String.Format("[{0}:{1}]", i + 1, info.BarcodeType))
                        Console.WriteLine(String.Format("Value:      {0}", info.Value))
                        Console.WriteLine(String.Format("Region:     {0}", info.Region))
                        Console.WriteLine()
                Next
        End If
End Sub
              
// Important: You need Vintasoft.WpfBarcode.dll,
//                     Vintasoft.Imaging.dll,
//                     Vintasoft.Imaging.Wpf.dll,
//                     Vintasoft.Imaging.Pdf.dll assemblies
//                     to run this code.
public static void ReadBarcodesFromVectorPDFDocument(string pdfFilename)
{
    // create the image collection
    using (ImageCollection pdfPages = new ImageCollection())
    {
        // add PDF document to the image collection
        pdfPages.Add(pdfFilename);

        // set the rendering settings if necessary
        pdfPages.SetRenderingSettings(new RenderingSettings(new Resolution(200, 200)));

        // for each PDF page
        foreach (VintasoftImage image in pdfPages)
        {
            // get page image
            BitmapSource pageImage = VintasoftImageConverter.ToBitmapSource(image);
            // read barcodes from image
            ReadBarcodesFromBitmapSource(pageImage);
        }

        // clear image collection
        pdfPages.ClearAndDisposeItems();
    }
}

public static void ReadBarcodesFromBitmapSource(BitmapSource barcodeImage)
{
    // create barcode reader
    BarcodeReader reader = new BarcodeReader();

    // specify that reader must search for Code39, Code39Extended,
    // Code128, SSCC18 and DataMatrix barcodes
    reader.Settings.ScanBarcodeTypes =
        BarcodeType.Code39 |
        BarcodeType.Code128 |
        BarcodeType.DataMatrix;
    reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.Code39Extended);
    reader.Settings.ScanBarcodeSubsets.Add(BarcodeSymbologySubsets.SSCC18);

    // specify that reader must search for horizontal and vertical barcodes only
    reader.Settings.ScanDirection = ScanDirection.Horizontal | ScanDirection.Vertical;

    // use Automatic Recognition
    reader.Settings.AutomaticRecognition = true;

    // read barcodes from image
    IBarcodeInfo[] infos = reader.ReadBarcodes(barcodeImage);

    // if barcodes are not detected
    if (infos.Length == 0)
    {
        Console.WriteLine("No barcodes found.");
    }
    // if barcodes are detected
    else
    {
        // get information about extracted barcodes

        Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
        Console.WriteLine();
        for (int i = 0; i < infos.Length; i++)
        {
            IBarcodeInfo info = infos[i];
            Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType));
            Console.WriteLine(string.Format("Value:      {0}", info.Value));
            Console.WriteLine(string.Format("Region:     {0}", info.Region));
            Console.WriteLine();
        }
    }
}