IronBarcodeを使用してPDFドキュメントにバーコードを作成してスタンプする方法

C#を使用して PDF に BarCode をスタンプする方法

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

C# を使用して、IronBarcode の CreateBarcode メソッド(単一ページの場合は StampToExistingPdfPage、複数ページの場合は StampToExistingPdfPages)を、座標とページ番号を指定して使用し、既存の PDF 文書に BARCODE をスタンプします。

クイックスタート: 生成されたバーコードを PDF ページにスタンプ

この例では、IronBarcodeのCreateBarcodeを使用してBARCODEを生成し、既存のPDFページにスタンプする方法を示しています。 PDFのパス、位置座標、ページ番号を提供してください。

  1. IronBarcode をNuGetパッケージマネージャでインストール

    PM > Install-Package BarCode
  2. このコード スニペットをコピーして実行します。

    IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150)
        .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1);
  3. 実際の環境でテストするためにデプロイする

    今日プロジェクトで IronBarcode を使い始めましょう無料トライアル

    arrow pointer

StampToExistingPdfPage StampToExistingPdfPages

既存の PDF ページにバーコードをスタンプするにはどうすればよいですか?

IronBarcode では、BARCODE を PDF としてエクスポートするだけでなく、既存の PDF 文書に GeneratedBarcode を直接スタンプすることも可能です。 この機能は、既存のレポート、請求書、フォームにトラッキングコード、在庫ラベル、またはドキュメント識別子を追加する場合に便利です。 次のコード・スニペットは、このタスクを示しています。

:path=/static-assets/barcode/content-code-examples/how-to/StampBarcodeOnExistingPdfPage.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128, 200, 100);
myBarcode.StampToExistingPdfPage("pdf_file_path.pdf", x: 200, y: 100, 3, "password");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128, 200, 100)
myBarcode.StampToExistingPdfPage("pdf_file_path.pdf", x:= 200, y:= 100, 3, "password")
$vbLabelText   $csharpLabel

StampToExistingPdfPageはどのようなパラメータを必要としますか?

StampToExistingPdfPage() GeneratedBarcode

このコードスニペットは、StampToExistingPdfPage メソッドを StampSettings オブジェクトと共に呼び出し、そのオブジェクトを PDF 文書にスタンプします。 この方法は、シンプルさを維持しながら、柔軟性を提供します。 以下はメソッドのパラメータです:

  • pdfFilePath: PDFドキュメントのパス(相対パスまたは絶対パス)を表す System.String
  • x: 左端からの水平位置(ピクセル単位)を表す System.Int32 型。
  • y: 下端からの垂直位置(ピクセル単位)を表す System.Int32 型。
  • pageNumber: ページ番号を示す System.Int32 型(1 から始まるインデックス、最初のページは 1)。
  • password: パスワードで保護されたPDF用のSystem.String(オプション)。

いつ StampToExistingPdfPage を使用する必要がありますか?

このコードスニペットを実行すると、中間保存を行わずに GeneratedBarcode が PDF 文書に直接埋め込まれます。 この方法は、以下のようなシナリオに適しています:

StampToExistingPdfPages() GeneratedBarcode

  • 配送ラベルや配送書類のユニークな追跡コード
  • 製造報告書のバッチ番号
  • 法的または規制上の文書管理番号
  • デジタル認証やクイックアクセスリンク用のQRコード

ダイレクトスタンプ方式は、処理時間を節約し、一時ファイルを排除します。 さまざまなバーコードの種類については、supported barcode formatsガイドを参照してください。

複数の PDF ページに BarCode をスタンプするにはどうすればよいですか?

同じ BarCode を複数のページにスタンプする必要がある場合があります。 一般的な用途としては、複数ページのレポートの各ページに文書識別子を適用したり、技術文書全体にバージョン管理コードを追加したり、機密資料の各ページにセキュリティ BarCode を挿入したりすることが挙げられます。 単一ページメソッドをループさせる代わりに、StampToExistingPdfPagesメソッドを使用してください。 次のコード・スニペットは、この方法を示しています。

