C#を使用して PDF に BarCode をスタンプする方法
単一ページには StampToExistingPdfPage、複数ページには StampToExistingPdfPages を使用して、C#で既存のPDFドキュメントにバーコードをスタンプします。座標とページ番号を指定します。
この例では、IronBarcodeの CreateBarcode を使用してバーコードを生成し、既存のPDFページにスタンプします。 PDFのパス、位置座標、ページ番号を提供してください。
-
1Install IronBarcode with NuGet Package Manager
-
2このコード スニペットをコピーして実行します。
IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1);C# -
3実際の環境でテストするためにデプロイする
今日プロジェクトで IronBarcode を使い始めましょう無料トライアル
StampToExistingPdfPage
StampToExistingPdfPages
最小限のワークフロー(5ステップ)
- PDF にバーコードをスタンプするための C# ライブラリをダウンロードする
- 指定されたバーコードタイプと値でバーコードを作成します
- バーコードのサイズを指定します
StampToExistingPdfPageメソッドを使用して、単一のPDFページにバーコードをスタンプします。StampToExistingPdfPagesメソッドを使用して、複数のPDFページにバーコードをスタンプします。
既存の PDF ページにバーコードをスタンプするにはどうすればよいですか?
バーコードをPDFとしてエクスポートする以外にも、IronBarcodeは GeneratedBarcode を直接既存のPDFドキュメントにスタンプすることを可能にします。 この機能は、既存のレポート、請求書、フォームにトラッキングコード、在庫ラベル、またはドキュメント識別子を追加する場合に便利です。 次のコード・スニペットは、このタスクを示しています。
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 メソッドを使用してください。 次のコード・スニペットは、この方法を示しています。
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");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は幅広いカスタマイズオプションを提供しています:
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);
異なる 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
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のスタンピング操作の際に、適切なエラー処理を実装すること:
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);
});
}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のパスワードを指定するだけです。
Why should you use the StampToExistingPdfPages method instead of looping for multiple pages?
The `StampToExistingPdfPages` method is preferred for stamping on multiple pages because it is more efficient, reduces processing time, and optimizes resource usage compared to manually looping through pages.
Are there performance considerations when stamping barcodes on large PDFs with IronBarcode?
Yes, when stamping barcodes on large PDFs, it's recommended to use batch operations, cache barcodes, and manage memory efficiently for better performance with IronBarcode.
What should be done if a barcode stamping operation fails in IronBarcode?
If a barcode stamping operation fails, implement error handling using `try-catch` blocks to log errors and ensure that the PDF file exists before stamping with IronBarcode.
