PDF document can store barcode in raster form in image-resource of PDF document. Also PDF document can store barcode in vector form in vector content of PDF document.
Private Shared Sub ReadBarcodesFromPdfDocument(fileName As String, barcodeType 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 = barcodeType ' 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 Image ' 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) ' free image resource barcodeImage.Dispose() ' 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
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Barcode /// <summary> /// Reads barcodes from image-resources of PDF document. /// </summary> /// <param name="fileName">A path to a PDF document.</param> /// <param name="barcodeTypes">The types of barcodes, which must be recognized.</param> static void ReadBarcodesFromPdfDocument(string fileName, Vintasoft.Barcode.BarcodeType barcodeTypes) { // create the PdfImageViewer object and open PDF document using (Vintasoft.Barcode.PdfImageViewer pdfImageViewer = new Vintasoft.Barcode.PdfImageViewer(fileName)) { // create the barcode reader using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader()) { // set barcode reader settings reader.Settings.ScanBarcodeTypes = barcodeTypes; // 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++) { System.Console.WriteLine(string.Format("Page {0}, image {1}: ", i, imageNames[k])); System.Drawing.Image barcodeImage; // get image of image resource try { barcodeImage = pdfImageViewer.GetImage(i, imageNames[k]); } catch (System.Exception ex) { // not supported image format System.Console.WriteLine(ex.Message); continue; } // read barcodes from image Vintasoft.Barcode.IBarcodeInfo[] barcodesInfo = reader.ReadBarcodes(barcodeImage); // free image resource barcodeImage.Dispose(); // output information about barcodes if (barcodesInfo.Length == 0) { System.Console.WriteLine("No barcodes found."); } else { for (int j = 0; j < barcodesInfo.Length; j++) System.Console.WriteLine(string.Format("[{0}] {1}", barcodesInfo[j].BarcodeType, barcodesInfo[j].Value)); } } } } } }
' Important: You need Vintasoft.Barcode.dll, ' Vintasoft.Imaging.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() ' foreach 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 Using bitmap As Image = imageResource.GetAsBitmap() Console.WriteLine(String.Format("[Page {0}, resource {1}]", i + 1, j + 1)) ' read barcodes from image ReadBarcodesFromImage(bitmap) End Using End Using Next Next End Using End Sub Public Shared Sub ReadBarcodesFromImage(barcodeImage As Image) ' 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
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Barcode /// <summary> /// Reads barcodes from image-resources of PDF document. /// </summary> /// <param name="pdfFilename">A path to a PDF document.</param> public static void ReadBarcodesFromPdfDocumentResources(string pdfFilename) { // open PDF document using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)) { // for each PDF page for (int i = 0; i < document.Pages.Count; i++) { // get image resources of PDF page Vintasoft.Imaging.Pdf.Tree.PdfImageResource[] imageResources = document.Pages[i].GetImages(); // foreach image resources for (int j = 0; j < imageResources.Length; j++) { // get image resource using (Vintasoft.Imaging.VintasoftImage imageResource = imageResources[j].GetImage()) { // get image of image resource using (System.Drawing.Image bitmap = imageResource.GetAsBitmap()) { System.Console.WriteLine(string.Format("[Page {0}, resource {1}]", i + 1, j + 1)); // read barcodes from image ReadBarcodesFromImage(bitmap); } } } } } } /// <summary> /// Reads barcodes from an image. /// </summary> /// <param name="barcodeImage">An image with barcodes.</param> public static void ReadBarcodesFromImage(System.Drawing.Image barcodeImage) { // create barcode reader using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader()) { // specify that reader must search for Code39, Code39Extended, // Code128, SSCC18 and DataMatrix barcodes reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.Code39 | Vintasoft.Barcode.BarcodeType.Code128 | Vintasoft.Barcode.BarcodeType.DataMatrix; reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.Code39Extended); reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.SSCC18); // specify that reader must search for horizontal and vertical barcodes only reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal | Vintasoft.Barcode.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 Vintasoft.Barcode.IBarcodeInfo[] infos = reader.ReadBarcodes(barcodeImage); // if barcodes are not detected if (infos.Length == 0) { System.Console.WriteLine("No barcodes found."); } // if barcodes are detected else { // get information about extracted barcodes System.Console.WriteLine(string.Format("{0} barcodes found:", infos.Length)); System.Console.WriteLine(); for (int i = 0; i < infos.Length; i++) { Vintasoft.Barcode.IBarcodeInfo info = infos[i]; System.Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType)); System.Console.WriteLine(string.Format("Value: {0}", info.Value)); System.Console.WriteLine(string.Format("Region: {0}", info.Region)); System.Console.WriteLine(); } } } }
' Important: You need Vintasoft.Barcode.dll, ' Vintasoft.Imaging.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 Using pageImage As Image = image.GetAsBitmap() ' read barcodes from image ReadBarcodesFromImage(pageImage) End Using Next ' clear image collection pdfPages.ClearAndDisposeItems() End Using End Sub Public Shared Sub ReadBarcodesFromImage(barcodeImage As Image) ' 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
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Barcode /// <summary> /// Reads barcodes from a vector PDF document. /// </summary> /// <param name="pdfFilename">A path to a PDF document.</param> public static void ReadBarcodesFromVectorPDFDocument(string pdfFilename) { // create the image collection using (Vintasoft.Imaging.ImageCollection pdfPages = new Vintasoft.Imaging.ImageCollection()) { // add PDF document to the image collection pdfPages.Add(pdfFilename); // set the rendering settings if necessary pdfPages.SetRenderingSettings(new Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(new Vintasoft.Imaging.Resolution(200, 200))); // for each PDF page foreach (Vintasoft.Imaging.VintasoftImage image in pdfPages) { // get page image using (System.Drawing.Image pageImage = image.GetAsBitmap()) { // read barcodes from image ReadBarcodesFromImage(pageImage); } } // clear image collection pdfPages.ClearAndDisposeItems(); } } /// <summary> /// Reads barcodes from an image. /// </summary> /// <param name="barcodeImage">An image with barcodes.</param> public static void ReadBarcodesFromImage(System.Drawing.Image barcodeImage) { // create barcode reader Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader(); // specify that reader must search for Code39, Code39Extended, // Code128, SSCC18 and DataMatrix barcodes reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.Code39 | Vintasoft.Barcode.BarcodeType.Code128 | Vintasoft.Barcode.BarcodeType.DataMatrix; reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.Code39Extended); reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.SSCC18); // specify that reader must search for horizontal and vertical barcodes only reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal | Vintasoft.Barcode.ScanDirection.Vertical; // use Automatic Recognition reader.Settings.AutomaticRecognition = true; // read barcodes from image Vintasoft.Barcode.IBarcodeInfo[] infos = reader.ReadBarcodes(barcodeImage); // if barcodes are not detected if (infos.Length == 0) { System.Console.WriteLine("No barcodes found."); } // if barcodes are detected else { // get information about extracted barcodes System.Console.WriteLine(string.Format("{0} barcodes found:", infos.Length)); System.Console.WriteLine(); for (int i = 0; i < infos.Length; i++) { Vintasoft.Barcode.IBarcodeInfo info = infos[i]; System.Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType)); System.Console.WriteLine(string.Format("Value: {0}", info.Value)); System.Console.WriteLine(string.Format("Region: {0}", info.Region)); System.Console.WriteLine(); } } }