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

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

海瑞尔 哈西米 本 奥马尔

如何在 C&num 中从 PDF 读取条形码;

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

2.如果需要,创建 PdfBarcodeReaderOptions

3.使用 BarcodeReader 中的 ReadPdf 方法从 PDF 文件中读取条形码。

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

5.提取条码值。



<

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

除了能从图像中读取条形码外,IronBarcode 还能从 PDF 文档中读取条形码。这样,用户就不必先将 PDF 文档转换成图像,然后再输入 IronBarcode 进行读取。由于 PDF 文档更为复杂,且不同于图像,因此也应使用不同的读取方法,这就是 条码阅读器.ReadPdf() method. This method accepts various types of PDF document input, including :

  • 字节 [] 数组 :PDF 文档的字节数组。
  • IEnumerable:将 PDF 文档作为字节数组存储在一个集合中。
  • 内存流 : PDF documents as 内存流 type.
  • IEnumerable: PDF documents as collection of 内存流
  • 字符串 :PDF 文档路径字符串。如果 PDF 文档已复制到项目中,则该字符串将是 PDF 文档的名称。
  • IEnumerable<字符串>:存储在集合中的 PDF 文档路径/名称字符串。

除了上述类型的输入外、 条码阅读器.ReadPdf() 还接受 PdfBarcodeReaderOptions 我们将在下一个子主题中讨论更高级/改进的阅读方法。现在,让我们看看下面的代码片段,它演示了如何使用 条码阅读器.ReadPdf() method to read barcodes in PDF documents.

: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
VB   C#

从上面的代码片段可以看出,要使用 IronBarcode 读取条形码,我们只需将 PDF 文档的文件路径字符串添加到 条码阅读器.ReadPdf() 方法读取条形码值,并将结果存储到变量中。如果您想将 PDF 文档中所有条形码的值打印到控制台,只需使用 执行 循环进行遍历,并通过调用 ToString() 方法。此外,上面的代码片段还演示了使用 PDF 文档名称集合作为 条码阅读器.ReadPdf() .

但如果 PDF 文档中的条形码无法读取怎么办?性能太慢怎么办?这就是 高级 PDF 条码读取 在这个过程中,我们将操纵 PdfBarcodeReaderOptions 以提高阅读质量、准确性和性能。

设置 PDF 条码阅读器选项

与从图像中读取条形码一样,从 PDF 文档中读取条形码也允许用户调整或调整条形码阅读器中的属性,称为 Pdf条码阅读器选项.调整 Pdf条码阅读器选项 对阅读大有帮助 质量、精度和性能.中的所有可调属性 条码阅读器选项Pdf条码阅读器选项为 PDF 文档提供了一些附加属性。首先,用户可以指定 页码collection of 页码s 的 PDF 文档中的 Pdf条码阅读器选项 的新实例时应用 Pdf条码阅读器选项.下面的代码片段演示了

: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)
VB   C#

现在,让我们来发现 Pdf条码阅读器选项 除了可在 条码阅读器选项

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
VB   C#

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

另一方面,我们也可以看到,我们可以使用来自 条码阅读器选项 例如 ExpectMultipleBarcodes期望条码类型Pdf条码阅读器选项 s于ce they are 于herited from the orig于al class. This will greatly help 于 overall read于g performance 和 accuracy. To apply the set properties of Pdf条码阅读器选项 于 the barcode read, 于put the variable name of the Pdf条码阅读器选项 作为第二个参数 条码阅读器.ReadPdf() method, with PDF document to be read file path as the first argument.

海瑞尔 哈西米 本 奥马尔

软件工程师

像所有优秀的工程师一样,Hairil 是一个热衷学习的人。他正在精进自己的 C#、Python 和 Java 知识,并利用这些知识为 Iron Software 团队成员增添价值。Hairil 毕业于马来西亚的马来西亚工艺大学(Universiti Teknologi MARA),获得了化学与工艺工程学士学位,然后加入了 Iron Software 团队。