如何從PDF文件中讀取條碼C#

How to read Barcodes from PDF Documents

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

從 PDF 文件中讀取條碼是檢測和解碼存在於 PDF 文件頁面中的條碼。此技術允許直接從數碼文件中提取編碼信息,而不是手動掃描打印的條碼。 這對於自動化涉及處理使用條碼存儲數據的發票、運單、報告和其他文件的工作流程特別有用。

快速開始:直接從 PDF 中讀取條碼)

使用 IronBarcode 的 ReadPdf 方法直接跳入讀取 — 它能即時從 PDF 中讀取條碼,而無需先轉換成圖像。只需一行代碼,您就可以快速提取條碼數據,然後在需要時開始構建更高級的選項。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf");
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer

as-heading:3(概覽:簡化的工作流程

  1. 安裝條碼庫以處理條碼文件。
  2. 視需要創建 PdfBarcodeReaderOptions
  3. 使用 BarcodeReaderReadPdf 方法從 PDF 中讀取條碼。
  4. 使用 BarcodeReaderOption 指定額外的條碼讀取選項。
  5. 抽取條碼值。

直接從 PDF 文件中讀取條碼

除了 IronBarcodes 能夠從圖像中讀取條碼之外,IronBarcode 還以其能夠從 PDF 文件中讀取條碼而自豪。 這省去了用戶將 PDF 文件轉換為圖像然後再將其導入 IronBarcode 進行閱讀的麻煩。 由於 PDF 文件比圖像更複雜並且有差異,因此應使用不同的讀取方法,即 BarcodeReader.ReadPdf() 方法。 此方法能接受各種類型的 PDF 文件輸入,包括:

  • byte[] 數組:PDF 文件作為字節數組。
  • IEnumerable<Byte[]>:PDF 文件作為存儲在集合中的字節數組。
  • MemoryStream:PDF 文件作為 MemoryStream 類型。
  • IEnumerable<Stream>:PDF 文件作為 MemoryStream 的集合。
  • String:PDF 文件路徑作為字串。 如果 PDF 文件已經被複製到專案中,這將是 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

從上面的代碼片段中,可以看到要使用 IronBarcode 讀取條碼,我們只需將 PDF 文件的文件路徑字符串添加到 BarcodeReader.ReadPdf() 方法中,以讀取條碼值並將結果存儲在變量中。 如果您希望將 PDF 文件中所有找到的條碼的值打印到控制台,只需使用 foreach 循環迭代並打印變量中找到的每個元素,通過調用 ToString() 方法對它們進行處理。 此外,上面的代碼片段還展示了使用 PDF 文件名集合作為 BarcodeReader.ReadPdf() 的參數。

一次讀取多個 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);
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

在上面的代碼中,我們首先從一個目錄中獲取所有 PDF 文件,然後將它們添加到 List<string>,然後調用 ReadPdfs 並將 docs 作為我們的輸入參數。 ReadPdfs 接著返回一個 BarcodeResults 數組。 最後,我們遍歷 docResult 並打印出每個 PDF 中找到的條碼。

設置 PDF 條碼讀取器選項

與從圖像中讀取條碼相似,從 PDF 文件中讀取條碼也允許用戶調整條碼讀取器中的屬性,稱為 PdfBarcodeReaderOptions。 調整 PdfBarcodeReaderOptions 中的屬性將大大有助於提高質量、準確性和性能。 所有在 BarcodeReaderOptions 中可調的屬性都被繼承到 PdfBarcodeReaderOptions 中,並且有一些針對 PDF 文件的附加屬性。 用戶可以在初始化 PdfBarcodeReaderOptions 的新實例時,指定從 PDF 文件中選擇的頁碼頁碼集合。 下面的代碼片段演示:

: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 或每英寸點數。 這將有助於讀取 PDF 文件中質量低的條碼圖像。 此屬性可以使用一個 Integer 值設置。

PageNumbers

如果用戶事先知道 PDF 文件中包含需要讀取的條碼的頁碼,可以在此屬性中指定。 這樣做將大大提高 IronBarcode 的讀取性能,尤其對於具有多頁的 PDF 文件,因為 IronBarcode 不必讀取所有頁面或無需讀取的頁面。 此屬性是基於 1 的,這意味著 PDF 文件的第一頁是 1 而非 0。

