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

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

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

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

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

この例では、IronBarCode の CreateBarcode を使用してバーコードを生成し、それを既存の 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

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

Apart from exporting barcodes as PDF, IronBarcode enables stamping the GeneratedBarcode directly onto existing PDF documents. この機能は、既存のレポート、請求書、フォームにトラッキングコード、在庫ラベル、またはドキュメント識別子を追加する場合に便利です。 次のコード・スニペットは、このタスクを示しています。

: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");
$vbLabelText   $csharpLabel

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

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

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

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

コード スニペットを実行すると、中間保存なしで、GeneratedBarcode が PDF ドキュメントに直接スタンプされます。 この方法は、以下のようなシナリオに適しています:

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

ダイレクトスタンプ方式は、処理時間を節約し、一時ファイルを排除します。 For information on different barcode types, see the supported barcode formats guide.

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

同じ BarCode を複数のページにスタンプする必要がある場合があります。 一般的な用途としては、複数ページのレポートの各ページに文書識別子を適用したり、技術文書全体にバージョン管理コードを追加したり、機密資料の各ページにセキュリティ BarCode を挿入したりすることが挙げられます。 シングルページ メソッドをループする代わりに、GeneratedBarcode クラスの 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");
$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");
$vbLabelText   $csharpLabel

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

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

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

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

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

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

高度なスタンピング技術

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

バーコードをPDFにスタンプする前に、その外観をカスタマイズしてください。 IronBarcode offers extensive customization options:

GeneratedBarcode myBarcode = 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(System.Drawing.Color.DarkBlue);

// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
GeneratedBarcode myBarcode = 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(System.Drawing.Color.DarkBlue);

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

異なる BarCode タイプを扱う

シナリオによって、必要なBarCodeの種類は異なります。QRコードはURLや大きなデータセットに適しており、Code128は英数字の識別子に適しています。 Learn more about creating QR codes or explore other formats:

// 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);
$vbLabelText   $csharpLabel

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

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

try
{
    GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345", 
        BarcodeEncoding.Code128, 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.Code128, 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
}
$vbLabelText   $csharpLabel

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

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

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

For advanced scenarios involving reading barcodes from PDFs after stamping, see our guide on reading barcodes from PDF documents.

他のIronBarcode機能との統合

PDFスタンプ機能は他のIronBarcode機能とシームレスに動作します。 Combine it with asynchronous processing for better web application performance:

// 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);
    });
}
$vbLabelText   $csharpLabel

Additionally, leverage IronBarcode's image correction features when working with scanned PDFs that might need enhancement before or after barcode stamping.

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

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

1.座標の問題:PDF の座標は左上ではなく、左下から始まります。 2.パスワードで保護されたPDF: 暗号化されたPDFの正しいパスワードパラメータを確認する。

  1. Large File Sizes: For optimization and handling tips, see our troubleshooting guide
  2. Font or Encoding Issues: For special characters or Unicode, check our writing Unicode barcodes guide

これらのガイドラインに従い、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のパスワードを指定するだけです。

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

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

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