How to Read Barcodes From PDFs in C#

如何用 C# 阅读 PDF 中的 BarCode;

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode 可直接从 PDF 文档中读取条形码,而无需先转换为图像,使用 ReadPdf 方法,只需一行代码即可从发票、运输标签和报告中提取条形码数据。

从 PDF 文档中读取条形码是指检测和解码 PDF 页面中的条形码。 该技术可直接从数字文档中提取编码信息,无需人工扫描打印条形码。 它可以自动处理发票、运输标签、报告和其他包含 BarCode 数据的文档的工作流程。

快速入门:直接从 PDF 读取条形码

<! -- 待办事项:在此处添加图片 --> <! --介绍实现的示意图 --> <!--说明:说明代码概念的图表或截图 -->

使用 IronBarcodeReadPdf 方法从 PDF 中读取条形码,而无需转换为图像。 用一行代码提取 BarCode 数据,然后根据需要添加高级选项。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronBarcode

    PM > Install-Package BarCode

  2. 复制并运行这段代码。

    var results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf");
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronBarcode,免费试用!
    arrow pointer

读取 PDF BarCode 的基本步骤是什么?

  1. 安装条形码库以处理条形码文件。 请查看我们的NuGet软件包指南,了解特定平台的安装。
  2. 如有需要,创建PdfBarcodeReaderOptions
  3. 使用BarcodeReaderReadPdf方法从 PDF 中读取条形码。
  4. 使用BarcodeReaderOption指定其他条形码读取选项。
  5. 提取条形码值。

如何直接读取 PDF 文档中的 BarCode?

<! -- 待办事项:在此处添加图片 --> <! -- 输出显示在 IronPDF 中直接从 pdf 文档中读取条形码的结果 --> -->。 <!--说明:显示代码执行输出或结果的截图 -->

IronBarcode 可直接从 PDF 文档中读取条形码,无需转换为图像。 如需全面了解所有功能,请访问我们的 功能页面。 使用 BarcodeReader.ReadPdf() 方法,该方法接受这些 PDF 输入类型:

  • byte[]数组:PDF 文档以字节数组形式表示。
  • IEnumerable<Byte[]> :以字节数组形式存储在集合中的 PDF 文档。
  • MemoryStream :将 PDF 文档作为 MemoryStream 类型。
  • IEnumerable<Stream> :PDF 文档作为 MemoryStream 的集合。 请参阅我们的 Read BarCode from Streams 指南。
  • 字符串:如果复制到项目中,PDF 文档路径为字符串或文件名。
  • IEnumerable<String> :存储在集合中的 PDF 文档路径/名称字符串。

BarcodeReader.ReadPdf()方法还接受PdfBarcodeReaderOptions以实现高级阅读功能,这将在下一节中讨论。 以下是如何使用 BarcodeReader.ReadPdf() 读取 PDF 文档中的条形码:

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-1.cs
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 value
$vbLabelText   $csharpLabel

将 PDF 文件路径字符串传递给 BarcodeReader.ReadPdf() 以读取条形码值。 有关从不同来源读取 BarCode 的更多示例,请查看我们的 Reading Barcodes in C# / .NET 教程。 要打印在 PDF 中找到的所有条形码值,请使用 foreach 循环遍历结果,并在每个元素上调用 ToString() 。 本示例还演示了使用 PDF 文档名称集合作为方法参数。

如何同时阅读多个 PDF?

IronBarcode 提供了一个 ReadPdfs 方法来同时处理多个 PDF。 此方法可从 PDF 列表中高效提取 BarCode。 如需处理文档中的多个条形码,请参阅我们的阅读多个条形码指南。

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-read-from-multiple-pdf.cs
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
Next
$vbLabelText   $csharpLabel

此代码从一个目录中检索所有 PDF 文件,将它们添加到 List<string> 中,然后以该列表为输入调用 ReadPdfs 。 该方法返回一个 BarcodeResults 数组。 循环查看结果,从每个 PDF 中获取 BarCode。

如何配置 PDF BarCode 阅读器选项?

<! -- 待办事项:在此处添加图片 --> <! -- Diagram illustrating setting pdf barcode reader options implementation --> <!--说明:说明代码概念的图表或截图 -->

使用 PdfBarcodeReaderOptions 配置从 PDF 中读取条形码。 有关所有阅读器设置的详细解释,请访问我们的 Set PDF BarCode Reader Options 示例。 调整这些属性可以提高 质量、准确性和性能PdfBarcodeReaderOptions 继承了所有 BarcodeReaderOptions 属性并添加了 PDF 特定选项。 在实例化 PdfBarcodeReaderOptions 时指定 页码

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-2.cs
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)
$vbLabelText   $csharpLabel

