如何从 PDF 文档中读取条形码
如何在 C&num 中从 PDF 读取条形码;
安装条形码库以处理条形码文件。
如果需要,请创建
PdfBarcodeReaderOptions
。使用
BarcodeReader
的ReadPdf
方法来从 PDF 读取条形码。使用
BarcodeReaderOption
指定其他条码读取选项。提取条形码值。
<
直接从PDF文档中读取条形码
除了IronBarcode能够从图像中读取条形码外,IronBarcode还特别能够从PDF文档中读取条形码。 这省去了用户在将PDF文档转换成图片以便于使用IronBarcode进行读取的麻烦。 由于PDF文档比图像复杂且不同,因此也应使用不同的阅读方法,即 BarcodeReader.ReadPdf()
method. 该方法接受各种类型的PDF文档输入,包括:
- 字节 [] 数组 :PDF 文档的字节数组。
- IEnumerable
:将 PDF 文档作为字节数组存储在一个集合中。 - 内存流 :将 PDF 文档作为 MemoryStream 类型。
- IEnumerable
:PDF 文件作为 MemoryStream 的集合 - 字符串 :PDF 文档路径字符串。如果 PDF 文档已复制到项目中,则该字符串将是 PDF 文档的名称。
- IEnumerable
:存储在集合中的 PDF 文档路径/名称字符串。
除了上述类型的输入外、 BarcodeReader.ReadPdf()
还接受 PdfBarcodeReaderOptions
我们将在下一个子主题中讨论更高级/改进的阅读方法。 现在,让我们看看下面的代码片段,它演示了如何使用 BarcodeReader.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
从上面的代码片段可以看出,要使用 IronBarcode 读取条形码,我们只需将 PDF 文档的文件路径字符串添加到 BarcodeReader.ReadPdf()
method to read the barcode value, and store the result in a variable. 如果您希望将PDF文档中找到的所有条形码的值打印到控制台上,请使用 执行
循环进行遍历,并通过调用 ToString()
method on them. 除此之外,上面的代码片段还展示了如何将PDF文档名称的集合作为参数使用。 BarcodeReader.ReadPdf()
.
但如果PDF文档中的条形码无法读取怎么办? 如果性能非常慢怎么办? 这就是在哪里 高级 PDF 条码读取 在这个过程中,我们将操纵 PdfBarcodeReaderOptions
以提高阅读质量、准确性和性能。
设置 PDF 条码阅读器选项
与从图像中读取条形码相同,从PDF文档中读取条形码也允许用户调整或调整条形码阅读器中的属性,称为 PdfBarcodeReaderOptions
. 调整属性 PdfBarcodeReaderOptions
对阅读大有帮助 质量、精度和性能. 所有可调整的属性在 条码阅读器选项
在 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)
现在,让我们来发现 PdfBarcodeReaderOptions
除了可在 条码阅读器选项
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
上面的代码片段演示了如何实现 PdfBarcodeReaderOptions
IronBarcode的属性。 "(《世界人权宣言》) PdfBarcodeReaderOptions
首先需要用变量名初始化,然后才能访问和调整属性。 在代码片段中,我们也可以看到,在初始化时使用了PDF文档的页码列表作为参数。 PdfBarcodeReaderOptions
. 这指定了我们想要设置的页面编号 PdfBarcodeReader
申请。 用户还可以指定PDF页面号。 PdfBarcodeReaderOptions
属性为 页码
.
另一方面,我们也可以看到我们可以使用属性从 条码阅读器选项
例如 ExpectMultipleBarcodes
和 期望条码类型
于 PdfBarcodeReaderOptions
因为它们是从原始类继承的。 这将大大提高整体阅读性能和准确性。 要应用设置的属性 PdfBarcodeReaderOptions
在条形码读取中,输入 PdfBarcodeReaderOptions
中作为第二个参数创建的 BarcodeReader.ReadPdf()
method, with PDF document to be read file path as the first argument.