如何在C#中从PDF读取条形码
IronBarcode 可直接从 PDF 文档中读取条形码,无需先转换为图像。使用 ReadPdf 方法,只需一行代码即可从发票、运输标签和报告中提取条形码数据。
从 PDF 文档中读取条形码是指检测和解码 PDF 页面中的条形码。 该技术可直接从数字文档中提取编码信息,无需人工扫描打印条形码。 它可以自动化处理发票、运输标签、报告和其他包含 BarCode 数据的文档的工作流程。
快速入门:直接从PDF读取条形码使用 IronBarcode 的 ReadPdf 方法从PDF中读取条形码,无需转换为图像。 用一行代码提取 BarCode 数据,然后根据需要添加高级选项。
-
1使用 IronBarcode 安装 NuGet Package Manager
-
2复制并运行这段代码。
var results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf");Dim results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf")Dim results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronBarcode
读取PDF条形码的基本步骤是什么?
- 安装条形码库以处理条形码文件。 请查看我们的 NuGet包 指南以进行平台特定的安装。
- 如果需要,创建
PdfBarcodeReaderOptions。 - 使用
BarcodeReader中的ReadPdf方法从PDF中读取条形码。 - 使用
BarcodeReaderOption指定额外的条形码读取选项。 - 提取条形码值。
如何直接从PDF文档中读取条形码?
IronBarcode 可直接从PDF文档中读取条形码,无需转换为图像。 有关所有功能的全面概述,请访问我们的 功能页面。 使用 BarcodeReader.ReadPdf() 方法,它接受以下PDF输入类型:
byte[]数组: PDF文档作为字节数组。IEnumerable<Byte[]>: 存储在集合中的PDF文档字节数组。MemoryStream: PDF文档作为MemoryStream类型。IEnumerable<Stream>: PDF文档作为MemoryStream集合。 请参阅我们的 从流中读取条形码 指南。String: PDF文档路径为字符串或复制到项目中的文件名。IEnumerable<String>: 存储在集合中的PDF文档路径/名称字符串。
BarcodeReader.ReadPdf() 方法也接受 PdfBarcodeReaderOptions 以实现高级读取功能,下一节将详细介绍。 下面是如何使用 BarcodeReader.ReadPdf() 来读取PDF文档中的条形码:
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());
}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 valueImports 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 value将PDF文件路径字符串传递给 BarcodeReader.ReadPdf() 来读取条形码值。 有关从不同来源读取条形码的更多示例,请查看我们的 在C# / .NET中读取条形码 教程。 要打印PDF中找到的所有条形码值,可使用 foreach 循环遍历结果,并对每个元素调用 ToString()。 本示例还演示了使用 PDF 文档名称集合作为方法参数。
如何一次读取多个PDF?
IronBarcode 提供了一种 ReadPdfs 方法以同时处理多个PDF。 此方法可从 PDF 列表中高效提取 BarCode。 对于处理文档内的多个条形码,请参阅我们的 读取多个条形码 指南。
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);
}
}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
NextImports 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
Next此代码从目录中检索所有PDF文件,将它们添加到 List<string>,并将列表作为输入传递给 ReadPdfs。 该方法返回一个 BarcodeResults 数组。 循环查看结果,从每个 PDF 中获取 BarCode。
如何配置PDF条形码读取选项?
使用 PdfBarcodeReaderOptions 配置从PDF读取条形码。 有关所有读取器设置的详细说明,请访问我们的 设置PDF条形码读取选项 示例。 调整这些属性可以提高 质量、准确性和性能。 PdfBarcodeReaderOptions 继承了所有 BarcodeReaderOptions 属性并添加了PDF特定选项。 在实例化 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
};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)Imports IronBarCode
Imports System.Collections.Generic
Private pageNumber As New List(Of Integer)() From {1, 2, 3}
Private PdfOptions As New PdfBarcodeReaderOptions(pageNumber)除从 BarcodeReaderOptions 继承的属性外,PdfBarcodeReaderOptions 还提供了以下附加属性。
DPI设置如何影响条形码读取?
设置 PDF 文档中条形码图像的 DPI(每英寸点数)。 这样可以提高对低质量 BarCode 的读取能力。 使用 整数 值。 默认 DPI 为 150。对于较小或质量较低的 BarCode,可提高到 300 或 600 以获得更好的识别效果。 较高的 DPI 值会增加处理时间和内存使用量。
何时应指定页码?
指定包含 BarCode 的页码,以提高性能,特别是对于多页 PDF。 当您提供特定页码时,IronBarcode 会跳过没有条形码的页面。 页码以 1 为基础(第一页为 1,而不是 0)。 有关处理大文件的优化技术,请参阅我们的 读取速度选项 指南。
如何处理受密码保护的PDF?
通过将密码作为 字符串 输入来处理加密的 PDF 文件。 IronBarcode 无法检索PDF密码。 确保您拥有必要的权限,并在应用程序中安全地存储密码。
小型条形码应使用什么缩放因子?
在转换为图像时,控制宽度和高度的 缩放因子。 接受 整数 值,默认值为 3.5。更高的缩放因子有助于通过缩放 PDF 来读取较小的 BarCode。 对于 1 英寸以下的 BarCode,请使用缩放因子 5.0 或更高。 较大的缩放因子会影响性能。
如何实现PDF的高级条形码读取?
在您的项目中应用 PdfBarcodeReaderOptions 属性以增强从PDF文档读取条形码。 对于条形码无法识别时的其他故障排除提示,请参阅我们的 条形码未被识别 指南。
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());
}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 valueImports 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 value使用变量名初始化 PdfBarcodeReaderOptions 以访问和调整属性。 在初始化过程中将页码作为参数传递,以便将设置应用到特定页面。 或者,使用 PageNumbers 属性设置页码。
使用继承的 BarcodeReaderOptions 属性,例如 ExpectMultipleBarcodes 和 ExpectBarcodeTypes,提高性能和准确性。 将 PDF 文件路径作为第一个参数、配置好的 PdfBarcodeReaderOptions 作为第二个参数传递给 BarcodeReader.ReadPdf(),即可应用这些设置。
对于处理中有缺陷或损坏条形码的PDF,请探索我们的 图像校正 功能,可以在PDF处理期间应用。
常见问题
如何使用C#从PDF文件中读取条形码?
您可以使用IronBarcode库从PDF文件中读取条形码,该库提供一个ReadPdf方法,允许您直接从PDF文档中提取条形码数据,而无需将其转换为图像。
我应该使用什么方法一次读取多个PDF文件?
使用IronBarcode中的ReadPdfs方法同时处理和提取多个PDF文件中的条形码。此方法可以高效处理PDF文档列表。
如何配置PDF文件的条形码阅读选项?
您可以通过使用PdfBarcodeReaderOptions类来设置DPI、特定页码及其他高级选项,以提高条形码阅读的准确性和性能。
如果我的PDF文档受密码保护,我该怎么办?
对于受密码保护的PDF文档,请将密码作为字符串输入提供给IronBarcode。确保您拥有必要的权限,并安全地存储密码。
DPI设置如何影响PDF中的条形码读取?
设置较高的DPI(每英寸点数)可以提高低质量条形码在PDF文档中的可读性。增加DPI可以帮助识别较小或较低质量的条形码,但可能也会增加处理时间。
从PDF中读取条形码时我应该指定页码吗?
指定页码可以提高性能,特别是在多页PDF中。IronBarcode会跳过没有条形码的页面,从而优化读取过程。
如何从不同类型的PDF输入格式中读取条形码?
IronBarcode的ReadPdf方法接受多种PDF输入类型,包括文件路径字符串、字节数组和流,提供了处理不同输入源的灵活性。
可以在不转换为图像的情况下从PDF中提取条形码吗?
可以,IronBarcode允许您使用ReadPdf方法直接从PDF文档中提取条形码,无需进行图像转换。
建议在PDF中处理小条形码时使用什么比例因子?
建议使用较高的比例因子,比如5.0或更高,以帮助放大条形码图像,使其更易于读取。
如何处理单个PDF文档中的多个条形码?
您可以在PdfBarcodeReaderOptions中使用ExpectMultipleBarcodes属性,以指示IronBarcode检测和解码同一个PDF页面中的多个条形码。