IRONSOFTWAREHOME

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

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Updated: 2026年4月22日

IronBarcode 使得可以直接從 PDF 文件讀取條形碼,而不需要先轉換為圖像,使用 ReadPdf 方法可以在一行程式碼中提取發票、運單標籤和報告中的條形碼資料。

從 PDF 文件讀取條形碼意味著在 PDF 頁面內檢測並解碼條形碼。 這項技術直接從數位文件中提取編碼的資訊,消除了手動掃描印刷條形碼的需求。 它自動化處理包含條形碼資料的發票、運單標籤、報告和其他文件的工作流程。

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

using IronBarcodeReadPdf 方法從 PDF 中讀取條形碼,而不需要轉換為圖像。 用一行程式碼提取條形碼資料,然後根據需要新增高級選項。

  1. 1Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

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

    var results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf");
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronBarcode,透過免費試用
    arrow pointer

讀取 PDF 條形碼的基本步驟是什麼?

  1. 安裝條形碼庫以處理條形碼文件。 請查看我們的NuGet 套件指南,瞭解特定平台的安裝。
  2. 如有需要,建立 PdfBarcodeReaderOptions
  3. 使用來自 BarcodeReaderReadPdf 方法從 PDF 中讀取條形碼。
  4. 使用 BarcodeReaderOption 指定額外的條形碼讀取選項。
  5. 提取條形碼值。

如何直接從 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 文件中的條形碼的方法:

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());
}

將 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);
    }
}

此程式碼從目錄中檢索所有 PDF 文件,將它們新增到 List<string>,並調用 ReadPdfs 並將列表作為輸入。 該方法返回 BarcodeResults 陣列。 迴圈遍歷結果以存取每個 PDF 的條形碼。

如何配置 PDF 條形碼閱讀器選項?

using 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
};

探索在 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 文件的條形碼讀取。 若條形碼無法識別,有關其它故障排除提示,請參閱我們的條形碼未識別指南。

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());
}

用變數名稱初始化 PdfBarcodeReaderOptions 以存取和調整屬性。 在初始化時將頁碼作為參數傳遞,以便將設定應用於特定頁面。 或者使用 PageNumbers 屬性設定頁碼。 using BarcodeReaderOptions 繼承的屬性,如 ExpectMultipleBarcodesExpectBarcodeTypes 提高性能和準確性。 將配置好的 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 條碼讀取非常適合自動化包含條碼資料的發票、運單、報告及其他商業文件的工作流程。這樣可以免去對印刷條碼的手動掃描,簡化文件處理。

How can I read barcodes from different types of PDF input formats?

IronBarcode's `ReadPdf` method accepts various PDF input types including file paths as strings, byte arrays, and streams, facilitating flexibility in handling different input sources.

Can I extract barcodes from PDFs without converting to images?

Yes, IronBarcode allows you to extract barcodes directly from PDF documents using the `ReadPdf` method, bypassing the need for image conversion.

What scale factor is recommended for small barcodes in PDFs?

A higher scale factor, like 5.0 or higher, is recommended for reading small barcodes as it helps in enlarging the barcode images, making them easier to read.

How can I handle multiple barcodes in a single PDF document?

You can use the `ExpectMultipleBarcodes` property in `PdfBarcodeReaderOptions` to instruct IronBarcode to detect and decode multiple barcodes within the same PDF page.

準備好開始了嗎?

Nuget Downloads 2,422,100版本:2026.9剛剛發布

立即獲取免費

立即獲取 30天試用金鑰

bullet_checked無需信用卡或註冊帳號
bullet_test在生產
環境中進行測試,且不顯示浮水印
bullet_calendar30 天全
功能產品
bullet_support試用期間提供 24/5 技術
支援
立即獲取您的免費30天試用金鑰
不需要信用卡或帳戶建立
C# NuGet程式庫,用於PDF
通過NuGet安裝

版本: 2026.9

PM > Install-Package BarCode
nuget.org/packages/BarCode/
  1. 在解決方案資源管理器中,右鍵點擊References,管理NuGet包
  2. 選擇瀏覽並搜尋"IronBarCode"
  3. 選擇包並安裝
C# PDF DLL
下載DLL

版本: 2026.9

  1. 下載並解壓IronBarCode至您的Solution目錄中的~/Libs等位置
  2. 在Visual Studio解決方案資源管理器中右鍵點擊References,選擇瀏覽,"IronBarCode.dll"

$999起的授權費用

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費即時演示
Booking Badge

全球數百萬工程師信賴

Iron Software的客戶標誌
獲取您的無義務諮詢
完成下方表單或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
全球數百萬工程師信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立