:path=/static-assets/barcode/content-code-examples/how-to/StampBarcodeOnMultiplePdfPages.cs
using IronBarCode;
using System.Collections.Generic;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128, 200, 100);
List<int> pages = new List<int>();
pages.Add(1);
pages.Add(2);
pages.Add(3);
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, pages, "password");
Imports IronBarCode
Imports System.Collections.Generic

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128, 200, 100)
Private pages As New List(Of Integer)()
pages.Add(1)
pages.Add(2)
pages.Add(3)
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x:= 200, y:= 100, pages, "password")
$vbLabelText   $csharpLabel

柔軟性を持たせるために、LINQを使用してページ範囲を動的に生成します:

// Stamp on all even pages from 2 to 10
var evenPages = Enumerable.Range(1, 10).Where(x => x % 2 == 0).ToList();
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, evenPages, "password");

// Stamp on the first and last 3 pages of a 20-page document
var selectedPages = new List<int> { 1, 2, 3, 18, 19, 20 };
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, selectedPages, "password");
// Stamp on all even pages from 2 to 10
var evenPages = Enumerable.Range(1, 10).Where(x => x % 2 == 0).ToList();
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, evenPages, "password");

// Stamp on the first and last 3 pages of a 20-page document
var selectedPages = new List<int> { 1, 2, 3, 18, 19, 20 };
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x: 200, y: 100, selectedPages, "password");
Imports System.Linq

' Stamp on all even pages from 2 to 10
Dim evenPages = Enumerable.Range(1, 10).Where(Function(x) x Mod 2 = 0).ToList()
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x:=200, y:=100, evenPages, "password")

' Stamp on the first and last 3 pages of a 20-page document
Dim selectedPages = New List(Of Integer) From {1, 2, 3, 18, 19, 20}
myBarcode.StampToExistingPdfPages("pdf_file_path.pdf", x:=200, y:=100, selectedPages, "password")
$vbLabelText   $csharpLabel

StampToExistingPdfPagesはどのようなパラメータを受け付けますか?

以下はメソッドのパラメータです:

  • pdfFilePath: PDFドキュメントのパスを表す System.String
  • x: 水平位置(ピクセル単位)を表す System.Int32 型。
  • y: ピクセル単位の垂直位置を表す System.Int32
  • pageNumbers: スタンプを付けるページの IEnumerable</system.int32> 配列(インデックスは 1 から)。
  • password: パスワードで保護されたPDF用のSystem.String(オプション)。

StampToExistingPdfPages() StampToExistingPdfPage() GeneratedBarcode

なぜループではなく StampToExistingPdfPages を使用するのですか?

この方法では、手作業による反復作業を行うことなく、複数のページに効率的に BarCode をスタンプすることができ、コードの可読性とパフォーマンスが向上します。 内部実装は、PDF処理を最適化し、以下のような結果をもたらします:

  • より高速な実行:PDFは複数回ではなく、一度だけ開いて処理されます。
  • メモリ使用量の削減: 大容量PDFの効率的なリソース管理
  • よりクリーンなコード: 手動のループとエラー処理の管理はありません。
  • 原子操作:すべてのページが1回の操作でスタンプされる

高度なスタンピング技術

スタンプ前のバーコードの外観をカスタマイズする

バーコードをPDFにスタンプする前に、その外観をカスタマイズしてください。 IronBarcodeは幅広いカスタマイズオプションを提供しています:

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Co/de128, 250, 80);

// Customize the appearance
myBarcode.AddAnnotationTextAboveBarcode("Invoice Number");
myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy");
myBarcode.SetMargins(10);
myBarcode.ChangeBarcodeForegroundColor(System.Drawing.Co/lor.DarkBlue);

// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Co/de128, 250, 80);

// Customize the appearance
myBarcode.AddAnnotationTextAboveBarcode("Invoice Number");
myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy");
myBarcode.SetMargins(10);
myBarcode.ChangeBarcodeForegroundColor(System.Drawing.Co/lor.DarkBlue);

// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
Imports System.Drawing

Dim myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Code128, 250, 80)

' Customize the appearance
myBarcode.AddAnnotationTextAboveBarcode("Invoice Number")
myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy")
myBarcode.SetMargins(10)
myBarcode.ChangeBarcodeForegroundColor(Color.DarkBlue)

' Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x:=450, y:=700, pageNumber:=1)
$vbLabelText   $csharpLabel

異なる BarCode タイプを扱う

シナリオによって、必要なBarCodeの種類は異なります。QRコードはURLや大きなデータセットに適しており、Code128は英数字の識別子に適しています。 QRコードの作成についての詳細や、他のフォーマットについてもご覧ください:

// QR Code for contact information
var qrCode = BarcodeWriter.CreateBarcode("BEGIN:VCARD\nFN:John Doe\nTEL:555-1234\nEND:VCARD", 
    BarcodeEncoding.QRCode, 150, 150);
qrCode.StampToExistingPdfPage("businesscard.pdf", x: 400, y: 50, pageNumber: 1);

// Data Matrix for product tracking
var dataMatrix = BarcodeWriter.CreateBarcode("PROD-2024-BATCH-789", 
    BarcodeEncoding.DataMatrix, 100, 100);
dataMatrix.StampToExistingPdfPage("product_sheet.pdf", x: 50, y: 750, pageNumber: 1);
// QR Code for contact information
var qrCode = BarcodeWriter.CreateBarcode("BEGIN:VCARD\nFN:John Doe\nTEL:555-1234\nEND:VCARD", 
    BarcodeEncoding.QRCode, 150, 150);
qrCode.StampToExistingPdfPage("businesscard.pdf", x: 400, y: 50, pageNumber: 1);

// Data Matrix for product tracking
var dataMatrix = BarcodeWriter.CreateBarcode("PROD-2024-BATCH-789", 
    BarcodeEncoding.DataMatrix, 100, 100);
dataMatrix.StampToExistingPdfPage("product_sheet.pdf", x: 50, y: 750, pageNumber: 1);
' QR Code for contact information
Dim qrCode = BarcodeWriter.CreateBarcode("BEGIN:VCARD" & vbLf & "FN:John Doe" & vbLf & "TEL:555-1234" & vbLf & "END:VCARD", 
    BarcodeEncoding.QRCode, 150, 150)
qrCode.StampToExistingPdfPage("businesscard.pdf", x:=400, y:=50, pageNumber:=1)

' Data Matrix for product tracking
Dim dataMatrix = BarcodeWriter.CreateBarcode("PROD-2024-BATCH-789", 
    BarcodeEncoding.DataMatrix, 100, 100)
dataMatrix.StampToExistingPdfPage("product_sheet.pdf", x:=50, y:=750, pageNumber:=1)
$vbLabelText   $csharpLabel

エラー処理とベストプラクティス

PDFのスタンピング操作の際に、適切なエラー処理を実装すること:

try
{
    GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345", 
        BarcodeEncoding.Co/de128, 200, 60);

    // Verify the PDF exists before attempting to stamp
    if (File.Exists("target.pdf"))
    {
        myBarcode.StampToExistingPdfPage("target.pdf", x: 100, y: 100, pageNumber: 1);
        Console.WriteLine("Barcode stamped successfully!");
    }
    else
    {
        Console.WriteLine("PDF file not found!");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error stamping barcode: {ex.Message}");
    // Log the error or handle it appropriately
}
try
{
    GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345", 
        BarcodeEncoding.Co/de128, 200, 60);

    // Verify the PDF exists before attempting to stamp
    if (File.Exists("target.pdf"))
    {
        myBarcode.StampToExistingPdfPage("target.pdf", x: 100, y: 100, pageNumber: 1);
        Console.WriteLine("Barcode stamped successfully!");
    }
    else
    {
        Console.WriteLine("PDF file not found!");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error stamping barcode: {ex.Message}");
    // Log the error or handle it appropriately
}
Imports System
Imports System.IO

