IRONSOFTWAREHOME

如何在PDF上加蓋條碼使用C#

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Updated: 2026年6月4日

使用IronBarcode的StampToExistingPdfPages對多頁的情況下,在現有的PDF文件中加上條碼,指定坐標和頁碼。

快速入門:將GeneratedBarcode加蓋到PDF頁面

此範例演示如何使用IronBarcode的CreateBarcode生成條碼並將其蓋在現有PDF頁面上。 提供PDF路徑、位置坐標和頁碼。

  1. 1Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. 2複製並運行這段程式碼片段。

    IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150)
        .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1);
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronBarcode,透過免費試用
    arrow pointer

StampToExistingPdfPage StampToExistingPdfPages

如何在現有的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");

StampToExistingPdfPage需要哪些參數?

StampToExistingPdfPage() GeneratedBarcode

程式碼片段呼叫StampSettings物件將其蓋在PDF文件上。 此方法在保持簡單性的同時提供靈活性。 以下是方法參數:

  • pdfFilePath:一個System.String,表示PDF文件路徑(相對或絕對)。
  • x:一個System.Int32,表示從左邊緣開始的水平位置像素。
  • y:一個System.Int32,表示從底邊緣開始的垂直位置像素。
  • pageNumber:一個System.Int32,用來指示頁碼(1起算,第一頁為1)。
  • password:用於密碼保護PDF的System.String(可選)。

何時應使用StampToExistingPdfPage?

執行程式碼片段會直接將GeneratedBarcode蓋入PDF文件中,而無需中間儲存。 此方法適用於以下情境:

StampToExistingPdfPages() GeneratedBarcode

  • 運送標籤或交付文件上的唯一追蹤編碼
  • 製造報告上的批號
  • 法律或監管表單上的文件控制號碼
  • 用於數位驗證或快速存取連結的QR碼

直接加蓋的方法節省了處理時間,並消除臨時文件。 有關不同條碼型別的資訊,請參閱支持的條碼格式指南。

如何在多個PDF頁面上加蓋條碼?

有時需要在多個頁面上蓋上相同的條碼。 常見用法包括將文件標識符應用於多頁報告的每個頁面,在技術文件中新增版本控制編碼,或在機密材料的每個頁面上插入安全條碼。 與其迴圈單頁方法,使用BarcodeStamper類。 以下程式碼片段演示此方法。

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");

為了靈活性,使用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");

StampToExistingPdfPages接受哪些參數?

以下是方法參數:

  • pdfFilePath:一個System.String,表示PDF文件路徑。
  • x:一個System.Int32,表示水平方向位置像素。
  • y:一個System.Int32,表示垂直方向位置像素。
  • pageNumbers:一個**IEnumerable<System.Int32>**,表示要加蓋的頁面(1起始)。
  • password:用於密碼保護PDF的System.String(可選)。

StampToExistingPdfPages() StampToExistingPdfPage() GeneratedBarcode

為什麼使用StampToExistingPdfPages而非迴圈?

此方法提供了更高效的多頁條碼加蓋,無需手動迴圈,提高程式碼的可讀性和性能。 內部實現優化了PDF處理,導致以下結果:

  • 更快的執行:PDF打開並處理一次,而不是多次
  • 更低的記憶體使用:對於大型PDF進行高效的資源管理
  • 更清晰的程式碼:無需手動迴圈和錯誤處理管理
  • 原子操作:在單次操作中加蓋所有頁面

進階加蓋技術

在加蓋前自訂條碼的外觀

在將您的條碼加蓋到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);
C#

使用不同的條碼型別

不同的情境需要不同的條碼型別。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);

錯誤處理和最佳實踐

在處理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
}

性能考量

在處理大型PDF或多次加蓋操作時,請考慮以下提示:

  1. 批量操作:使用StampToExistingPdfPage
  2. 條碼快取:建立一次並重用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);
    });
}

此外,在處理可能需要在條碼加蓋前或之後增強的掃描PDF時,利用IronBarcode的圖像校正功能

常見問題故障排除

如果在PDF上加蓋條碼時遇到問題,以下是解決方案:

  1. 坐標問題:PDF坐標從左下角開始,而不是左上角
  2. 密碼保護PDF:確保加密的PDF使用正確的密碼參數
  3. 大文件大小:如需優化和處理提示,請參考我們的故障排除指南
  4. 字體或編碼問題:如涉及特殊字元或Unicode,請查看我們的書寫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使用像素為基礎的座標系統,其中x座標從頁面左邊緣開始測量,而y座標在使用StampToExistingPdfPage方法時從頁面底部開始測量。

在現有PDF上印章條碼的常見應用場景是什麼?

IronBarcode的PDF印章功能通常用於在運送標籤上新增唯一的追蹤碼,製造報告上的批次號碼,法律文件上的文件控制號碼,以及數位認證或快速存取連結的QR碼。

在PDF上印章條碼是否需要保存中間文件?

不需要,IronBarcode的StampToExistingPdfPage方法直接在PDF文件上印章條碼而不建立臨時文件,這樣可以節省處理時間和儲存空間。

我可以在有密碼保護的PDF文件上印章條碼嗎?

可以,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.

準備好開始了嗎?

Nuget Downloads 2,422,100版本:2026.9剛剛發布

立即獲取免費

立即獲取 30天試用金鑰

bullet_checked無需信用卡或註冊帳號
bullet_test在生產
環境中進行測試,且不顯示浮水印
bullet_calendar30 天全
功能產品
bullet_support試用期間提供 24/5 技術
支援
立即獲取您的免費30天試用金鑰
不需要信用卡或帳戶建立
C# NuGet程式庫,用於PDF
通過NuGet安裝

版本: 2026.9

PM > Install-Package BarCode
nuget.org/packages/BarCode/
  1. 在解決方案資源管理器中,右鍵點擊References,管理NuGet包
  2. 選擇瀏覽並搜尋"IronBarCode"
  3. 選擇包並安裝
C# PDF DLL
下載DLL

版本: 2026.9

  1. 下載並解壓IronBarCode至您的Solution目錄中的~/Libs等位置
  2. 在Visual Studio解決方案資源管理器中右鍵點擊References,選擇瀏覽,"IronBarCode.dll"

$999起的授權費用

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費即時演示
Booking Badge

全球數百萬工程師信賴

Iron Software的客戶標誌
獲取您的無義務諮詢
完成下方表單或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
全球數百萬工程師信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立