如何在 C# 中讀取 PDF 中的條碼
IronBarcode 可直接從 PDF 文件中讀取條碼,無需先轉換為圖像,使用 ReadPdf 方法只需一行程式碼即可從發票、貨運標籤和報告中提取條碼資料。
從 PDF 文件中讀取條碼是指偵測和解碼 PDF 頁面中的條碼。 這項技術直接從數位文件中提取編碼訊息,無需手動掃描列印的條碼。 它可以自動處理包含條碼資料的發票、出貨標籤、報告和其他文件的工作流程。
快速入門:直接從 PDF 讀取條碼
使用 IronBarcode 的 ReadPdf 方法從 PDF 讀取條碼,而無需將其轉換為圖像。 用一行程式碼提取條碼數據,然後根據需要添加高級選項。
讀取PDF條碼的基本步驟是什麼?
- 安裝條碼庫以處理條碼檔案。 請查看我們的NuGet套件指南,以了解特定平台的安裝方法。
- 如有需要,建立
PdfBarcodeReaderOptions。 - 使用
BarcodeReader中的ReadPdf方法從 PDF 讀取條碼。 - 使用
BarcodeReaderOption指定其他條碼讀取選項。 - 提取條碼值。
如何直接從PDF文件中讀取條碼?
IronBarcode 直接從 PDF 文件讀取條碼,無需轉換為影像。 如需全面了解所有功能,請造訪我們的功能頁面。 使用 BarcodeReader.ReadPdf() 方法,該方法接受以下 PDF 輸入類型:
byte[]陣列:PDF 文件作為位元組數組。IEnumerable<Byte[]>: 將 PDF 文件作為位元組陣列儲存在集合中。MemoryStream: PDF 文件為 MemoryStream 類型。IEnumerable<Stream>: PDF 文件作為 MemoryStream 的集合。 請參閱我們的"從串流媒體讀取條碼"指南。String: 如果複製到專案中,則 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
將 PDF 檔案路徑字串傳遞給 BarcodeReader.ReadPdf() 以讀取條碼值。 有關從不同來源讀取條碼的更多範例,請查看我們的C# / .NET讀取條碼教學。 若要列印 PDF 中找到的所有條碼值,請使用 foreach 循環遍歷結果,並對每個元素呼叫 ToString()。 此範例也示範如何使用 PDF 文件名稱集合作為方法參數。
如何同時閱讀多個PDF文件?
IronBarcode 提供了一個 ReadPdfs 方法來同時處理多個 PDF 檔案。 該方法能夠有效率地從 PDF 清單中提取條碼。 有關如何處理文件中的多個條碼,請參閱我們的《讀取多個條碼》指南。
: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
這段程式碼從目錄中檢索所有 PDF 文件,將它們添加到 List<string>,並將清單作為輸入調用 ReadPdfs。 此方法傳回一個陣列 BarcodeResults。 遍歷結果以存取每個 PDF 中的條碼。
如何配置PDF條碼閱讀器選項?
使用 PdfBarcodeReaderOptions 設定從 PDF 讀取條碼。 有關所有閱讀器設定的詳細說明,請造訪我們的"設定 PDF 條碼閱讀器選項"範例。 調整這些屬性可以提高品質、準確性和性能。 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)
探索 PdfBarcodeReaderOptions 中除從 BarcodeReaderOptions 繼承的屬性之外的其他屬性。
DPI設定如何影響條碼讀取?
設定 PDF 文件中條碼影像的 DPI(每吋點數)。 這樣可以提高低品質條碼的讀取效果。 請使用整數值。 預設 DPI 為 150。對於較小或品質較低的條碼,請增加到 300 或 600 以獲得更好的識別效果。 DPI值越高,處理時間和記憶體佔用就越高。
何時該指定頁碼?
指定包含條碼的頁碼可以提高效能,尤其對於多頁 PDF 檔案。 IronBarcode 當您提供特定頁碼時,跳過沒有條碼的頁面。 頁碼從 1 開始(第一頁是 1,而不是 0)。 有關優化大型文件的技巧,請參閱我們的閱讀速度選項指南。
如何處理受密碼保護的PDF檔案?
透過提供密碼作為字串輸入來處理加密的 PDF 檔案。 IronBarcode 無法檢索 PDF 密碼。 請確保您擁有必要的權限,並將密碼安全地儲存在您的應用程式中。
小條碼應該使用多大的比例因子?
轉換影像時,控制寬度和高度的縮放比例。 接受一個整數值,預設值為 3.5。較高的縮放因子可以透過放大 PDF 來幫助讀取較小的條碼。 對於小於 1 英吋的條碼,請使用 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
使用變數名稱初始化 PdfBarcodeReaderOptions,以便存取和調整屬性。 在初始化時將頁碼作為參數傳遞,以便將設定套用至特定頁面。 或者,使用 PageNumbers 屬性設定頁碼。
使用繼承的 BarcodeReaderOptions 屬性,如 ExpectMultipleBarcodes 和 ExpectBarcodeTypes,以提高效能和準確性。 將配置好的 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 物件、MemoryStreams 集合、檔案路徑字串以及檔案路徑字串集合。這種靈活性可讓您處理來自不同來源的 PDF。
在讀取 BarCode 之前,是否需要將 PDF 轉換為影像?
不,IronBarcode 可直接從 PDF 文件讀取條碼,而不需要任何圖像轉換。該函式庫可原生處理 PDF 檔案,不僅節省時間,還能保留條碼資料的原始品質。
實現 PDF 條碼讀取的基本步驟是什麼?
使用 IronBarcode 從 PDF 讀取條碼:1) 通過 NuGet 安裝條碼函式庫,2) 如果需要進階設定,請創建 PdfBarcodeReaderOptions,3) 使用 BarcodeReader 的 ReadPdf 方法,4) 可選擇使用 BarcodeReaderOption 指定其他讀取選項,5) 從結果中提取條碼值。
我可以設定 PDF 條碼擷取的進階讀取選項嗎?
是的,IronBarcode 透過 PdfBarcodeReaderOptions 支援進階讀取功能。這允許您使用特定的參數和選項自定義條碼讀取過程,以優化特定使用情況下的檢測和精確度。
哪些類型的文件可以受益於 PDF 條碼讀取?
IronBarcode 的 PDF 條碼讀取是涉及發票、出貨標籤、報告和任何其他包含條碼資料的商業文件的自動化工作流程的理想選擇。這消除了手動掃描印刷條碼的需要,並簡化了文件處理。