除了从 BarcodeReaderOptions 继承的属性外,探索 PdfBarcodeReaderOptions 中可用的其他属性。

DPI 设置如何影响条形码读取?

设置 PDF 文档中条形码图像的 DPI(每英寸点数)。 这样可以提高对低质量 BarCode 的阅读能力。 使用 Integer 值。 默认 DPI 为 150。对于较小或质量较低的 BarCode,可提高到 300 或 600 以获得更好的识别效果。 较高的 DPI 值会增加处理时间和内存使用量。

何时应指定页码?

指定包含 BarCode 的页码,以提高性能,特别是对于多页 PDF。 IronBarcode 在您提供特定页码时,会跳过没有条形码的页面。 页码以 1 为基础(第一页为 1,而不是 0)。 有关大型文档的优化技巧,请参阅我们的阅读速度选项指南。

如何处理受密码保护的 PDF?

通过将密码作为 String 输入来处理加密的 PDF 文件。 IronBarcode 无法检索 PDF 密码。 确保您拥有必要的权限,并在应用程序中安全地存储密码。

小条形码应使用什么比例系数?

在转换为图像时,控制宽度和高度的 scale factor。 接受 Integer 值,默认值为 3.5。更高的比例系数有助于通过缩放 PDF 来读取较小的 BarCode。 对于 1 英寸以下的 BarCode,请使用比例因子 5.0 或更高。 高比例因素会影响性能。

如何从 PDF 实现高级条形码读取?

在您的项目中应用 PdfBarcodeReaderOptions 属性,以增强从 PDF 文档中读取条码的能力。 有关条形码无法识别时的其他故障排除技巧,请参阅我们的条形码无法识别指南。

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-pdf-3.cs
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 value
$vbLabelText   $csharpLabel

使用变量名初始化 PdfBarcodeReaderOptions 以访问和调整属性。 在初始化过程中将页码作为参数传递,以便将设置应用到特定页面。 或者,使用 PageNumbers 属性设置页码。

使用继承的 BarcodeReaderOptions 属性,如 ExpectMultipleBarcodesExpectBarcodeTypes 来提高性能和准确性。 将配置好的 PdfBarcodeReaderOptions 作为第二个参数传递给 BarcodeReader.ReadPdf() 并将 PDF 文件路径作为第一个参数。

要处理条形码不完善或损坏的 PDF,请探索我们的 图像校正功能,这些功能可在 PDF 处理过程中应用。

常见问题解答

如何用 C# 从 PDF 文件中读取 BarCode?

IronBarcode 提供了一个简单的 ReadPdf 方法,允许您直接从 PDF 文档中读取条码,而无需先将其转换为图像。您只需一行代码即可从 PDF 文件中提取条码数据: var results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf");

BarCode 阅读器接受哪些类型的 PDF 输入?

IronBarcode 的 BarcodeReader.ReadPdf() 方法接受多种 PDF 输入类型,包括:字节数组、字节数组集合、MemoryStream 对象、MemoryStream 集合、文件路径字符串和文件路径字符串集合。这种灵活性使您可以处理各种来源的 PDF。

读取 BarCode 之前需要将 PDF 转换为图像吗?

不,IronBarcode 可直接从 PDF 文档中读取条形码,无需转换为图像。该库可原生处理 PDF 文件,从而节省时间并保留条码数据的原始质量。

实现 PDF BarCode 阅读的基本步骤是什么?

使用 IronBarcode 从 PDF 中读取条形码:1) 通过 NuGet 安装条码库,2) 如果需要高级设置,创建 PdfBarcodeReaderOptions,3) 使用 BarcodeReader 的 ReadPdf 方法,4) 可选使用 BarcodeReaderOption 指定其他读取选项,5) 从结果中提取条码值。

我能否为 PDF 条码提取配置高级读取选项?

是的,IronBarcode 通过 PdfBarcodeReaderOptions 支持高级读取功能。这允许您使用特定参数和选项自定义条码读取过程,以优化您特定使用情况下的检测和准确性。

哪些类型的文档可以受益于 PDF 条码阅读?

IronBarcode 的 PDF 条码读取是涉及发票、运输标签、报告和任何其他包含条码数据的商业文档的自动化工作流程的理想选择。这样就无需手动扫描打印条码,简化了文档处理过程。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 2,070,733 | 版本: 2026.2 刚刚发布