如何在 C# 中從 PDF 讀取條形碼
IronBarcode 使得可以直接從 PDF 文件讀取條形碼,而不需要先轉換為圖像,使用 ReadPdf 方法可以在一行程式碼中提取發票、運單標籤和報告中的條形碼資料。
從 PDF 文件讀取條形碼意味著在 PDF 頁面內檢測並解碼條形碼。 這項技術直接從數位文件中提取編碼的資訊,消除了手動掃描印刷條形碼的需求。 它自動化處理包含條形碼資料的發票、運單標籤、報告和其他文件的工作流程。
快速入門:從 PDF 直接讀取條形碼
using 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>:作為MemoryStream集合的 PDF 文件。 請查看我們的從流中讀取條形碼指南。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 條形碼閱讀器選項?
using 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。對於較小或質量較低的條形碼,將 DPI 提高到 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 屬性設定頁碼。
using BarcodeReaderOptions 繼承的屬性,如 ExpectMultipleBarcodes 和 ExpectBarcodeTypes 提高性能和準確性。 將配置好的 PdfBarcodeReaderOptions 作為第二個參數傳遞給 BarcodeReader.ReadPdf(),PDF 文件路徑作為第一個參數。
對於處理條形碼不完美或損壞的 PDF,請探索可以在 PDF 處理過程中應用的圖像校正功能。
常見問題
如何在 C# 中從 PDF 文件中讀取條碼?
IronBarcode 提供了一個簡單的 ReadPdf 方法,它允許您直接從 PDF 文件中讀取條碼而無需先將其轉換為圖像。您只需一行程式碼即可從 PDF 中提取條碼資料:var results = IronBarCode.BarcodeReader.ReadPdf('invoice.pdf');
條碼讀取器接受哪種型別的 PDF 輸入?
IronBarcode 的 BarcodeReader.ReadPdf() 方法接受多種 PDF 輸入型別,包括:字節陣列、字節陣列集合、MemoryStream 物件、MemoryStream 的集合、文件路徑字串及文件路徑字串的集合。此靈活性允許您使用不同來源的 PDF 文件。
我是否需要在讀取條碼前先將 PDF 文件轉換為圖像?
不需要,IronBarcode 可直接從 PDF 文件中讀取條碼而不需要任何圖像轉換。該程式庫本地處理 PDF 文件,這節省了時間並保持了條碼資料的原始品質。
實現 PDF 條碼讀取的基本步驟是什麼?
using IronBarcode 從 PDF 讀取條碼的步驟:1)透過 NuGet 安裝條碼程式庫,2)如果需要高級設定,請建立 PdfBarcodeReaderOptions,3)使用 BarcodeReader 的 ReadPdf 方法,4)可選用 BarcodeReaderOption 指定附加的讀取選項,5)從結果中提取條碼值。
我可以為 PDF 條碼提取配置高級讀取選項嗎?
可以,IronBarcode 支持透過 PdfBarcodeReaderOptions 進行高級讀取選項配置。這允許您使用特定的參數和選項來自定義條碼讀取過程,以優化檢測和準確性。
哪些型別的文件可受益於 PDF 條碼讀取?
IronBarcode 的 PDF 條碼讀取非常適合自動化包含條碼資料的發票、運單、報告及其他商業文件的工作流程。這樣可以免去對印刷條碼的手動掃描,簡化文件處理。
IronBarcode是否提供自定義條碼外觀的支持?
是的,IronBarcode提供了廣泛的條碼外觀自定義選項,包括顏色、大小和文字註釋,讓您可以根據具體設計需求定制條碼。
IronBarcode如何幫助改善業務流程效率?
IronBarcode通過使條碼生成和讀取快速且準確來提高業務流程效率,減少手動資料輸入錯誤,並改善庫存和資產追蹤。
將IronBarcode實現於專案中需要什麼程式設計技能?
基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文件來指導開發者。
IronBarcode適合於小型專案和大型企業應用嗎?
IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

