C#でPDFからバーコードを読む方法
IronBarcode の ReadPdf メソッドを使用すると、PDF文書を画像に変換せずに、請求書、配送ラベル、レポートからバーコードデータをわずか1行のコードで直接抽出できます。
PDF 文書から BarCode を読み取るとは、PDF ページ内のバーコードを検出してデコードすることです。 この技術は、デジタル文書からエンコードされた情報を直接抽出するため、印刷された BarCode を手作業でスキャンする必要がありません。 このツールは、請求書、出荷ラベル、レポート、および BarCode データを含むその他の文書を処理するワークフローを自動化します。
クイックスタート: PDFから直接バーコードを読み取るIronBarcode の ReadPdf メソッドを使用して、画像に変換せずにPDFからバーコードを読み取ります。 1行のコードでBarCodeデータを抽出し、必要に応じて高度なオプションを追加します。
-
1IronBarcode を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
var results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf");Dim results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf")Dim results = IronBarCode.BarcodeReader.ReadPdf("invoice.pdf") -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronBarcodeを使い始めるには、無料トライアルをお試しください。
PDFバーコードを読むための基本的な手順は何ですか?
- バーコードファイルを処理するためのバーコードライブラリをインストールします。 NuGetパッケージガイドをご覧いただき、プラットフォームに特化したインストール手順を確認してください。
- 必要に応じて
PdfBarcodeReaderOptionsを作成してください。 ReadPdfメソッドをBarcodeReaderから使用して、PDFからバーコードを読み取ります。BarcodeReaderOptionを使用して追加のバーコード読み取りオプションを指定します。- バーコードの値を抽出します。
PDF文書から直接バーコードを読むにはどうすればよいですか?
IronBarcodeは、画像に変換せずともPDFドキュメントから直接バーコードを読み取ります。 すべての機能の概要については、機能ページをご覧ください。 以下のPDF入力タイプを受け入れるBarcodeReader.ReadPdf()メソッドを使用します:
byte[]配列:バイト配列としてのPDFドキュメント。IEnumerable<Byte[]>:コレクションに格納されたバイト配列としてのPDFドキュメント。MemoryStream: PDFドキュメントをMemoryStreamタイプとして。IEnumerable<Stream>: PDFドキュメントをMemoryStreamのコレクションとして。 ストリームからのバーコード読み取りガイドをご覧ください。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());
}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 valueImports 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 valuePDFファイルパスの文字列をBarcodeReader.ReadPdf()に渡して、バーコードの値を読み取ります。 さまざまなソースからのバーコードの読み取り例については、C# / .NETでのバーコード読み取りチュートリアルを確認してください。 PDFで見つかったすべてのバーコード値を出力するには、foreach ループを使用して結果を反復処理し、各要素に対して ToString() を呼び出します。 この例では、PDF文書名のコレクションをメソッドの引数として使用する例も示します。
複数のPDFを一度に読むにはどうすればよいですか?
IronBarcode は複数のPDFを同時に処理するための ReadPdfs メソッドを提供します。 この方法では、PDFのリストから効率的にBarCodeを抽出します。 ドキュメント内の複数のバーコードの処理については、複数のバーコードを読み取るガイドをご覧ください。
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);
}
}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
NextImports 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からBarCodeにアクセスします。
PDFバーコードリーダーのオプションを設定するにはどうすればいいですか?
PDFからバーコードを読み取るためにPdfBarcodeReaderOptionsを設定します。 すべてのリーダー設定の詳細な説明については、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
};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)Imports IronBarCode
Imports System.Collections.Generic
Private pageNumber As New List(Of Integer)() From {1, 2, 3}
Private PdfOptions As New PdfBarcodeReaderOptions(pageNumber)BarcodeReaderOptions から継承したプロパティに加え、PdfBarcodeReaderOptions で利用できる追加プロパティを確認します。
DPI設定がバーコードの読み取りにどのように影響しますか?
PDF文書内のバーコード画像のDPI(Dots Per Inch)を設定します。 これにより、低品質のBarCodeの読み取りが向上します。 整数値を使用してください。 デフォルトのDPIは150です。バーコードが小さかったり、品質が低い場合は、300または600に上げると、より認識されやすくなります。 DPI値が高くなると、処理時間とメモリ使用量が増えます。
いつページ番号を指定するべきですか?
特に複数ページのPDFのパフォーマンスを向上させるために、BarCodeを含むページ番号を指定します。 指定したページ番号を提供すると、IronBarcodeはバーコードのないページをスキップします。 ページ番号は1ベースです(最初のページは0ではなく1です)。 大規模なドキュメントでの最適化技術については、読み取り速度オプションガイドをご覧ください。
パスワード保護されたPDFをどのように扱いますか?
パスワードを文字列入力として提供することで、暗号化されたPDFファイルを扱うことができます。 IronBarcode はPDFのパスワードを取得できません。 アプリケーションに必要なパーミッションがあり、パスワードが安全に保存されていることを確認してください。
小さいバーコードにどのスケールファクターを使用すべきですか?
画像に変換するときに、幅と高さのスケールファクターを制御してください。 Integer 値を受け付け、デフォルトは 3.5 です。拡大率を高くすると、PDF をズームして小さな BarCode を読みやすくなります。 1インチ未満のBarCodeには、スケールファクター5.0以上を使用してください。 スケールファクターを高くすると、パフォーマンスに影響します。
PDFからの高度なバーコード読み取りを実装するにはどうすればいいですか?
PDFドキュメントからのバーコード読み取りを向上させるには、プロジェクト内でPdfBarcodeReaderOptionsプロパティを適用します。 認識されないバーコードに関する追加のトラブルシューティングのヒントについては、バーコードが認識されない場合の対処法ガイドを参照してください。
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());
}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 valueImports 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プロパティを使用してページ番号を設定します。
パフォーマンスと精度を向上させるために、BarcodeReaderOptions から継承したプロパティ ExpectMultipleBarcodes および ExpectBarcodeTypes を使用します。 設定された PdfBarcodeReaderOptions を BarcodeReader.ReadPdf() に対する第二引数として渡し、第一引数としてPDFファイルのパスを指定します。
不完全または損傷したバーコードを含むPDFの処理については、PDF処理中に適用できる画像補正機能をご覧ください。
よくある質問
PDFファイルからバーコードをC#で読み取るにはどうすればよいですか?
PDFファイルからバーコードを読み取るには、IronBarcodeライブラリを使用します。このライブラリのReadPdfメソッドを使うことで、画像に変換せずにPDF文書から直接バーコードデータを抽出できます。
複数のPDFファイルを一度に読み取るためにはどのメソッドを使用すればよいですか?
IronBarcodeのReadPdfsメソッドを使用すると、複数のPDFファイルから同時にバーコードを処理および抽出できます。このメソッドはPDF文書のリストを効率的に処理します。
PDFファイルのバーコードリーダーオプションはどのように設定しますか?
PdfBarcodeReaderOptionsクラスを使用して、DPI、特定のページ番号、その他の高度なオプションなどのプロパティを設定することで、バーコードの読み取り精度とパフォーマンスを向上させることができます。
PDF文書がパスワードで保護されている場合はどうすればよいですか?
パスワードで保護されたPDF文書には、IronBarcodeにパスワードを文字列入力として提供してください。必要な権限があることを確認し、パスワードを安全に保管してください。
DPI設定はPDFでのバーコード読み取りにどのように影響しますか?
より高いDPI (Dots Per Inch)を設定すると、低品質のバーコードの可読性が向上します。DPIを高くすることで小さなバーコードや低品質のバーコードの認識が向上しますが、処理時間が長くなる可能性もあります。
PDFからバーコードを読み取る際にページ番号を指定すべきですか?
ページ番号を指定することで、特に複数ページのPDFにおいてパフォーマンスが向上します。IronBarcodeはバーコードのないページをスキップするため、読み取りプロセスを最適化します。
異なるPDF入力形式からバーコードを読むにはどうすればよいですか?
IronBarcodeのReadPdfメソッドは、ファイルパスを文字列、バイト配列、ストリームとして入力するなど、さまざまなPDF入力形式を受け付け、異なる入力ソースを柔軟に扱うことができます。
画像に変換せずにPDFからバーコードを抽出できますか?
はい、IronBarcodeを使用すればReadPdfメソッドを使ってPDF文書から直接バーコードを抽出でき、画像への変換が不要です。
PDFでの小さなバーコードにはどのスケールファクターが推奨されますか?
5.0以上の高いスケールファクターが小さなバーコードを読み取るのに推奨されます。これによりバーコード画像を拡大し、読み取りやすくします。
1つのPDF文書で複数のバーコードをどのように処理しますか?
PdfBarcodeReaderOptionsのExpectMultipleBarcodesプロパティを使用すると、IronBarcodeに同じPDFページ内で複数のバーコードを検出してデコードするように指示できます。