如何在 C# 中從 PDF 中讀取條碼

如何在 C# 中讀取 PDF 中的 BARCODE

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode 支援直接從 PDF 文件讀取 BarCode,無需事先轉換為圖像;透過 ReadPdf 方法,僅需一行程式碼即可從發票、運送標籤及報告中擷取 BarCode 資料。

從 PDF 文件中讀取 BarCode,意指偵測並解碼 PDF 頁面內的 BarCode。 這項技術能直接從數位文件中提取編碼資訊,省去了手動掃描紙本BarCode的步驟。 它可自動化處理發票、運送標籤、報告及其他包含BarCode資料的文件之工作流程。

快速入門:直接從 PDF 讀取 BarCode

使用 IronBarcodeReadPdf 方法,可直接從 PDF 讀取 BARCODE,無需轉換為圖片。 只需一行程式碼即可擷取BARCODE資料,並可視需要新增進階選項。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 請複製並執行此程式碼片段。

    var results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf");
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronBarcode

    arrow pointer

讀取 PDF BARCODE 的基本步驟有哪些?

  1. 安裝BarCode函式庫以處理條碼檔案。 請參閱我們的 NuGet 套件指南,了解各平台的安裝方式。
  2. 如有需要,請建立 PdfBarcodeReaderOptions
  3. 使用 ReadPdf 中的 BarcodeReader 方法,從 PDF 檔案中讀取 BARCODE。
  4. 使用 BarcodeReaderOption 指定額外的 BARCODE 讀取選項。
  5. 擷取BarCode值。

如何直接從 PDF 文件讀取 BarCode?

IronBarcode 可直接從 PDF 文件讀取 BARCODE,無需轉換為圖像。 如需了解所有功能的完整概覽,請造訪我們的"功能"頁面。 請使用 BarcodeReader.ReadPdf() 方法,該方法支援以下 PDF 輸入類型:

  • byte[] 陣列:以位元組陣列形式呈現的 PDF 文件。
  • IEnumerable<Byte[]>:以位元組陣列形式儲存於集合中的 PDF 文件。
  • MemoryStream:作為 MemoryStream 類型的 PDF 文件。
  • IEnumerable<Stream>:PDF 文件作為 MemoryStream 的集合。 請參閱我們的《從資料流讀取BarCode》指南。
  • String:PDF 文件路徑(若已複製至專案中,則為字串或檔案名稱)。
  • IEnumerable<String>:儲存在集合中的 PDF 文件路徑/名稱字串。

BarcodeReader.ReadPdf() 方法亦支援 PdfBarcodeReaderOptions 以實現進階讀取功能,相關內容將於下一節中討論。 以下是使用 BarcodeReader.ReadPdf() 讀取 PDF 文件中 BarCode 的方法:

: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
$vbLabelText   $csharpLabel

將 PDF 檔案路徑字串傳遞給 BarcodeReader.ReadPdf() 以讀取 BarCode 值。 如需更多關於從不同來源讀取BARCODE的範例,請參閱我們的《C# / .NET BARCODE讀取教學》。 若要列印 PDF 中所有找到的 BARCODE 值,請使用 foreach 迴圈遍歷結果,並對每個元素呼叫 ToString()。 此範例同時展示了如何將一組 PDF 文件名稱作為方法參數傳入。

如何一次讀取多個 PDF 檔案?

IronBarcode 提供 ReadPdfs 方法,可同時處理多個 PDF 檔案。 此方法能高效地從一組 PDF 檔案中擷取 BarCode。 關於處理文件中的多個BarCode,請參閱我們的《讀取多個BarCode》指南。

: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
$vbLabelText   $csharpLabel

此程式碼會從目錄中擷取所有 PDF 檔案,將其加入 List<string>,並將該清單作為輸入參數傳遞給 ReadPdfs。 此方法會傳回一個 BarcodeResults 陣列。 遍歷結果以取得每個 PDF 中的 BARCODE。

