如何使用 C# 在 PDF 上標示 BarCode
using IronBarcode 的 CreateBarcode 方法,透過 StampToExistingPdfPage(單頁)或 StampToExistingPdfPages(多頁)指定座標與頁碼,在 C# 中將 BARCODE 加印至現有 PDF 文件上。
快速入門:將生成的條碼蓋章到 PDF 頁面
此範例展示如何使用 IronBarcode 的 CreateBarcode 生成 BARCODE,並將其烙印至現有的 PDF 頁面。 提供 PDF 路徑、位置座標和頁碼。
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/BarCode
PM > Install-Package BarCode -
請複製並執行此程式碼片段。
IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronBarcode
StampToExistingPdfPage
StampToExistingPdfPages
最小工作流程(5 個步驟)
- 下載 C# 庫,用於在 PDF 上新增條碼
- 創建具有特定條碼類型和值的條碼
- 指定條碼尺寸
- 使用
StampToExistingPdfPage方法將條碼新增至單一 PDF 頁面上 - 使用
StampToExistingPdfPages方法將條碼新增至多個 PDF 頁面上
如何在現有的 PDF 頁面上標示條碼?
除了將 BARCODE 匯出為 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 代碼
直接蓋章的方式可節省處理時間,並消除臨時檔案。 有關不同條碼類型的資訊,請參閱 支援的條碼格式指南。
如何在多個 PDF 頁面上標示 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 而非 Looping?
此方法可在多個頁面上提供有效率的 BarCode 燙印,無須手動反覆燙印,可提高程式碼的可讀性和效能。 內部實作會優化 PDF 處理,結果是
- 執行速度更快:PDF 開啟和處理一次,而非多次
- 降低記憶體使用量:針對大型 PDF 的高效資源管理
- 更清潔的程式碼:無需手動迴圈和錯誤處理管理
- 原子操作:所有頁面以單一操作蓋章
進階沖壓技術
沖印前自訂 BarCode 外觀
在 PDF 上印上您的 BarCode 之前,請自訂其外觀。 IronBarcode 提供廣泛的自訂選項:
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Co/de128, 250, 80);
// Customize the appearance
myBarcode.AddAnnotationTextAboveBarcode("Invoice Number");
myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy");
myBarcode.SetMargins(10);
myBarcode.ChangeBarcodeForegroundColor(System.Drawing.Co/lor.DarkBlue);
// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Co/de128, 250, 80);
// Customize the appearance
myBarcode.AddAnnotationTextAboveBarcode("Invoice Number");
myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy");
myBarcode.SetMargins(10);
myBarcode.ChangeBarcodeForegroundColor(System.Drawing.Co/lor.DarkBlue);
// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
Imports System.Drawing
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.ChangeBarcodeForegroundColor(Color.DarkBlue)
' Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x:=450, y:=700, pageNumber:=1)
使用不同的 BarCode 類型
不同的場景需要不同的 BarCode 類型。QR 條碼適合 URL 和大型資料集,而 Code128 則適用於字母數字識別碼。 進一步了解 建立 QR code 或探索其他格式:
// 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)
錯誤處理與最佳實務
在處理 PDF 蓋章作業時,實施正確的錯誤處理:
try
{
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345",
BarcodeEncoding.Co/de128, 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.Co/de128, 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 檔案或多重燙印作業時,請考慮以下提示:
- 批次操作:使用
StampToExistingPdfPages取代循環StampToExistingPdfPage - BarCode快取:建立一次並重複使用
GeneratedBarcode物件 3.座標計算:預先計算一致的位置座標 4.記憶體管理:分批處理非常大的 PDF
如需瞭解涉及在蓋章後從 PDF 讀取條碼的進階方案,請參閱我們的指南 從 PDF 文件讀取條碼。
與其他 IronBarcode 功能整合
/csharp/barcode/ 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.Password-Protected PDFs:確保加密 PDF 的密碼參數正確無誤 3.檔案大小大:有關最佳化與處理技巧,請參閱我們的 疑難排解指南。 4.字體或編碼問題:對於特殊字符或 Unicode,請查看我們的 writing Unicode 條碼指南
遵循這些準則,並利用 IronBarcode 的 PDF 燙印功能,可以在保持高性能和代碼質量的同時,有效地將條碼添加到現有的 PDF 文件中。
常見問題
如何在C#中將條碼添加到現有PDF文件中?
使用IronBarcode的CreateBarcode方法生成條碼,然後使用StampToExistingPdfPage方法將其放置在您的PDF上。只需指定PDF文件路徑、位置座標(x, y)及您想加蓋條碼的頁碼。
StampToExistingPdfPage方法需要哪些參數?
IronBarcode的StampToExistingPdfPage方法需要:pdfFilePath(PDF位置的字串)、x和y座標(定位的整數形式,以像素為單位)、pageNumber(整數,從1開始)、以及用於受保護PDF的可選密碼參數。
我可以在PDF上的多個頁面上加蓋相同的條碼嗎?
可以,IronBarcode提供StampToExistingPdfPages方法(注意複數“Pages”),允許您在PDF文件的多個頁面上加蓋單一生成的條碼。
在PDF上定位條碼使用什麼座標系統?
IronBarcode使用基於像素的座標系統,當使用StampToExistingPdfPage方法時,x座標從頁面的左邊緣測量,y座標從頁面的底部測量。
在現有PDF上加蓋條碼的常見使用情境是什麼?
IronBarcode的PDF加蓋功能常用於為運送標籤添加唯一追蹤碼、為製造報告上的批次號、新增法律文件上的文件控制號、以及數位驗證或快速訪問連結的QR Code。
在PDF上加蓋條碼是否需要儲存中間文件?
不需要,IronBarcode的StampToExistingPdfPage方法將條碼直接加蓋在PDF文件上,不會創建臨時文件,從而節省處理時間和存儲空間。
我能在受密碼保護的PDF文件上加蓋條碼嗎?
可以,IronBarcode支持在受密碼保護的PDF文件上加蓋條碼。只需在StampToExistingPdfPage方法中作為可選參數提供PDF密碼即可。
IronBarcode如何幫助改善業務流程效率?
IronBarcode通過使條碼生成和讀取快速且準確來提高業務流程效率,減少手動數據輸入錯誤,並改善庫存和資產追蹤。
將IronBarcode實現於專案中需要什麼程式設計技能?
基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文檔來指導開發者。
IronBarcode適合於小型專案和大型企業應用嗎?
IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

