How to Create and Stamp Barcodes in PDF Documents Using IronBarcode

How to Stamp Barcodes on PDFs

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

快速入门:在 PDF 页上打印生成的条形码

此一行代码示例展示了如何使用 IronBarcode 的 CreateBarcode 生成条形码后立即将其打印到现有的 PDF 页上。 您只需提供 PDF 路径、位置坐标、页码,就可以完成一个准备就绪的工作流程,设置最小。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150)
        .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1);
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小化工作流程(5步)

  1. 下载 C# 库以在 PDF 上打印条形码
  2. 创建具有指定条形码类型和值的条形码
  3. 指定条形码大小
  4. 使用 StampToExistingPdfPage 方法将条形码打印到单个 PDF 页上
  5. 使用 StampToExistingPdfPages 方法将条形码打印到多个 PDF 页上

在现有 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

从上面的代码片段中,我们只需调用 StampToExistingPdfPage() 方法,并使用 GeneratedBarcode 对象将该对象打印到 PDF 文档上。 下面是此方法中的参数:

  • pdfFilePath: 表示内存中 PDF 文档路径的 System.String
  • x: 表示 PDF 页面上水平位置的 System.Int32,单位为像素。
  • y: 表示 PDF 页面上垂直位置的 System.Int32,单位为像素。
  • pageNumber: 表示要打印的 PDF 页面的 System.Int32。注意此值是从1开始计数的,因此第一页表示为 1
  • password: 用于打开 PDF 的密码 System.String。此项为可选项,仅用于已加密的 PDF。 如果 PDF 没有密码保护,用户可以将此参数留空。 运行上面的代码片段将立即把 GeneratedBarcode 打印到 PDF 文档中,无需中间文档保存步骤。

在多个 PDF 页上打印条形码

有时需要将相同的条形码打印到多个页面上,而不是单个页面上。

用户可以使用 GeneratedBarcode 类中的 StampToExistingPdfPages() 方法直接执行此操作,而不是循环使用上述方法将相同条形码打印到多个页面上。 以下代码片段演示了如何使用此方法。 * pageNumbers: 包含要打印的 PDF 页面的 IEnumerable。注意这些值是从1开始计数的,因此 PDF 的第一页表示为 1

: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: 表示 PDF 页面上水平位置的 System.Int32,单位为像素。
  • y: 表示 PDF 页面上垂直位置的 System.Int32,单位为像素。
  • pageNumbers: An IEnumerable comprising the PDF pages to stamp. Note these values are 1-indexed, so the first page of a PDF is represented as 1.
  • password: 用于打开 PDF 的密码 System.String。此项为可选项,仅用于已加密的 PDF。 如果 PDF 没有密码保护,用户可以将此参数留空。 运行上面的代码片段将立即把 GeneratedBarcode 打印到 PDF 文档中,无需中间文档保存步骤。

常见问题解答

如何在C#中在PDF文档上标记条形码?

您可以使用IronBarcode库在C#中在PDF文档上标记条形码。首先,从NuGet下载库,创建具有指定参数的条形码,然后使用StampToExistingPdfPageStampToExistingPdfPages方法将条形码应用到PDF页面上。

我应该使用什么方法在单个PDF页面上标记条形码?

要在单个PDF页面上标记条形码,请使用IronBarcode库中的StampToExistingPdfPage方法。此方法需要参数,例如PDF文件路径、位置的x和y坐标、页码,以及保护PDF的可选密码。

我如何在PDF的多个页面上标记条形码?

要在多个PDF页面上标记条形码,请使用StampToExistingPdfPages方法。这允许您指定多个页码,并自动在这些页面上标记条形码,而无需手动遍历它们。

我可以在密码保护的PDF上标记条形码吗?

是的,您可以使用IronBarcode在密码保护的PDF上标记条形码。在使用StampToExistingPdfPageStampToExistingPdfPages方法时需要提供密码作为可选参数。

在PDF上标记条形码需要哪些参数?

所需的参数包括文档路径的pdfFilePath,位置的xy,目标页面的pageNumber,以及访问受保护PDF的可选password

用IronBarcode标记条形码后是否需要保存PDF?

是的,在标记条形码后,您应该使用SaveAs方法保存修改后的PDF文档,以确保更改得以保留。

我如何下载用于在PDF上标记条形码的库?

用于在PDF上标记条形码的库可以从NuGet包管理器下载。在nuget.org/packages/IronPdf搜索IronPdf包。

我可以用IronBarcode创建哪些类型的条形码?

IronBarcode允许您创建多种类型的条形码,例如QR码、Code 128和UPC等。您可以在生成条形码时指定条形码类型。

是否可以在PDF页面上准确定位条形码?

是的,您可以通过指定像素的x和y坐标来准确定位PDF页面上的条形码,这决定了页面上的水平和垂直位置。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 1,935,276 | 版本: 2025.11 刚刚发布