如何从 PDF 文档中读取条形码

Hairil related to 如何从 PDF 文档中读取条形码
海瑞尔 哈西米 本 奥马尔
2023年三月19日
更新 2024年十月20日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

How to Read Barcode From PDF in C#

  1. 安装条形码库以处理条形码文件。

  2. 如有需要,创建PdfBarcodeReaderOptions

  3. 使用 BarcodeReaderReadPdf 方法从 PDF 中读取条形码。

  4. 使用BarcodeReaderOption指定其他条形码读取选项。

  5. 提取条形码值。



    <

直接从PDF文档中读取条形码

除了IronBarcode能够从图像中读取条形码外,IronBarcode还特别能够从PDF文档中读取条形码。 这省去了用户在将PDF文档转换成图片以便于使用IronBarcode进行读取的麻烦。 由于PDF文档比图像更复杂且不同,应该使用不同的读取方法,即BarcodeReader.ReadPdf()方法。 该方法接受各种类型的PDF文档输入,包括:

  • byte [] array:PDF文档作为字节数组。
  • IEnumerable : PDF documents as byte arrays stored in a collection.
  • MemoryStream:PDF 文档作为 MemoryStream 类型。
  • IEnumerable : PDF documents as collection of MemoryStream
  • 字符串:PDF文档路径字符串。如果PDF文档已经被复制到项目中,这将是PDF文档的名称字符串。
  • IEnumerable : PDF document path/name strings stored in a collection.

除了上述提到的输入类型之外,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.ReadPdf(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.ReadPdf(docs) 'can also accept individual PDF document file path as argument

For Each value In myBarcode
	Console.WriteLine(value.ToString())
Next value
$vbLabelText   $csharpLabel

从上面的代码片段中,我们可以看到,要使用IronBarcode读取条形码,我们只需将PDF文档的文件路径字符串添加到BarcodeReader.ReadPdf()方法中以读取条形码值,并将结果存储在一个变量中。 如果您希望将 PDF 文档中找到的所有条形码的值打印到控制台上,只需使用 foreach 循环迭代,并通过调用 ToString() 方法来打印变量中找到的每个元素。 除此之外,上面的代码片段还演示了使用包含PDF文档名称集合作为BarcodeReader.ReadPdf()参数。

但如果PDF文档中的条形码无法读取怎么办? 如果性能非常慢怎么办? 这是进行高级PDF条码读取的地方,我们将在这里操作PdfBarcodeReaderOptions以提高读取质量、准确性和性能。

设置 PDF 条码阅读器选项

与从图像读取条形码相同,从PDF文档读取条形码也允许用户调整或修改条形码读取器中的属性,称为PdfBarcodeReaderOptions。 调整PdfBarcodeReaderOptions中的属性将极大地有助于读取质量、准确性以及性能BarcodeReaderOptions中的所有可调整属性在PdfBarcodeReaderOptions中被继承,并且增加了一些用于PDF文档的附加属性。 首先,用户可以指定 PDF 文档中的页码一组页码,以便在实例化新的 PdfBarcodeReaderOptions 实例时应用 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

现在,让我们发现PdfBarcodeReaderOptions中可以操作的其他属性,除了BarcodeReaderOptions中提供的那些属性之外。

DPI

用户可以指定PDF文档中条形码图像的DPI(每英寸点数)。 这将有助于读取PDF文件中质量较低的条形码图像。 此属性可以使用整数值进行设置。

页码

如果用户事先知道PDF文档中包含需要读取的条形码的页面号码,用户可以在此属性中指定它们。 这样做将极大地提高IronBarcode的阅读性能,特别是对于那些有许多页面的PDF文档,因为IronBarcode不需要阅读所有页面或者没有条码的页面。 此属性为基于 1 的,意味着 PDF 文档的第一页是 1 而不是 0。

密码

如名称所示,此属性使用户能够处理需要输入密码才能访问PDF文档内容的加密PDF文件。 请注意,IronBarcode无法为PDF文档设置密码。 此属性将接受字符串输入。

规模

此属性使用户能够在转换为图像时控制缩放因子以缩放宽度和高度。 此属性接受整数作为值,此属性的默认值为3.5。设置此属性将有助于读取PDF文档中存在的小型条形码,因为放大将会缩放PDF文档。

从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

上面的代码片段演示了如何在IronBarcode中实现PdfBarcodeReaderOptions属性。 PdfBarcodeReaderOptions需要先用变量名进行初始化,然后才能访问和调整属性。 在代码片段中,我们还可以看到,PDF文档的页码列表在初始化PdfBarcodeReaderOptions时被用作参数。 这指定了我们希望PdfBarcodeReader设置应用的页码。 用户还可以在PdfBarcodeReaderOptions属性中将PDF页码指定为PageNumbers

另一方面,我们也可以看到,我们可以在PdfBarcodeReaderOptions中使用BarcodeReaderOptions的属性,例如ExpectMultipleBarcodesExpectBarcodeTypes,因为它们是从原始类继承的。 这将大大提高整体阅读性能和准确性。 要在条码读取中应用PdfBarcodeReaderOptions的设置属性,请将我们创建的PdfBarcodeReaderOptions类的变量名作为BarcodeReader.ReadPdf()方法的第二个参数,以及要读取的PDF文档的文件路径作为第一个参数。

Hairil related to 从PDF文档中高级条形码读取
海瑞尔 哈西米 本 奥马尔
软件工程师
像所有优秀的工程师一样,Hairil 是一个热衷学习的人。他正在精进自己的 C#、Python 和 Java 知识,并利用这些知识为 Iron Software 团队成员增添价值。Hairil 毕业于马来西亚的马来西亚工艺大学(Universiti Teknologi MARA),获得了化学与工艺工程学士学位,然后加入了 Iron Software 团队。