如何從PDF文件中讀取條碼
如何在 C# 中從 PDF 讀取條碼
安裝條碼庫以處理條碼文件。
如果需要,創建
PdfBarcodeReaderOptions
。使用
BarcodeReader
的ReadPdf
方法從 PDF 中讀取條碼。使用
BarcodeReaderOption
指定額外的條碼讀取選項。- 提取條碼值。
<
直接從 PDF 文件讀取條碼
除了 IronBarcode 從圖像讀取條碼的能力外,IronBarcode 也以其能從 PDF 文件中讀取條碼而自豪。這節省了用戶將 PDF 文件轉換為圖像再導入 IronBarcode 進行讀取的麻煩。由於 PDF 文件比圖像更為複雜且不同,應使用不同的讀取方法,那就是 BarcodeReader.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 文件路徑/名稱字串存儲在集合中。
除了上述提到的輸入類型以外, 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()
讀取條碼值的方法,並將結果存儲在變量中。如果您希望將PDF文件中找到的所有條碼值打印到控制台,只需使用 foreach
遍歷循環並通過呼叫列印變量中找到的每個元素 字串轉換()
方法。另外,上述代碼片段還演示了使用一組 PDF 文件名稱作為參數。 BarcodeReader.ReadPdf()
但是,如果 PDF 文件中的條碼無法被讀取怎麼辦?如果性能非常慢怎麼辦?這就是 IronBarcode 的用武之地。 進階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)
現在讓我們來探索另外的屬性 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
上面的程式碼片段展示了如何實現 Pdf條碼讀取器選項
IronBarcode 中的屬性。 Pdf條碼讀取器選項
首先需要使用變數名進行初始化,然後才能訪問和調整屬性。在代碼片段中,我們還可以看到,PDF 文件的頁碼列表被用作初始化時的參數。 Pdf條碼讀取器選項
. 這指定了我們想要設置的頁碼 PdfBarcodeReader
申請。用戶也可以指定 PDF 頁碼在 Pdf條碼讀取器選項
屬性為 頁碼
另一方面,我們也可以看到我們可以使用屬性从 條碼讀取器選項
例如 預期多個條碼
和 期望條碼類型
在 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條碼讀取器選項
我們創建的類作為第二個參數 BarcodeReader.ReadPdf()
method, with PDF document to be read file path as the first argument.