IronBarcode 操作指南 .NET 标记条形码PDF 如何使用 C# 在 PDF 上加印 BarCode; Hairil Hasyimi Bin Omar 已更新:2025年12月14日 下载 IronBarcode NuGet 下载 DLL 下载 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English 使用 IronBarcode 的 CreateBarcode 方法,在 C# 中将条形码添加到现有的 PDF 文档中,对于单页使用 StampToExistingPdfPage,对于多页使用 StampToExistingPdfPages,并指定坐标和页码。 快速入门:将生成的条形码添加到 PDF 页面 本示例演示如何使用 IronBarCode 的 CreateBarcode 生成条形码,并将其印制到现有的 PDF 页面上。 提供 PDF 路径、位置坐标和页码。 使用 NuGet 包管理器安装 https://www.nuget.org/packages/BarCode PM > Install-Package BarCode 复制并运行这段代码。 IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1); 部署到您的生产环境中进行测试 通过免费试用立即在您的项目中开始使用IronBarcode Free 30 Day Trial 最小工作流程(5 个步骤) 下载 C# 库,用于在 PDF 上添加条形码 创建具有指定条码类型和值的条形码 指定条形码大小 使用`StampToExistingPdfPage`方法将条形码添加到单个 PDF 页面上 使用`StampToExistingPdfPages`方法将条形码添加到多个 PDF 页面上 如何在现有 PDF 页面上加印条形码? Apart from exporting barcodes as PDF, IronBarcode enables stamping the GeneratedBarcode directly onto existing PDF documents. 该功能在为现有报告、发票或表格添加跟踪代码、库存标签或文档标识符时非常有用。 下面的代码片段演示了这项任务。 :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"); $vbLabelText $csharpLabel StampToExistingPdfPage 需要哪些参数? 该代码片段使用 StampToExistingPdfPage() 对象调用 GeneratedBarcode 方法,将该对象印章到 PDF 文档上。 这种方法既灵活又简单。 以下是方法参数: pdfFilePath: 表示 PDF 文档路径(相对或绝对)的 System.String 。 x: 表示从左边缘开始的水平位置(以像素为单位)的 System.Int32 。 y: 一个System.Int32 类型的值,表示从底部边缘开始的垂直位置(以像素为单位)。 pageNumber: 一个System.Int32表示页码(从 1 开始索引,第一页为 1)。 password: 用于受密码保护的 PDF 的System.String (可选)。 何时应使用 StampToExistingPdfPage? 运行此代码片段会将 GeneratedBarcode 直接添加到 PDF 文档中,而无需中间保存。 这种方法适用于以下要求的场景: 运输标签或交货文件上的唯一跟踪代码 生产报告中的批号 法律或监管表格上的文件控制号 用于数字认证或快速访问链接的 QR 码 直接加盖公章的方法可节省处理时间,并消除临时文件。 For information on different barcode types, see the supported barcode formats guide. 如何在多个 PDF 页面上加盖条形码? 有时,同一个 BarCode 需要在多个页面上盖章。 常见用途包括在多页报告的每一页上应用文档标识符,在整个技术文档中添加版本控制代码,或在机密材料的每一页上插入安全条形码。 不要循环使用单页方法,而是使用 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"); $vbLabelText $csharpLabel 为提高灵活性,请使用 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"); // 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"); $vbLabelText $csharpLabel StampToExistingPdfPages 接受哪些参数? 以下是方法参数: pdfFilePath: 表示 PDF 文档路径的System.String 。 x: 以像素为单位的水平位置的System.Int32 。 y: 一个System.Int32 类型的值,表示以像素为单位的垂直位置。 pageNumbers: 一个IEnumerable待盖章的页数(1-索引)。 password: 用于受密码保护的 PDF 的System.String (可选)。 为什么使用 StampToExistingPdfPages 而不是循环? 这种方法可在多个页面上提供高效的条形码戳记,无需手动迭代,提高了代码的可读性和性能。 内部实施优化了 PDF 处理,从而实现了以下结果: 执行速度更快:PDF 一次打开和处理,而不是多次打开和处理 降低内存使用率:大型 PDF 的高效资源管理 更简洁的代码:无需手动循环和错误处理管理 原子操作:所有页面在单个操作中盖章 高级冲压技术 打标前自定义 BarCode 外观 在 PDF 上印制条形码之前,请先自定义其外观。 IronBarcode offers extensive customization options: 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); 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); $vbLabelText $csharpLabel 使用不同的条形码类型 不同的场景需要不同的 BarCode 类型。QR 代码适用于 URL 和大型数据集,而 Code128 则适用于字母数字标识符。 Learn more about creating QR codes or explore other formats: // 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); // 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); $vbLabelText $csharpLabel 错误处理和最佳实践 在使用 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 } 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 } $vbLabelText $csharpLabel 性能考虑 在处理大型 PDF 文件或多次盖章操作时,请考虑以下提示: 1.批量操作:使用 StampToExistingPdfPages() 而不是循环 StampToExistingPdfPage() 2.条形码缓存:创建一次即可重复使用 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: // 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); }); } // 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); }); } $vbLabelText $csharpLabel 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 的密码参数正确无误 Large File Sizes: For optimization and handling tips, see our troubleshooting guide 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,108,094 | 版本: 2026.3 刚刚发布 免费试用 免费 NuGet 下载 总下载量:2,108,094 查看许可证 还在滚动吗? 想快速获得证据? PM > Install-Package BarCode 运行示例 观看您的字符串变成 BarCode。 免费 NuGet 下载 总下载量:2,108,094 查看许可证