如何使用C#將條碼標記在PDF上
在C#中使用IronBarcode的CreateBarcode方法將條碼蓋在現有PDF文件上,使用StampToExistingPdfPage方法處理單頁或StampToExistingPdfPages方法處理多頁,指定座標及頁碼。
此範例展示如何使用IronBarcode的CreateBarcode生成條碼,並將其蓋印到現有的PDF頁面上。 提供 PDF 路徑、位置座標和頁碼。
-
1使用 IronBarcode 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1);IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) _ .StampToExistingPdfPage("report.pdf", x:=50, y:=50, pageNumber:=1)IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) _ .StampToExistingPdfPage("report.pdf", x:=50, y:=50, pageNumber:=1) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronBarcode,並使用 免費試用
StampToExistingPdfPage
StampToExistingPdfPages
最小工作流程(5 個步驟)
- 下載C#程式庫以在PDF上蓋印條碼
- 創建具有特定條碼類型和值的條碼
- 指定條碼尺寸
- 使用
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");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")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方法將物件蓋印到PDF文件上。 此方法在保持簡潔的同時,也提供了靈活性。 以下是方法參數:
pdfFilePath:表示PDF文件路徑(相對或絕對)的System.String。x:從左邊緣以像素為單位的水平位置的System.Double。y:從底部邊緣以像素為單位的垂直位置的System.Double。pageNumber:指示頁數的System.Int32(從1開始,第一頁是1)。password:用於密碼保護PDF的System.String(可選)。
什麼情況下應使用StampToExistingPdfPage?
運行此程式碼片段直接將GeneratedBarcode蓋印到PDF文件中,無需中途保存。 此方法適用於以下需求的場景:
StampToExistingPdfPages()
GeneratedBarcode
- 航運標籤或交貨文件上的獨特追蹤碼
- 生產報告上的批號
- 在法律或監管表格上的文件控制編號
- 用於數位驗證或快速訪問連結的QR碼
直接蓋章的方式可節省處理時間,並消除臨時檔案。 有關不同條碼類型的資訊,請參閱支持的條碼格式指南。
如何在多個PDF頁面上蓋印條碼?
有時候,同一個 BarCode 需要在多個頁面上蓋章。 常見的用途包括在多頁報告的每頁上套用文件識別碼、在整個技術文件中加入版本控制代碼,或在機密資料的每頁上插入安全條碼。 不必循環單頁方法,而是使用StampToExistingPdfPages方法處理GeneratedBarcode對象。 以下程式碼片段展示了此方法。
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");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")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")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.Double。y:以像素為單位的垂直位置的System.Double。pageNumbers:要蓋印的頁面IEnumerable<System.Int32>(以1作索引)。password:用於密碼保護PDF的System.String(可選)。
StampToExistingPdfPages()
StampToExistingPdfPage()
GeneratedBarcode
為什麼使用StampToExistingPdfPages而不是循環?
此方法可在多個頁面上提供有效率的 BarCode 燙印,無須手動反覆燙印,可提高程式碼的可讀性和效能。 內部實作會優化 PDF 處理,結果是
- 更快的執行: PDF只開啟並處理一次,不是多次
- 更低的記憶體使用: 大型PDF的高效資源管理
- 更乾淨的程式碼: 無需手動循環和錯誤處理管理
- 原子操作: 在單次操作中蓋印所有頁面
高級蓋印技術
在蓋印之前自訂條碼外觀
在 PDF 上印上您的 BarCode 之前,請自訂其外觀。 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);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)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 類型。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)' 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
}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 TryImports 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 檔案或多重燙印作業時,請考慮以下提示:
- 批次操作: 使用
StampToExistingPdfPages代替循環StampToExistingPdfPage - 條碼快取: 一次創建並重用
GeneratedBarcode物件 - 座標計算: 預計算一致的位置座標
- 記憶體管理: 批量處理非常大的PDF
對於蓋印後涉及從PDF閱讀條碼的高級場景,請參閱我們的從PDF文件中閱讀條碼指南。
與其他IronBarcode功能的整合
PDF蓋印功能可以與IronBarcode的其他功能一起使用。 將其與非同步處理結合使用,以提高網頁應用的性能:
// 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 FunctionImports 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 時遇到問題,以下是解決方案:
- 座標問題: PDF的座標是從左下角開始,而不是左上角
- 受密碼保護的PDF: 確保加密PDF的密碼參數正確
- 大文件大小: 有關優化和處理提示,請參考我們的疑難排解指南
- 字型或編碼問題: 對於特殊字元或Unicode,請查看我們的寫入Unicode條碼指南
遵循這些準則,並利用 IronBarcode 的 PDF 燙印功能,可以在保持高性能和代碼質量的同時,有效地將條碼添加到現有的 PDF 文件中。
常見問題
使用IronBarcode在PDF上嵌入條碼的主要方法是什麼?
使用IronBarcode在PDF上嵌入條碼的主要方法是StampToExistingPdfPage方法。
IronBarcode可以一次在多個PDF頁面上嵌入條碼嗎?
是的,IronBarcode支持使用StampToExistingPdfPages方法在多個PDF頁面上嵌入條碼。
在PDF上嵌入條碼的一些常見用途是什麼?
在PDF上嵌入條碼的常見用途包括為報告、發票或表單添加追蹤碼、庫存標籤或文件標識符。
如何在IronBarcode中指定PDF頁面上條碼的位置?
在IronBarcode中,您可以使用表示像素座標的x和y參數來指定PDF頁面上條碼的位置。
在嵌入條碼時,IronBarcode支持密碼保護的PDF嗎?
是的,IronBarcode支持密碼保護的PDF。您可以在使用StampToExistingPdfPage或StampToExistingPdfPages方法時提供密碼作為可選參數。
在PDF上嵌入條碼時,IronBarcode可以生成哪些類型的條碼?
IronBarcode可以生成各種條碼類型,包括QR碼和Code128,並可以嵌入到PDF上。
在IronBarcode中,如何在將條碼嵌入到PDF前自定義條碼的外觀?
您可以在IronBarcode中使用如AddAnnotationTextAboveBarcode、AddAnnotationTextBelowBarcode和ChangeBarCodeColor等方法來自定義條碼的外觀,然後再嵌入。
為何您應該使用StampToExistingPdfPages方法而不是對多個頁面進行迴圈?
StampToExistingPdfPages方法更適合於多頁面嵌入,因為它更有效率,減少處理時間,並優化資源使用,相比於手動對頁面進行迴圈。
使用IronBarcode在大型PDF上加蓋條碼時,是否有性能考量?
是的,當在大容量PDF上嵌入條碼時,建議使用批次操作、緩存條碼並有效管理記憶體,以改善IronBarcode的性能。
如果IronBarcode中的條碼嵌入操作失敗應該怎麼辦?
如果條碼嵌入操作失敗,請使用try-catch塊來實施錯誤處理,記錄錯誤並確保PDF文件存在然後再與IronBarcode一起嵌入。