如何在 C# 中從 PDF 讀取條碼
IronBarcode 可以直接從PDF文件中讀取條碼,而不需要先轉換為影像,使用 ReadPdf 方法即可從發票、運送標籤和報告中擷取條碼資料,只需一行程式碼。
從 PDF 文件讀取條碼是指偵測和解碼 PDF 頁面內的條碼。 此技術可直接從數位文件中擷取編碼資訊,省去手動掃描印刷條碼的工作。 它可以自動化處理發票、出貨標籤、報告和其他包含條碼資料的文件的工作流程。
快速開始:直接從PDF中讀取條碼使用 IronBarcode 的 ReadPdf 方法,從PDF中讀取條碼而不需轉換為影像。 只需一行程式碼即可擷取條碼資料,然後視需要新增進階選項。
-
1使用 IronBarcode 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf");Dim results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf")Dim results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronBarcode,並使用 免費試用
讀取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文件中的條碼:
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());
}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 valueImports 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 清單中有效率地抽取條碼。 若需處理文件中的多個條碼,請參閱我們的指南讀取多個條碼。
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);
}
}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
NextImports 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 時指定頁碼:
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
};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)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 (每英吋點數)。 這可改善低品質條碼的讀取。 使用 Integer 值。 預設 DPI 為 150。對於較小或品質較低的條碼,可提高至 300 或 600 以獲得更好的辨識度。 較高的 DPI 值會增加處理時間和記憶體使用量。
我應該在什麼時候指定頁碼?
指定包含條碼的頁碼以改善效能,尤其是多頁 PDF。 當您提供特定頁碼時,IronBarcode 將跳過沒有條碼的頁面。 頁碼以 1 為基礎(第一頁為 1,而非 0)。 如需大型文件的最佳化技術,請參考我們的 讀取速度選項 指南。
我如何處理加密的PDF文件?
透過提供密碼作為 String 輸入來處理加密的 PDF 檔案。 IronBarcode 無法檢索PDF密碼。 確保您擁有必要的權限,並在應用程式中安全地儲存密碼。
小型條碼應使用什麼縮放係數?
轉換為影像時,可控制寬度和高度的縮放係數。接受 Integer 值,預設值為 3.5。提高縮放係數可放大 PDF,協助讀取小型條碼。對於小於 1 英吋的條碼,請使用 5.0 或更高的縮放係數。較高的縮放係數會影響效能。
我如何從PDF中實施進階的條碼讀取?
在您的專案中應用 PdfBarcodeReaderOptions 屬性來增強從PDF文件中讀取條碼。 當條碼無法識別時的附加疑難解答提示請參照我們的 條碼未識別 指南。
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());
}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 valueImports 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處理期間應用。
常見問題
如何使用C#從PDF檔中讀取條碼?
您可以使用IronBarcode程式庫從PDF檔案中讀取條碼,該程式庫提供了ReadPdf方法,允許您直接從PDF文件中提取條碼數據,而無需將其轉換為圖像。
我應該使用哪種方法一次讀取多個PDF文件?
使用IronBarcode中的ReadPdfs方法來處理和提取多個PDF文件中的條碼。此方法高效處理PDF文件列表。
如何配置PDF文件的條碼閱讀器選項?
您可以使用PdfBarcodeReaderOptions類來配置條碼閱讀器選項,以設置如DPI、特定頁碼以及其他高級選項,以提高條碼閱讀的準確性和性能。
如果我的PDF文件是受密碼保護的,我應該怎麼辦?
對於受密碼保護的PDF文件,將密碼作為字串輸入提供給IronBarcode。確保您擁有必要的許可權並安全保存密碼。
DPI設置如何影響PDF中的條碼閱讀?
設置較高的DPI(每英寸點數)可以提高低質量條碼在PDF文件中的可讀性。增加DPI可以幫助識別較小或低質量的條碼,但可能也會增加處理時間。
在從PDF中讀取條碼時,我應該指定頁碼嗎?
指定頁碼可以提高性能,尤其是在多頁PDF中。IronBarcode會跳過沒有條碼的頁面,從而優化閱讀過程。
如何從不同類型的PDF輸入格式中讀取條碼?
IronBarcode的ReadPdf方法接受多種PDF輸入類型,包括文件路徑字串、字節陣列和流,從而靈活處理不同的輸入來源。
我可以在不轉換圖像的情況下從PDF中提取條碼嗎?
可以,IronBarcode允許您使用ReadPdf方法直接從PDF文件中提取條碼,無需圖像轉換。
對於PDF中的小條碼,建議使用什麼比例因子?
建議使用較高的比例因子,如5.0或更高,以幫助放大條碼圖像,使其更易讀取。
如何處理單個PDF文件中的多個條碼?
您可以在PdfBarcodeReaderOptions中使用ExpectMultipleBarcodes屬性來指示IronBarcode在同一個PDF頁面中檢測和解碼多個條碼。