How to Read Barcodes from PDF in C#
IronBarcode enables direct barcode reading from PDF documents without converting to images first, using the ReadPdf method to extract barcode data from invoices, shipping labels, and reports in just one line of code.
Reading barcodes from PDF documents means detecting and decoding barcodes within PDF pages. This technology extracts encoded information directly from digital documents, eliminating manual scanning of printed barcodes. It automates workflows for processing invoices, shipping labels, reports, and other documents containing barcode data.
Quickstart: Reading Barcodes Directly from a PDFUse IronBarcode's ReadPdf method to read barcodes from PDFs without converting to images. Extract barcode data with one line of code, then add advanced options as needed.
-
1Install IronBarcode with NuGet Package Manager
-
2Copy and run this code snippet.
var results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf");C# -
3Deploy to test on your live environment
Start using IronBarcode in your project today with a free trial
What Are the Basic Steps to Read PDF Barcodes?
- Install the barcode library to process barcode files. Check our NuGet Packages guide for platform-specific installation.
- Create
PdfBarcodeReaderOptionsif required. - Use the
ReadPdfmethod fromBarcodeReaderto read barcodes from PDFs. - Specify additional barcode reading options using
BarcodeReaderOption. - Extract barcode values.
How Do I Read Barcodes from PDF Documents Directly?
IronBarcode reads barcodes directly from PDF documents without requiring conversion to images. For a comprehensive overview of all features, visit our Features page. Use the BarcodeReader.ReadPdf() method, which accepts these PDF input types:
byte[]array: PDF document as a byte array.IEnumerable<Byte[]>: PDF documents as byte arrays stored in a collection.MemoryStream: PDF documents asMemoryStreamtype.IEnumerable<Stream>: PDF documents as a collection ofMemoryStream. See our Read Barcode from Streams guide.String: PDF document path as a string or filename if copied into the project.IEnumerable<String>: PDF document path/name strings stored in a collection.
The BarcodeReader.ReadPdf() method also accepts PdfBarcodeReaderOptions for advanced reading capabilities, discussed in the next section. Here's how to use BarcodeReader.ReadPdf() to read barcodes in PDF documents:
using IronBarCode;
using System;
using System.Collections.Generic;
List<String> docs = new List<String>();
docs.Add(@"pdf_a.pdf");
docs.Add(@"pdf_b.pdf");
var myBarcode = BarcodeReader.ReadPdfs(docs); //can also accept individual PDF document file path as argument
foreach (var value in myBarcode)
{
Console.WriteLine(value.ToString());
}Imports IronBarCode
Imports System
Imports System.Collections.Generic
Private docs As New List(Of String)()
docs.Add("pdf_a.pdf")
docs.Add("pdf_b.pdf")
Dim myBarcode = BarcodeReader.ReadPdfs(docs) 'can also accept individual PDF document file path as argument
For Each value In myBarcode
Console.WriteLine(value.ToString())
Next valuePass the PDF file path string to BarcodeReader.ReadPdf() to read barcode values. For more examples on reading barcodes from different sources, check our Reading Barcodes in C# / .NET tutorial. To print all barcode values found in the PDF, iterate through the results using a foreach loop and call ToString() on each element. This example also demonstrates using a collection of PDF document names as the method argument.
How Can I Read Multiple PDFs at Once?
IronBarcode provides a ReadPdfs method to process multiple PDFs simultaneously. This method extracts barcodes from a list of PDFs efficiently. For handling multiple barcodes within documents, see our guide on Read Multiple Barcodes.
using IronBarCode;
using System;
using System.Collections.Generic;
using System.IO;
// Get all PDF files from a directory and add to list
string folderPath = @"PATH_TO_YOUR_FOLDER";
List<string> docs = new List<string>(Directory.GetFiles(folderPath, "*.pdf"));
// Read barcodes from all PDFs
var docResult = BarcodeReader.ReadPdfs(docs);
// Print results
foreach (var doc in docResult)
{
foreach (var item in doc)
{
Console.WriteLine("Barcode " + item.ToString() + " found at page " + item.PageNumber);
}
}Imports IronBarCode
Imports System
Imports System.Collections.Generic
Imports System.IO
' Get all PDF files from a directory and add to list
Dim folderPath As String = "PATH_TO_YOUR_FOLDER"
Dim docs As New List(Of String)(Directory.GetFiles(folderPath, "*.pdf"))
' Read barcodes from all PDFs
Dim docResult = BarcodeReader.ReadPdfs(docs)
' Print results
For Each doc In docResult
For Each item In doc
Console.WriteLine("Barcode " & item.ToString() & " found at page " & item.PageNumber)
Next
NextThis code retrieves all PDF files from a directory, adds them to a List<string>, and calls ReadPdfs with the list as input. The method returns an array of BarcodeResults. Loop through the results to access barcodes from each PDF.
How Do I Configure PDF Barcode Reader Options?
Configure barcode reading from PDFs using PdfBarcodeReaderOptions. For a detailed explanation of all reader settings, visit our Set PDF Barcode Reader Options example. Adjusting these properties improves quality, accuracy, and performance. PdfBarcodeReaderOptions inherits all BarcodeReaderOptions properties and adds PDF-specific options. Specify page numbers when instantiating PdfBarcodeReaderOptions:
using IronBarCode;
using System.Collections.Generic;
List<int> pageNumber = new List<int>() { 1, 2, 3 };
PdfBarcodeReaderOptions PdfOptions = new PdfBarcodeReaderOptions(pageNumber) // can also use individual page number as argument
{
// Properties of PDF Barcode reader options
};Imports IronBarCode
Imports System.Collections.Generic
Private pageNumber As New List(Of Integer)() From {1, 2, 3}
Private PdfOptions As New PdfBarcodeReaderOptions(pageNumber)Explore the additional properties available in PdfBarcodeReaderOptions beyond those inherited from BarcodeReaderOptions.
How Does DPI Setting Affect Barcode Reading?
Set the DPI (Dots Per Inch) of barcode images in PDF documents. This improves reading of low-quality barcodes. Use an Integer value. Default DPI is 150. For smaller or lower-quality barcodes, increase to 300 or 600 for better recognition. Higher DPI values increase processing time and memory usage.
When Should I Specify Page Numbers?
Specify page numbers containing barcodes to improve performance, especially for multi-page PDFs. IronBarcode skips pages without barcodes when you provide specific page numbers. Page numbering is 1-based (first page is 1, not 0). For optimization techniques with large documents, see our guide on Reading Speed Options.
How Do I Handle Password-Protected PDFs?
Work with encrypted PDF files by providing the password as a String input. IronBarcode cannot retrieve PDF passwords. Ensure you have necessary permissions and store passwords securely in your application.
What Scale Factor Should I Use for Small Barcodes?
Control the scale factor for width and height when converting to images. Accepts an Integer value with a default of 3.5. Higher scale factors help read small barcodes by zooming the PDF. For barcodes under 1 inch, use scale factor 5.0 or higher. High scale factors impact performance.
How Do I Implement Advanced Barcode Reading from PDFs?
Apply PdfBarcodeReaderOptions properties in your project to enhance barcode reading from PDF documents. For additional troubleshooting tips when barcodes aren't recognized, refer to our Barcode Not Recognized guide.
using IronBarCode;
using System;
using System.Collections.Generic;
List<int> pageNumber = new List<int>() { 1, 2, 3 };
PdfBarcodeReaderOptions PdfOptions = new PdfBarcodeReaderOptions(pageNumber)
{
DPI = 150,
//PageNumbers = pageNumber, //this property is not needed if page numbers has been specified as the argument in PdfBarcodeReaderOptions
Password = "barcode",
Scale = 3.5,
//properties below are some of the properties inherited from BarcodeReaderOptions
Speed = ReadingSpeed.Detailed,
ExpectBarcodeTypes = BarcodeEncoding.Code93,
ExpectMultipleBarcodes = true
};
var myBarcode = BarcodeReader.ReadPdf(@"pdf_a_filepath.pdf", PdfOptions);
foreach (var value in myBarcode)
{
Console.WriteLine(value.ToString());
}Imports IronBarCode
Imports System
Imports System.Collections.Generic
Private pageNumber As New List(Of Integer)() From {1, 2, 3}
Private PdfOptions As New PdfBarcodeReaderOptions(pageNumber) With {
.DPI = 150,
.Password = "barcode",
.Scale = 3.5,
.Speed = ReadingSpeed.Detailed,
.ExpectBarcodeTypes = BarcodeEncoding.Code93,
.ExpectMultipleBarcodes = True
}
Private myBarcode = BarcodeReader.ReadPdf("pdf_a_filepath.pdf", PdfOptions)
For Each value In myBarcode
Console.WriteLine(value.ToString())
Next valueInitialize PdfBarcodeReaderOptions with a variable name to access and adjust properties. Pass page numbers as an argument during initialization to apply settings to specific pages. Alternatively, set page numbers using the PageNumbers property.
Use inherited BarcodeReaderOptions properties like ExpectMultipleBarcodes and ExpectBarcodeTypes to improve performance and accuracy. Apply configured PdfBarcodeReaderOptions by passing it as the second argument to BarcodeReader.ReadPdf(), with the PDF file path as the first argument.
For processing PDFs with imperfect or damaged barcodes, explore our Image Correction features that can be applied during PDF processing.
Frequently Asked Questions
How can I read barcodes from PDF files using C#?
You can read barcodes from PDF files using the IronBarcode library, which provides a `ReadPdf` method allowing you to extract barcode data directly from PDF documents without needing to convert them to images.
What method should I use to read multiple PDF files at once?
Use the `ReadPdfs` method in IronBarcode to process and extract barcodes from multiple PDF files simultaneously. This method efficiently handles lists of PDF documents.
How do I configure the barcode reader options for PDF files?
You can configure the barcode reader options by using the `PdfBarcodeReaderOptions` class to set properties like DPI, specific page numbers, and other advanced options to enhance barcode reading accuracy and performance.
What should I do if my PDF documents are password-protected?
For password-protected PDF documents, provide the password as a string input to IronBarcode. Ensure that you have the necessary permissions and store passwords securely.
How does DPI setting affect barcode reading in PDFs?
Setting a higher DPI (Dots Per Inch) can improve the readability of low-quality barcodes in PDF documents. Increasing the DPI can aid in recognizing smaller or lower-quality barcodes but may also increase processing time.
Should I specify page numbers when reading barcodes from PDFs?
Specifying page numbers can improve performance, especially in multi-page PDFs. IronBarcode will skip pages without barcodes, thus optimizing the reading process.
How can I read barcodes from different types of PDF input formats?
IronBarcode's `ReadPdf` method accepts various PDF input types including file paths as strings, byte arrays, and streams, facilitating flexibility in handling different input sources.
Can I extract barcodes from PDFs without converting to images?
Yes, IronBarcode allows you to extract barcodes directly from PDF documents using the `ReadPdf` method, bypassing the need for image conversion.
What scale factor is recommended for small barcodes in PDFs?
A higher scale factor, like 5.0 or higher, is recommended for reading small barcodes as it helps in enlarging the barcode images, making them easier to read.
How can I handle multiple barcodes in a single PDF document?
You can use the `ExpectMultipleBarcodes` property in `PdfBarcodeReaderOptions` to instruct IronBarcode to detect and decode multiple barcodes within the same PDF page.
