如何使用 IronBarcode 在 PDF 文檔中創建並印章條碼

如何使用 C# 在 PDF 上標示 BarCode

This article was translated from English: Does it need improvement?
Translated
View the article in English

在 C# 中使用 IronBarcode 的 CreateBarcode 方法,使用 StampToExistingPdfPage 為單頁或 StampToExistingPdfPages 為多頁,指定坐標和頁碼,將條碼印在現有的 PDF 文件上。

快速入門:將產生的條碼新增至 PDF 頁面

本示例演示了使用 IronBarcode 的 CreateBarcode 生成条形码,并将其戳印到现有的 PDF 页面上。 提供 PDF 路徑、位置座標和頁碼。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronBarcode

    PM > Install-Package BarCode

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

    IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150)
        .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1);
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronBarcode,免費試用!
    arrow pointer

如何在現有的 PDF 頁面上標示條碼? --> <!--說明:說明程式碼概念的圖表或截圖 --> Apart from exporting barcodes as PDF, IronBarcode enables stamping the `GeneratedBarcode` directly onto existing PDF documents. 在現有的報表、發票或表格中加入追蹤代碼、庫存標籤或文件識別符時,此功能非常有用。 以下程式碼片段展示了這項任務。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/StampBarcodeOnExistingPdfPage.cs ```

StampToExistingPdfPage 需要哪些參數? 該程式碼片段使用 `GeneratedBarcode` 物件呼叫 `StampToExistingPdfPage()` 方法,將該物件戳記在 PDF 文件上。 此方法在保持簡潔的同時,也提供了靈活性。 以下是方法參數: * `pdfFilePath`:代表 PDF 文件路徑(相對或絕對)的 **System.String**。 * `x`:一個 **System.Int32**,代表水平位置,單位為距離左邊的像素。 * `y`:一個 **System.Int32**,代表垂直位置,單位為距離底部邊緣的像素。 * `pageNumber`:表示頁面的 **System.Int32**(1-索引,第一頁為 1)。 * `密碼`:用於密碼保護 PDF 的 **System.String**(可選)。

何時應該使用 StampToExistingPdfPage? 執行程式碼片段會將 `GeneratedBarcode` 直接戳記在 PDF 文件中,無須中間儲存。 此方法適用於以下需求的場景: - 運送標籤或送貨文件上的唯一追蹤代碼 - 製造報告上的批號 - 法律或法規表格上的文件控制編號 - 用於數位認證或快速存取連結的 QR 代碼 直接蓋章的方式可節省處理時間,並消除臨時檔案。 For information on different barcode types, see the supported barcode formats guide.

如何在多個 PDF 頁面上標示 BarCode? --> <!--說明:說明程式碼概念的圖表或截圖 --> 有時候,同一個 BarCode 需要在多個頁面上蓋章。 常見的用途包括在多頁報告的每頁上套用文件識別碼、在整個技術文件中加入版本控制代碼,或在機密資料的每頁上插入安全條碼。 請使用 `GeneratedBarcode` 類中的 `StampToExistingPdfPages()` 方法,而非循環單頁方法。 以下程式碼片段展示了此方法。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/StampBarcodeOnMultiplePdfPages.cs ``` 為了增加彈性,請使用 LINQ 動態產生頁面範圍: ```csharp // 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 { 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**的頁面戳記(1-索引)。 * `密碼`:用於密碼保護 PDF 的 **System.String**(可選)。

為何要使用 StampToExistingPdfPages 而非 Looping? 此方法可在多個頁面上提供有效率的 BarCode 燙印,無須手動反覆燙印,可提高程式碼的可讀性和效能。 內部實作會優化 PDF 處理,結果是 - **執行速度更快**:PDF 開啟和處理一次,而非多次 - **降低記憶體使用量**:針對大型 PDF 的高效資源管理 - **更清潔的程式碼**:無需手動迴圈和錯誤處理管理 - **原子操作**:所有頁面以單一操作蓋章 ## 進階沖壓技術 <!--說明:說明程式碼概念的圖表或截圖 --> ### 沖印前自訂 BarCode 外觀 在 PDF 上印上您的 BarCode 之前,請自訂其外觀。 IronBarcode offers extensive customization options: ```csharp 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.ChangeBarcodeForegroundColor(System.Drawing.Color.DarkBlue); // Now stamp the customized barcode myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1); ``` ### 使用不同的 BarCode 類型 不同的場景需要不同的 BarCode 類型。QR 條碼適合 URL 和大型資料集,而 Code128 則適用於字母數字識別碼。 Learn more about creating QR codes or explore other formats: ```csharp // 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 蓋章作業時,實施正確的錯誤處理: ```csharp 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.**批次作業**:使用 `StampToExistingPdfPages()` 取代循環 `StampToExistingPdfPage()` 2.**Barcode 快取**:一次建立並重複使用 `GeneratedBarcode` 物件 3.**座標計算**:預先計算一致的位置座標 4.**記憶體管理**:分批處理非常大的 PDF For advanced scenarios involving reading barcodes from PDFs after stamping, see our guide on reading barcodes from PDF documents. ## 與其他 IronBarcode 功能整合。 PDF 燙印功能可與其他 IronBarcode 功能無縫配合。 Combine it with asynchronous processing for better web application performance: ```csharp // 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); }); } ``` Additionally, leverage IronBarcode's image correction features when working with scanned PDFs that might need enhancement before or after barcode stamping. ## 疑難排解常見問題 如果在 PDF 上標示 BarCode 時遇到問題,以下是解決方案: 1.**座標問題**:PDF 座標從左下角開始,而非左上角 2.**Password-Protected PDFs**:確保加密 PDF 的密碼參數正確無誤 3. **Large File Sizes**: For optimization and handling tips, see our troubleshooting guide 4. **Font or Encoding Issues**: For special characters or Unicode, check our writing Unicode barcodes guide 遵循這些準則,並利用 IronBarcode 的 PDF 燙印功能,可以在保持高性能和代碼質量的同時,有效地將條碼添加到現有的 PDF 文件中。

