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

如何在 C# 中從 PDF 讀取 BarCode;

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

IronBarcode 可直接從 PDF 文件中讀取條碼,而無需先轉換為圖像,使用 ReadPdf 方法,只需一行代碼即可從發票、出貨標籤和報表中提取條碼資料。

從 PDF 文件讀取 BarCode 是指檢測和解碼 PDF 頁面內的條碼。 此技術可直接從數位文件中擷取編碼資訊,省去手動掃描印刷條碼的工作。 它可以自動化處理發票、出貨標籤、報告和其他包含 BarCode 資料的文件的工作流程。

快速入門:直接從 PDF 讀取條碼

<! -- 引言實作示意圖 --> <!--說明:說明程式碼概念的圖表或截圖 -->

使用 IronBarcodeReadPdf 方法從 PDF 中讀取條碼,而無需轉換為圖像。 只需一行程式碼即可擷取 BarCode 資料,然後視需要新增進階選項。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronBarcode

    PM > Install-Package BarCode

  2. 複製並運行這段程式碼。

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

    立即開始在您的專案中使用 IronBarcode,免費試用!
    arrow pointer

讀取 PDF BarCode 的基本步驟是什麼?

  1. 安裝條碼庫以處理條碼檔案。 請參閱我們的 NuGet 套件指南,以瞭解特定平台的安裝方式。
  2. 如有需要,建立PdfBarcodeReaderOptions
  3. 使用BarcodeReaderReadPdf方法從 PDF 讀取條碼。
  4. 使用BarcodeReaderOption指定其他條碼讀取選項。
  5. 提取條碼值。

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

<! -- 輸出顯示在 IronPDF 中直接從 pdf 文件讀取條碼的結果 --> --> <!--說明:顯示程式碼執行輸出或結果的截圖 -->

IronBarcode 可直接從 PDF 文件讀取條碼,而無需轉換為影像。 如需全面瞭解所有功能,請造訪我們的 功能頁面。 使用 BarcodeReader.ReadPdf() 方法,它接受這些 PDF 輸入類型:

  • byte[]陣列:PDF 文件以位元組數組形式表示。
  • IEnumerable<Byte[]> :以位元組數組形式儲存在集合中的 PDF 文件。
  • MemoryStream :將 PDF 文件作為 MemoryStream 類型。
  • IEnumerable<Stream> :PDF 文件作為 MemoryStream 的集合。 請參閱我們的 Read Barcode from Streams 指南。
  • 字串:如果複製到專案中,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
$vbLabelText   $csharpLabel

將 PDF 檔案路徑字串傳給 BarcodeReader.ReadPdf() 以讀取條碼值。 如需從不同來源讀取 BarCode 的更多範例,請查看我們的 Reading Barcodes in C# / .NET 教學。 若要列印在 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 條碼閱讀器選項?

<! -- 圖解說明設定 pdf 條碼閱讀器選項的實作 --> --> <!--說明:說明程式碼概念的圖表或截圖 -->

使用 PdfBarcodeReaderOptions 設定 PDF 的條碼讀取。 如需所有閱讀器設定的詳細說明,請造訪我們的 Set PDF BarCode Reader Options 范例。 調整這些屬性可改善 品質、準確性和效能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)
$vbLabelText   $csharpLabel

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

DPI 設定如何影響條碼讀取?

設定 PDF 文件中條碼影像的 DPI (每英吋點數)。 這可改善低品質 BarCode 的讀取。 使用 Integer 值。 預設 DPI 為 150。對於較小或品質較低的 BarCode,可提高至 300 或 600 以獲得更好的辨識度。 較高的 DPI 值會增加處理時間和記憶體使用量。

何時應該指定頁碼?

指定包含 BarCode 的頁碼以改善效能,尤其是多頁 PDF。 IronBarcode 在您提供特定頁碼時,會跳過沒有條碼的頁面。 頁碼以 1 為基礎(第一頁為 1,而非 0)。 有關大型文件的優化技巧,請參閱我們的 閱讀速度選項指南。

如何處理有密碼保護的 PDF?

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

小尺寸 BarCode 應該使用什麼比例係數?

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

如何從 PDF 實作進階的 BarCode 讀取?

在您的專案中套用 PdfBarcodeReaderOptions 屬性來增強 PDF 文件中的條碼讀取。 如需條碼無法辨識時的其他疑難排解技巧,請參閱我們的 Barcode Not Recognized 指南。

: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 檔案路徑作為第一個參數。

若要處理條碼不完善或損壞的 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 條碼讀取是涉及發票、出貨標籤、報告和任何其他包含條碼資料的商業文件的自動化工作流程的理想選擇。這消除了手動掃描印刷條碼的需要,並簡化了文件處理。

Hairil Hasyimi Bin Omar
軟體工程師
就像所有優秀的工程師一樣,Hairil 也是一位狂熱的學習者。他不斷精進 C#、Python 和 Java 的知識,利用這些知識為整個 Iron Software 的團隊成員增加價值。Hairil 從馬來西亞的 Universiti Teknologi MARA 大學加入 Iron Software 團隊,畢業於該校的化學與流程工程學系,並取得學士學位。
準備好開始了嗎?
Nuget 下載 2,070,733 | 版本: 2026.2 剛剛發布