如何使用 IronBarcode 在 PDF 文件中創建和標示條碼

如何使用 C# 在 PDF 上新增條碼

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

快速入門:將產生的條碼新增至 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, one of the most popular functionalities in IronBarcode is the ability to stamp the GeneratedBarcode directly onto a user's existing PDF document. 以下程式碼片段示範如何實現此功能。

: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")
$vbLabelText   $csharpLabel

從上面的程式碼片段中,我們只需使用GeneratedBarcode物件呼叫StampToExistingPdfPage()方法,即可將該物件新增至 PDF 文件中。 以下是此方法中使用的參數:

  • pdfFilePath : 表示記憶體中 PDF 文件路徑的System.String
  • x : 一個System.Int32 值,表示 PDF 頁面上的水平位置(以像素為單位)。
  • y :一個System.Int32 類型值,表示 PDF 頁面上的垂直位置(以像素為單位)。
  • pageNumber :一個System.Int32 類型的值,表示要加蓋戳記的 PDF 頁碼。請注意,此值從 1 開始索引,因此第一頁表示為1
  • password : 一個System.String類型的字串,表示開啟 PDF 所需的密碼。 這是可選的,僅用於受密碼保護的 PDF 檔案。 如果 PDF 檔案未設定密碼保護,使用者可以將此參數留空。

執行上面的程式碼片段會將GeneratedBarcode立即新增到 PDF 文件中,而無需中間儲存文件的步驟。

在多個 PDF 頁面上加蓋條碼

有時,同一個條碼需要印在多張紙上,而不是一張紙上。 使用者無需循環使用上述方法在多個頁面上蓋印相同的條碼,而是可以使用GeneratedBarcode類別中的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")
$vbLabelText   $csharpLabel

以下是此方法中使用的參數:

  • pdfFilePath : 表示記憶體中 PDF 文件路徑的System.String
  • x : 一個System.Int32 值,表示 PDF 頁面上的水平位置(以像素為單位)。
  • y :一個System.Int32 類型值,表示 PDF 頁面上的垂直位置(以像素為單位)。
  • pageNumbers :一個IEnumerable 對象包含要蓋章的 PDF 頁碼。請注意,這些值從 1 開始索引,因此 PDF 的第一頁表示為1
  • password : 一個System.String類型的字串,表示開啟 PDF 所需的密碼。 這是可選的,僅用於受密碼保護的 PDF 檔案。 如果 PDF 檔案未設定密碼保護,使用者可以將此參數留空。

常見問題解答

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

透過 IronBarcode 函式庫,您可以在 C# 中在 PDF 文件上標示條碼。首先,從 NuGet 下載該函式庫,以指定的參數建立條碼,然後使用 StampToExistingPdfPageStampToExistingPdfPages 方法將條碼套用至 PDF 頁面上。

我應該使用何種方法在單一 PDF 頁面上標示 BarCode?

要在單一 PDF 頁面上標示條碼,請使用 IronBarcode 程式庫中的 StampToExistingPdfPage 方法。此方法需要一些參數,如 PDF 檔案路徑、位置的 x 和 y 座標、頁碼,以及受保護 PDF 的可選密碼。

如何在 PDF 的多頁上標示 BarCode?

若要在多個 PDF 頁面上標示條碼,請使用 StampToExistingPdfPages 方法。這允許您指定多個頁面號碼,並自動在每個頁面上標示條碼,而無需手動循環查看。

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

是的,您可以使用 IronBarcode 在受密碼保護的 PDF 上標示條碼。當使用 StampToExistingPdfPageStampToExistingPdfPages 方法時,您需要提供密碼作為可選參數。

在 PDF 上標示 BarCode 需要哪些參數?

所需的參數包括:文件路徑的 pdfFilePath 、位置的 xy 、目標頁面的 pageNumber 以及用於存取受保護 PDF 的選用 password

使用 IronBarcode 燙印條碼後,我需要保存 PDF 嗎?

是的,在燙印條碼之後,您應該使用 SaveAs 方法儲存修改後的 PDF 文件,以確保保留更改。

如何下載在 PDF 上標示 BarCode 的程式庫?

可從 NuGet 套件管理程式下載在 PDF 上標示 BarCode 的函式庫。在 nuGet.org/packages/IronPdf 中搜尋 IronPDF 套件。

我可以用 IronBarcode 創建哪些類型的條碼?

IronBarcode 可讓您建立各種類型的條碼,例如 QR 代碼、Code 128 和 UPC 等等。您可以在生成條碼時指定條碼類型。

是否可以在 PDF 頁面上精確定位 BarCode?

是的,您可以在 PDF 頁面上精確地定位 BarCode,方法是指定以像素為單位的 x 和 y 坐標,它們決定了在頁面上的水平和垂直位置。

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