How to Read Barcode From PDF in C#
- Install the barcode library to process barcode files.
- Create
PdfBarcodeReaderOptions
if required. - Use the
ReadPdf
method fromBarcodeReader
to read barcodes from PDFs. - Specify additional barcode reading options using
BarcodeReaderOption
. - Extract barcode values.
Read Barcodes from PDF Documents directly
Apart from IronBarcodes' ability to read barcodes from images, IronBarcode also takes pride in its ability to read barcodes from PDF documents. This saves users from the hassle of converting PDF documents into images before feeding them into IronBarcode for reading. Since PDF documents are more intricate and different from images, a different read method should also be used, and that is the BarcodeReader.ReadPdf()
method. This method accepts various types of PDF document input, including:
byte[]
array: PDF document as a byte array.IEnumerable<Byte[]>
: PDF documents as byte arrays stored in a collection.MemoryStream
: PDF documents as MemoryStream type.IEnumerable<Stream>
: PDF documents as a collection of MemoryStream.String
: PDF document path as a string. If the PDF document is already copied into the project, this would be the name of the PDF document in a string.IEnumerable<String>
: PDF document path/name strings stored in a collection.
Apart from the types of input mentioned above, BarcodeReader.ReadPdf()
also accepts PdfBarcodeReaderOptions
for more advanced/improved reading which we will discuss in the next subtopic. Now, let us see the code snippet below that demonstrates the use of the BarcodeReader.ReadPdf()
method to read barcodes in PDF documents.
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-1.cs
// Import necessary namespaces
using IronBarCode; // Library for reading barcodes
using System;
using System.Collections.Generic;
// Main code execution
// Create a list to hold the PDF document file paths
List<string> docs = new List<string>
{
@"pdf_a.pdf", // Add the first PDF file path
@"pdf_b.pdf" // Add the second PDF file path
};
// Read barcodes from the list of PDF documents
// The ReadPdf method can also accept a single PDF document file path as an argument
IEnumerable<BarcodeResult> myBarcode = BarcodeReader.ReadPdf(docs);
// Iterate through each barcode result and output its string representation to the console
foreach (var barcode in myBarcode)
{
// Output the details of each barcode found in the PDFs to the console
Console.WriteLine(barcode.ToString());
}
' Import necessary namespaces
Imports IronBarCode ' Library for reading barcodes
Imports System
Imports System.Collections.Generic
' Main code execution
' Create a list to hold the PDF document file paths
Private docs As New List(Of String) From {"pdf_a.pdf", "pdf_b.pdf"}
' Read barcodes from the list of PDF documents
' The ReadPdf method can also accept a single PDF document file path as an argument
Private myBarcode As IEnumerable(Of BarcodeResult) = BarcodeReader.ReadPdf(docs)
' Iterate through each barcode result and output its string representation to the console
For Each barcode In myBarcode
' Output the details of each barcode found in the PDFs to the console
Console.WriteLine(barcode.ToString())
Next barcode
From the code snippet above, we can see that to read barcodes using IronBarcode, we can simply add the file path string of the PDF document into the BarcodeReader.ReadPdf()
method to read the barcode value and store the result in a variable. If you wish to print the values of all the barcodes found in the PDF document onto the console, just use a foreach
loop to iterate and print every element found in the variable by calling the ToString()
method on them. On top of that, the code snippet above also demonstrates using a collection of PDF document names as an argument in BarcodeReader.ReadPdf()
.
But what if the barcodes in the PDF document are unable to be read? What if the performance is slow? This is where advanced PDF barcode reading takes place, in which we manipulate PdfBarcodeReaderOptions
to improve reading quality, accuracy, and performance.
Setting PDF Barcode Reader Options
Similar to reading barcodes from images, reading barcodes from PDF documents also allows users to tweak or adjust the properties in the barcode reader called PdfBarcodeReaderOptions
. Adjusting the properties in PdfBarcodeReaderOptions
will greatly help in reading quality, accuracy, and performance. All the adjustable properties in BarcodeReaderOptions
are inherited in PdfBarcodeReaderOptions
, with some additional properties for PDF documents. Users can specify the page number or collection of page numbers from the PDF document in which they want the PdfBarcodeReaderOptions
to apply when instantiating a new instance of PdfBarcodeReaderOptions
. The code snippet below demonstrates:
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-2.cs
using IronBarCode;
using System.Collections.Generic;
// Create a list of page numbers where barcodes need to be read within a PDF
List<int> pageNumbers = new List<int> { 1, 2, 3 };
// Initialize PDF Barcode Reader Options with the list of page numbers
PdfBarcodeReaderOptions pdfOptions = new PdfBarcodeReaderOptions(pageNumbers)
{
// Properties of PDF Barcode reader options can be configured here if needed.
// Here you might set things like Barcode Encoding or specifying the quality
// settings for reading the barcodes. Check the IronBarCode documentation for
// detailed properties you can set.
};
// Note: The above setup assumes that the IronBarCode library is being used to read barcodes
// from a PDF document. The `PdfBarcodeReaderOptions` allows specifying which pages should be
// scanned for barcodes. Adjust the properties in `pdfOptions` according to the requirements
// of the IronBarCode library and your specific use case to enhance barcode reading accuracy
// or performance.
Imports IronBarCode
Imports System.Collections.Generic
' Create a list of page numbers where barcodes need to be read within a PDF
Private pageNumbers As New List(Of Integer) From {1, 2, 3}
' Initialize PDF Barcode Reader Options with the list of page numbers
Private pdfOptions As New PdfBarcodeReaderOptions(pageNumbers)
' Note: The above setup assumes that the IronBarCode library is being used to read barcodes
' from a PDF document. The `PdfBarcodeReaderOptions` allows specifying which pages should be
' scanned for barcodes. Adjust the properties in `pdfOptions` according to the requirements
' of the IronBarCode library and your specific use case to enhance barcode reading accuracy
' or performance.
Now let us discover the additional properties in PdfBarcodeReaderOptions
available to manipulate, aside from those available in BarcodeReaderOptions
.
DPI
Users can specify the DPI or Dots Per Inch of the barcode image in a PDF document. This will help in reading a low-quality barcode image in a PDF document. This property can be set using an Integer value.
PageNumbers
If users know beforehand the page number that contains the barcode that needs to be read in a PDF document, they can specify it in this property. Doing this will greatly improve the reading performance of IronBarcode, especially for PDF documents that have many pages, since IronBarcode would not need to read all pages or pages that do not have barcodes to be read. This property is 1-based, meaning the first page of the PDF document is 1 instead of 0.
Password
As the name suggests, this property enables users to work with encrypted PDF files that require password input to access the contents of the PDF document. Kindly note however, IronBarcode will not be able to provide the PDF document's password. This property will accept a String input.
Scale
This property enables users to control the scale factor for scaling width and height when converting to an Image. This property accepts an Integer as a value, and the default value for this property is 3.5. Setting this property will help in reading small barcodes present in a PDF document, as scaling up will zoom the PDF document.
Advanced Barcode Reading from PDF Document
Now that we know the properties in PdfBarcodeReaderOptions
available for adjustments and tweaks, let us see how to apply them in the project for reading barcodes in a PDF document.
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-3.cs
using IronBarCode;
using System;
using System.Collections.Generic;
// Create a list of integer values representing the page numbers from which you want to read barcodes
List<int> pageNumbers = new List<int>() { 1, 2, 3 };
// Instantiate the PdfBarcodeReaderOptions and configure its properties
PdfBarcodeReaderOptions pdfOptions = new PdfBarcodeReaderOptions(pageNumbers)
{
DPI = 150, // Set the dots per inch for optimal reading accuracy
Password = "barcode", // Set the PDF password, if the document is password-protected
Scale = 3.5, // Set the scaling factor for rendering the PDF during barcode reading
// Properties inherited from BarcodeReaderOptions
Speed = ReadingSpeed.Detailed, // Use detailed speed for more thorough reading, albeit slower
ExpectBarcodeTypes = BarcodeEncoding.Code93, // Indicate expected barcode type for improved accuracy
ExpectMultipleBarcodes = true // Allow detection of multiple barcodes on the same page
};
// Attempt to read the barcodes from the specified PDF file using the configured options
var myBarcodes = BarcodeReader.ReadPdf(@"pdf_a_filepath.pdf", pdfOptions);
// Iterate through all discovered barcodes and print their string representations to the console
foreach (var barcode in myBarcodes)
{
Console.WriteLine(barcode.ToString()); // Output details for each detected barcode
}
Imports IronBarCode
Imports System
Imports System.Collections.Generic
' Create a list of integer values representing the page numbers from which you want to read barcodes
Private pageNumbers As New List(Of Integer)() From {1, 2, 3}
' Instantiate the PdfBarcodeReaderOptions and configure its properties
Private pdfOptions As New PdfBarcodeReaderOptions(pageNumbers) With {
.DPI = 150,
.Password = "barcode",
.Scale = 3.5,
.Speed = ReadingSpeed.Detailed,
.ExpectBarcodeTypes = BarcodeEncoding.Code93,
.ExpectMultipleBarcodes = True
}
' Attempt to read the barcodes from the specified PDF file using the configured options
Private myBarcodes = BarcodeReader.ReadPdf("pdf_a_filepath.pdf", pdfOptions)
' Iterate through all discovered barcodes and print their string representations to the console
For Each barcode In myBarcodes
Console.WriteLine(barcode.ToString()) ' Output details for each detected barcode
Next barcode
The code snippet above demonstrates how to implement PdfBarcodeReaderOptions
properties in IronBarcode. The PdfBarcodeReaderOptions
first needs to be initialized with a variable name before accessing and adjusting the properties. In the code snippet, we can see that the list of page numbers of the PDF document was used as an argument when initializing PdfBarcodeReaderOptions
. This specifies the page number that we want the settings of PdfBarcodeReader
to apply. Users can also specify the PDF page number in the PdfBarcodeReaderOptions
property as PageNumbers
.
On the other hand, we can also see that we can use properties from BarcodeReaderOptions
such as ExpectMultipleBarcodes
and ExpectBarcodeTypes
in PdfBarcodeReaderOptions
since they are inherited from the original class. This will greatly help in overall reading performance and accuracy. To apply the set properties of PdfBarcodeReaderOptions
in the barcode read, input the variable name of the PdfBarcodeReaderOptions
class we created as the second argument in the BarcodeReader.ReadPdf()
method, with the PDF document to be read file path as the first argument.
Frequently Asked Questions
What is IronBarcode?
IronBarcode is a .NET Barcode Library that allows users to read and write barcodes within .NET applications, including reading barcodes from PDF documents.
How do I read barcodes from a PDF in C#?
To read barcodes from a PDF in C#, install the IronBarcode library, use the ReadPdf method from BarcodeReader, and specify options using PdfBarcodeReaderOptions if needed.
What input types does BarcodeReader.ReadPdf() accept?
BarcodeReader.ReadPdf() accepts various input types including byte[] arrays, MemoryStream, and string paths, among others.
What are PdfBarcodeReaderOptions?
PdfBarcodeReaderOptions are settings that can be adjusted to improve barcode reading quality, accuracy, and performance when reading from PDF documents.
Can IronBarcode read encrypted PDF files?
Yes, IronBarcode can read encrypted PDF files by using the Password property in PdfBarcodeReaderOptions to provide the required password.
How can I improve barcode reading performance in PDFs?
You can improve reading performance by specifying the page numbers of interest in the PageNumbers property of PdfBarcodeReaderOptions.
What is the default scale factor in PdfBarcodeReaderOptions?
The default scale factor for converting a PDF to an image in PdfBarcodeReaderOptions is 3.5.
Is it possible to read multiple barcodes from a PDF?
Yes, you can read multiple barcodes from a PDF by setting the ExpectMultipleBarcodes property in PdfBarcodeReaderOptions.
How can I specify the DPI for reading barcodes in PDFs?
You can specify the DPI by setting the DPI property in PdfBarcodeReaderOptions to an integer value.
Can I use IronBarcode to read barcodes from images as well as PDFs?
Yes, IronBarcode can read barcodes from both images and PDF documents, using different methods for each.