常見問題解答

如何在 C# 中將 BarCode 新增至現有的 PDF 文件?

使用 IronBarcode 的 CreateBarcode 方法生成條碼,然後應用 StampToExistingPdfPage 方法將其放置在 PDF 上。只需指定PDF文件路徑,位置坐標(x,y),以及您希望條碼出現的頁碼。

StampToExistingPdfPage 方法需要哪些參數?

IronBarcode 中的 StampToExistingPdfPage 方法需要:pdfFilePath(PDF 位置的字符串)、x 和 y 坐標(像素定位的整數)、pageNumber(整數,1-索引),以及受保護 PDF 的可選密碼參數。

我可以在 PDF 的多頁上標示相同的 BarCode 嗎?

是的,IronBarcode 提供 StampToExistingPdfPages 方法(注意复数'Pages'),它允许您在 PDF 文档中的多个页面上加盖一个生成的条形码。

在 PDF 上定位 BarCode 時使用何種坐標系統?

IronBarcode 使用基於像素的坐標系統,當使用 StampToExistingPdfPage 方法時,x 坐標從頁面的左邊開始量測,y 坐標從頁面的底邊開始量測。

在現有 PDF 上標示 BarCode 的常見用例有哪些?

IronBarcode 的 PDF 燙印功能常用於在出貨標籤上加入獨特的追蹤代碼、製造報告上的批次號碼、法律表格上的文件控制號碼,以及用於數位驗證或快速存取連結的 QR 代碼。

在 PDF 上標示 BarCode 是否需要儲存中間檔案?

不,IronBarcode 的 StampToExistingPdfPage 方法可直接在 PDF 文档上加印条形码,而无需创建临时文件,从而节省了处理时间和存储空间。

我可以在受密碼保護的 PDF 文件上標示 BarCode 嗎?

是的,IronBarcode 支持在受密码保护的 PDF 上加印条码。只需在 StampToExistingPdfPage 方法中提供 PDF 密碼作為可選參數即可。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 2,002,059 | 版本: 2025.12 剛發表