Try
    Dim myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345", BarcodeEncoding.Code128, 200, 60)

    ' Verify the PDF exists before attempting to stamp
    If File.Exists("target.pdf") Then
        myBarcode.StampToExistingPdfPage("target.pdf", x:=100, y:=100, pageNumber:=1)
        Console.WriteLine("Barcode stamped successfully!")
    Else
        Console.WriteLine("PDF file not found!")
    End If
Catch ex As Exception
    Console.WriteLine($"Error stamping barcode: {ex.Message}")
    ' Log the error or handle it appropriately
End Try
$vbLabelText   $csharpLabel

パフォーマンスの考慮事項

大きなPDFや複数のスタンプ操作を行う場合は、以下のヒントを考慮してください:

  1. バッチ処理: ループ処理の代わりに StampToExistingPdfPages を使用してください StampToExistingPdfPage
  2. BarCodeのキャッシュ: GeneratedBarcode オブジェクトを一度作成して再利用 3.座標計算:一貫した位置座標を事前に計算します。 4.メモリ管理:非常に大きなPDFをバッチ処理

スタンプ後にPDFからバーコードを読み取る高度なシナリオについては、PDFドキュメントからバーコードを読み取るのガイドを参照してください。

他のIronBarcode機能との統合

PDFスタンプ機能は他のIronBarcode機能とシームレスに動作します。 非同期処理と組み合わせると、Webアプリケーションのパフォーマンスが向上します:

// Asynchronous PDF stamping
public async Task StampBarcodeAsync(string pdfPath, string barcodeData)
{
    await Task.Run(() =>
    {
        var barcode = BarcodeWriter.CreateBarcode(barcodeData, BarcodeEncoding.QRCode, 200, 200);
        barcode.StampToExistingPdfPage(pdfPath, x: 100, y: 100, pageNumber: 1);
    });
}
// Asynchronous PDF stamping
public async Task StampBarcodeAsync(string pdfPath, string barcodeData)
{
    await Task.Run(() =>
    {
        var barcode = BarcodeWriter.CreateBarcode(barcodeData, BarcodeEncoding.QRCode, 200, 200);
        barcode.StampToExistingPdfPage(pdfPath, x: 100, y: 100, pageNumber: 1);
    });
}
Imports System.Threading.Tasks

' Asynchronous PDF stamping
Public Async Function StampBarcodeAsync(pdfPath As String, barcodeData As String) As Task
    Await Task.Run(Sub()
                       Dim barcode = BarcodeWriter.CreateBarcode(barcodeData, BarcodeEncoding.QRCode, 200, 200)
                       barcode.StampToExistingPdfPage(pdfPath, x:=100, y:=100, pageNumber:=1)
                   End Sub)
End Function
$vbLabelText   $csharpLabel

さらに、バーコードのスタンプの前または後に補正が必要なスキャンされたPDFを扱う場合、IronBarcodeの画像補正機能を活用してください。

一般的な問題のトラブルシューティング

PDFにBarCodeをスタンプする際に問題が発生した場合の解決策をご紹介します:

1.座標の問題:PDF の座標は左上ではなく、左下から始まります。 2.パスワードで保護されたPDF: 暗号化されたPDFの正しいパスワードパラメータを確認する。 3.ファイルサイズが大きい:最適化と処理のヒントについては、トラブルシューティング・ガイドを参照してください。 4.フォントまたはエンコーディングの問題:特殊文字やUnicodeについては、Unicodeバーコードを書くガイドを確認してください。

