How to Create and Stamp Barcodes in PDF Documents Using IronBarcode

如何使用 C# 在 PDF 上加印 BarCode;

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

在 C# 中使用 IronBarcode 的 CreateBarcode 方法将条形码印制到现有的 PDF 文档上,对于单页使用 StampToExistingPdfPage 方法,对于多页使用 StampToExistingPdfPages 方法,指定坐标和页码。

快速入门:在 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 需要在多个页面上盖章。 常见用途包括在多页报告的每一页上应用文档标识符,在整个技术文档中添加版本控制代码,或在机密材料的每一页上插入安全条形码。 请使用 `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 而不是循环? 这种方法可在多个页面上提供高效的条形码戳记,无需手动迭代,提高了代码的可读性和性能。 内部实施优化了 PDF 处理,从而实现了以下结果: - **执行速度更快**:PDF 一次打开和处理,而不是多次打开和处理 - **降低内存使用率**:大型 PDF 的高效资源管理 - **更简洁的代码**:无需手动循环和错误处理管理 - **原子操作**:所有页面在单个操作中盖章 ## 高级冲压技术 <!--说明:说明代码概念的图表或截图 --> ### 打标前自定义 BarCode 外观 在 PDF 上印制条形码之前,请先自定义其外观。 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 类型。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. ### ABCpdf 常见问题和解决方案 如果在 PDF 上加盖 BarCode 时遇到问题,这里有解决方案: 1.**坐标问题**:PDF 坐标从左下角开始,而不是从左上角开始 2.**受密码保护的 PDF**:确保加密 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# 中为现有 PDF 文档添加 BarCode?

使用 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 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 2,035,202 | 版本: 2025.12 刚刚发布