如何設定 PDF 條碼讀取器的選項?

使用 PdfBarcodeReaderOptions 設定從 PDF 讀取 BARCODE 的功能。 如需了解所有讀取設定的詳細說明,請參閱我們的《設定 PDF BarCode 讀取器選項》範例。 調整這些屬性可提升品質、準確性與效能PdfBarcodeReaderOptions 繼承了 BarcodeReaderOptions 的所有屬性,並新增了 PDF/A 專屬選項。 在實例化 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)
$vbLabelText   $csharpLabel

探索 PdfBarcodeReaderOptions 中除繼承自 BarcodeReaderOptions 之外的其他可用屬性。

DPI 設定如何影響 BarCode 讀取?

設定 PDF 文件中 BARCODE 影像的 DPI(每英吋點數)。 這能提升低品質BarCode的讀取效果。 請使用整數值。 預設 DPI 為 150。若 BARCODE 較小或品質較差,請將 DPI 調高至 300 或 600 以獲得更好的辨識效果。 較高的 DPI 值會增加處理時間與記憶體使用量。

何時應標示頁碼?

請指定包含 BarCode 的頁碼以提升效能,特別是針對多頁面的 PDF 檔案。 IronBarcode 當您提供特定頁碼時,會跳過沒有BarCode的頁面。 頁碼採用 1 為起點(第一頁為 1,而非 0)。 關於處理大型文件的優化技巧,請參閱我們的《閱讀速度選項指南》。

如何處理受密碼保護的 PDF 檔案?

透過提供密碼作為字串輸入,即可處理加密的 PDF 檔案。 IronBarcode 無法取得 PDF 密碼。 請確保您擁有必要的權限,並在應用程式中安全地儲存密碼。

針對小型BarCode應使用何種縮放係數?

轉換為圖像時,請控制寬度和高度的縮放比例。 接受整數值,預設值為 3.5。較高的縮放係數有助於透過放大 PDF 來讀取小型 BarCode。 對於小於 1 英吋的 BARCODE,請使用 5.0 或更高的縮放係數。 高比例因子會影響效能。

如何在 PDF 中實作進階 BarCode 讀取功能?

在您的專案中套用 PdfBarcodeReaderOptions 屬性,以提升從 PDF 文件讀取 BarCode 的能力。 若BarCode無法被識別,請參閱我們的《BarCode無法識別》指南以獲取更多疑難排解技巧。

: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
$vbLabelText   $csharpLabel

請使用變數名稱初始化 PdfBarcodeReaderOptions,以便存取並調整屬性。 在初始化時將頁碼作為參數傳入,以將設定套用至特定頁面。 或者,可使用 PageNumbers 屬性設定頁碼。

請使用 BarcodeReaderOptions 繼承的屬性(如 ExpectMultipleBarcodesExpectBarcodeTypes),以提升效能與精準度。 請將已設定的 PdfBarcodeReaderOptions 作為 BarcodeReader.ReadPdf() 的第二個參數傳入,並將 PDF 檔案路徑作為第一個參數。

若需處理 BarCode 不完整或受損的 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 條碼讀取的基本步驟是什麼?

要使用 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設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

Hairil Hasyimi Bin Omar
軟體工程師
如同所有傑出的工程師,Hairil 是一位熱衷學習的人。他正不斷精進自己在 C#、Python 和 Java 方面的知識,並運用這些知識為 Iron Software 的團隊成員創造價值。Hairil 從馬來西亞馬拉科技大學(Universiti Teknologi MARA)加入 Iron Software 團隊,他在該校取得化學與製程工程學士學位。
準備好開始了嗎?
Nuget 下載 2,229,569 | 版本: 2026.5 just released
Still Scrolling Icon

還在滾動嗎?

想快速獲得證據? PM > Install-Package BarCode
執行示例看您的字符串變成條碼。