これらのガイドラインに従い、IronBarcodeのPDFスタンプ機能を活用することで、高いパフォーマンスとコード品質を維持しながら、既存のPDFドキュメントに効率的にバーコードを追加することができます。

よくある質問

C# で既存の PDF 文書に BarCode を追加するには?

IronBarcodeのCreateBarcodeメソッドを使用してバーコードを生成し、StampToExistingPdfPageメソッドを適用してPDFに配置します。PDFファイルのパス、位置座標(x, y)、バーコードを表示したいページ番号を指定するだけです。

StampToExistingPdfPageメソッドにはどのようなパラメータが必要ですか?

IronBarcodeのStampToExistingPdfPageメソッドは、pdfFilePath (PDFの場所の文字列)、xとyの座標(ピクセル単位の位置決めのための整数)、pageNumber (整数、1-インデックス)、そして保護されたPDFのためのオプションのパスワードパラメータを必要とします。

PDFの複数ページに同じBarCodeをスタンプできますか?

はい、IronBarcodeはStampToExistingPdfPagesメソッド(複数形の'Pages'に注意)を提供し、PDFドキュメントの複数のページに1つの生成されたバーコードをスタンプすることができます。

PDF上のBarCodeの位置決めには、どのような座標系が使用されますか?

IronBarcodeはピクセルベースの座標系を使用しており、StampToExistingPdfPageメソッドを使用する場合、x座標はページの左端から、y座標はページの下端から測定します。

既存のPDFにBarCodeをスタンプする一般的なユースケースは何ですか?

IronBarcodeのPDFスタンプ機能は、出荷ラベルへのユニークな追跡コードの追加、製造報告書のバッチ番号、法的フォームの文書管理番号、デジタル認証やクイックアクセスリンクのためのQRコードなどによく使用されます。

PDFにBarCodeをスタンプするには、中間ファイルを保存する必要がありますか?

いいえ、IronBarcodeのStampToExistingPdfPageメソッドは、一時ファイルを作成することなくPDFドキュメントに直接バーコードをスタンプするため、処理時間とストレージスペースを節約できます。

パスワードで保護されたPDF文書にBarCodeをスタンプできますか?

はい、IronBarcodeはパスワードで保護されたPDFへのバーコードのスタンプをサポートしています。StampToExistingPdfPageメソッドのオプションパラメータとしてPDFのパスワードを指定するだけです。

IronBarcodeはビジネスプロセスの効率向上にどのように役立ちますか?

IronBarcodeは迅速かつ正確なバーコード生成と読み取りを可能にし、手動データ入力エラーの減少、在庫および資産追跡の改善などにより、ビジネスプロセスの効率を向上させます。

プロジェクトにIronBarcodeを実装するために必要なプログラミングスキルは何ですか?

IronBarcodeをプロジェクトに実装するためには、C#プログラミングの基本的な知識があれば十分で、開発者をガイドするための簡単なメソッドと包括的なドキュメントが提供されています。

IronBarcodeは小規模プロジェクトと大規模エンタープライズアプリケーションの両方に適していますか?

IronBarcodeはスケーラブルかつ多用途に設計されており、小規模プロジェクトおよび強力なバーコードソリューションを必要とする大規模エンタープライズアプリケーションの両方に適しています。

Hairil Hasyimi Bin Omar
ソフトウェアエンジニア
すべての優れたエンジニアのように、ハイリルは熱心な学習者です。彼はC#、Python、Javaの知識を磨き、その知識を活用してIron Softwareのチームメンバーに価値を追加しています。ハイリルはマレーシアのマラ工科大学からIron Softwareのチームに参加し、化学およびプロセス工学の学士号を取得しました。
準備はできましたか?
Nuget ダウンロード 2,240,258 | バージョン: 2026.5 just released
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package BarCode
サンプルを実行する 文字列が BarCode になるのを見る。