Password

顧名思義,此屬性使用戶能够處理需要輸入密碼访问 PDF 文件内容的加密 PDF 文件。 請注意,IronBarcode 無法提供 PDF 文件的密碼。 此屬性將接受 String 輸入。

Scale

此屬性使用戶能够控制在轉換為圖像時縮放寬度和高度的縮放因子。 此屬性接受一個Integer 作為值,此屬性的默認值為 3.5。設置此屬性將有助於讀取 PDF 文件中存在的小條碼,因為放大將放大 PDF 文件。

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

上面的代碼片段演示了如何在 IronBarcode 中實施 PdfBarcodeReaderOptions 屬性。 PdfBarcodeReaderOptions 首先需要以變量名稱初始化,然後才能訪問和調整屬性。 在代碼片段中,我們可以看到使用 PDF 文件的頁碼列表作為初始化 PdfBarcodeReaderOptions 的參數。 這指定了我們希望 PdfBarcodeReader 的設置應用所在的頁碼。 用戶還可以在 PdfBarcodeReaderOptions 屬性中指定 PDF 的頁碼作為 PageNumbers

另一方面,我們還可以看到可以使用來自 BarcodeReaderOptions 的屬性,如 ExpectMultipleBarcodesExpectBarcodeTypesPdfBarcodeReaderOptions 中,因為它們是從原始類別繼承的。 這將大大有助於整體的讀取性能和準確性。 要將 PdfBarcodeReaderOptions 的設置屬性應用到條碼讀取中,將我們創建的 PdfBarcodeReaderOptions 類的變數名稱作為 BarcodeReader.ReadPdf() 方法的第二個參數,並將要讀取的 PDF 文件的文件路徑作為第一個參數。

常見問題解答

如何從 PDF 文件中讀取條碼?

您可以在 .NET 應用程式中使用 BarcodeReader 類的 `ReadPdf` 方法從 PDF 文檔中讀取條碼。此方法接受字節數組、記憶流和文件路徑等輸入。

使用 PdfBarcodeReaderOptions 有什麼優勢?

PdfBarcodeReaderOptions 提供增強條碼讀取質量和準確性的設置,如調整 DPI、指定頁碼以及對具密碼加密的 PDF 處理。

我可以從加密的 PDF 文件中讀取條碼嗎?

可以,您可以使用 PdfBarcodeReaderOptions 中的 Password 屬性提供必要的密碼,以便從加密的 PDF 文件中讀取條碼。

如何提高從 PDF 中讀取條碼的效率?

通過使用 PdfBarcodeReaderOptions 指定頁碼和調整比例因數來提高效率,這樣可以減少將 PDF 轉換為圖像的需求,節省時間和資源。

ReadPdf 方法接受哪些輸入格式?

ReadPdf 方法接受多種輸入格式,包括 byte[] 數組、MemoryStream 和字符串文件路徑。

是否可能從單個 PDF 文件中讀取多個條碼?

是的,通過在 PdfBarcodeReaderOptions 中設置 ExpectMultipleBarcodes 屬性,您可以從單個 PDF 文件中讀取多個條碼。

我該如何指定從 PDF 中讀取條碼的解析度?

通過在 PdfBarcodeReaderOptions 中設置 DPI 屬性為所需的整數值來控制掃描質量。

比例因數在從 PDF 中讀取條碼中起什麼作用?

默認為 3.5 的比例因數用於將 PDF 轉換為圖像,影響條碼讀取的質量和性能。

我可以使用 .NET 條碼庫來從圖像中讀取條碼嗎?

可以,像 IronBarcode 這樣的 .NET 條碼庫可以使用針對每個格式的具體方法從圖像和 PDF 文件中讀取條碼。

如果條碼讀取不夠準確我該怎麼辦?

如果條碼讀取不夠準確,請調整 PdfBarcodeReaderOptions,通過更改 DPI、比例因數或指定正確的頁碼以提高質量。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 1,935,276 | 版本: 2025.11 剛剛發布