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");
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");
ImportsIronBarCodeImportsSystem.Collections.GenericPrivate myBarcode AsGeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128, 200, 100)Private pages As New List(OfInteger)()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 10var 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 documentvar 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");
ImportsSystem.Linq' Stamp on all even pages from 2 to 10Dim evenPages = Enumerable.Range(1, 10).Where(Function(x) x Mod2 = 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 documentDim selectedPages = New List(OfInteger) From {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")
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Code128, 250, 80);// Customize the appearancemyBarcode.AddAnnotationTextAboveBarcode("Invoice Number");myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy");myBarcode.SetMargins(10);myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkBlue);// Now stamp the customized barcodemyBarcode.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.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkBlue);
// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, 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}
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
}
ImportsSystemImportsSystem.IOTry Dim myBarcode AsGeneratedBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345", BarcodeEncoding.Code128, 200, 60) ' Verify the PDF exists before attempting to stamp IfFile.Exists("target.pdf") Then myBarcode.StampToExistingPdfPage("target.pdf", x:=100, y:=100, pageNumber:=1)Console.WriteLine("Barcode stamped successfully!") ElseConsole.WriteLine("PDF file not found!") End IfCatch ex AsExceptionConsole.WriteLine($"Error stamping barcode: {ex.Message}") ' Log the error or handle it appropriatelyEndTry
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
ImportsSystem.Threading.Tasks' Asynchronous PDF stampingPublicAsync Function StampBarcodeAsync(pdfPath AsString, barcodeData AsString) AsTaskAwaitTask.Run(Sub() Dim barcode = BarcodeWriter.CreateBarcode(barcodeData, BarcodeEncoding.QRCode, 200, 200) barcode.StampToExistingPdfPage(pdfPath, x:=100, y:=100, pageNumber:=1) End Sub)End Function
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
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.