C#を使用して PDF に BarCode をスタンプする方法
単一ページには StampToExistingPdfPage、複数ページには StampToExistingPdfPages を使用して、C#で既存のPDFドキュメントにバーコードをスタンプします。座標とページ番号を指定します。
クイックスタート: 生成されたバーコードを PDF ページにスタンプ
この例では、IronBarcodeの CreateBarcode を使用してバーコードを生成し、既存のPDFページにスタンプします。 PDFのパス、位置座標、ページ番号を提供してください。
StampToExistingPdfPage
StampToExistingPdfPages
最小限のワークフロー(5ステップ)
- PDF にバーコードをスタンプするための C# ライブラリをダウンロードする
- 指定されたバーコードタイプと値でバーコードを作成します
- バーコードのサイズを指定します
StampToExistingPdfPageメソッドを使用して、単一のPDFページにバーコードをスタンプします。StampToExistingPdfPagesメソッドを使用して、複数のPDFページにバーコードをスタンプします。
既存の PDF ページにバーコードをスタンプするにはどうすればよいですか?
バーコードをPDFとしてエクスポートする以外にも、IronBarcodeは GeneratedBarcode を直接既存のPDFドキュメントにスタンプすることを可能にします。 この機能は、既存のレポート、請求書、フォームにトラッキングコード、在庫ラベル、またはドキュメント識別子を追加する場合に便利です。 次のコード・スニペットは、このタスクを示しています。
: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")
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 を挿入したりすることが挙げられます。 単一ページメソッドをループする代わりに、BarcodeStamper クラスから 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")
柔軟性を持たせるために、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")
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は幅広いカスタマイズオプションを提供しています:
:path=/static-assets/barcode/content-code-examples/how-to/create-and-stamp-barcode-pdf-5.cs
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.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkBlue);
// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
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.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkBlue)
' Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x:=450, y:=700, pageNumber:=1)
異なる BarCode タイプを扱う
シナリオによって、必要なBarCodeの種類は異なります。QRコードはURLや大きなデータセットに適しており、Code128は英数字の識別子に適しています。 QRコードの作成についての詳細や、他のフォーマットについてもご覧ください:
:path=/static-assets/barcode/content-code-examples/how-to/create-and-stamp-barcode-pdf-6.cs
// 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)
エラー処理とベストプラクティス
PDFのスタンピング操作の際に、適切なエラー処理を実装すること:
:path=/static-assets/barcode/content-code-examples/how-to/create-and-stamp-barcode-pdf-7.cs
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
}
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
パフォーマンスの考慮事項
大きなPDFや複数のスタンプ操作を行う場合は、以下のヒントを考慮してください:
- バッチ処理:
StampToExistingPdfPageをループする代わりにStampToExistingPdfPagesを使用してください - バーコードキャッシング: 一度作成して
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
さらに、バーコードのスタンプの前または後に補正が必要なスキャンされた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はスケーラブルかつ多用途に設計されており、小規模プロジェクトおよび強力なバーコードソリューションを必要とする大規模エンタープライズアプリケーションの両方に適しています。

