如何使用 C# 在 PDF 上加印 BarCode;
using IronBarcode 的 CreateBarcode 方法,结合 StampToExistingPdfPage(单页)或 StampToExistingPdfPages(多页),指定坐标和页码,在 C# 中将条形码添加到现有 PDF 文档中。
快速入门:在 PDF 页面上盖上生成的条码
本示例演示了如何使用 IronBarcode 的 CreateBarcode 生成 BarCode,并将其添加到现有的 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
StampToExistingPdfPage
StampToExistingPdfPages
最小工作流程(5 个步骤)
- 下载 C# 库,用于在 PDF 上添加条形码
- 创建具有指定条码类型和值的条形码
- 指定条形码大小
- 使用
StampToExistingPdfPage方法将条形码添加到单个 PDF 页面上 - 使用
StampToExistingPdfPages方法将条形码添加到多个 PDF 页面上
如何在现有 PDF 页面上加印条形码?
除了将 BARCODE 导出为 PDF 外,IronBarcode 还支持将 GeneratedBarcode 直接加盖到现有的 PDF 文档上。 该功能在为现有报告、发票或表格添加跟踪代码、库存标签或文档标识符时非常有用。 下面的代码片段演示了这项任务。
: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")
StampToExistingPdfPage 需要哪些参数?
StampToExistingPdfPage()
GeneratedBarcode
该代码片段调用 StampToExistingPdfPage 方法并传入 StampSettings 对象,将该对象印制到 PDF 文档上。 这种方法既灵活又简单。 以下是方法参数:
pdfFilePath:表示 PDF 文档路径(相对或绝对)的 System.String 对象。x:一个 System.Int32 类型,表示距左边缘的水平位置(以像素为单位)。y:表示距底部边缘垂直位置(以像素为单位)的 System.Int32 类型。pageNumber:一个 System.Int32 类型,表示页码(从 1 开始计数,第一页为 1)。password:用于密码保护的 PDF 文件的 System.String 对象(可选)。
何时应使用 StampToExistingPdfPage?
运行该代码片段会将 GeneratedBarcode 直接嵌入 PDF 文档中,无需中间保存。 这种方法适用于以下要求的场景:
StampToExistingPdfPages()
GeneratedBarcode
- 运输标签或交货文件上的唯一跟踪代码
- 生产报告中的批号
- 法律或监管表格上的文件控制号
- 用于数字认证或快速访问链接的 QR 码
直接加盖公章的方法可节省处理时间,并消除临时文件。 有关不同条形码类型的信息,请参阅支持的条形码格式指南。
如何在多个 PDF 页面上加盖条形码?
有时,同一个 BarCode 需要在多个页面上盖章。 常见用途包括在多页报告的每一页上应用文档标识符,在整个技术文档中添加版本控制代码,或在机密材料的每一页上插入安全条形码。 请勿循环调用单页方法,而应使用 StampToExistingPdfPages 类中的 BarcodeStamper 方法。 下面的代码片段演示了这种方法。
: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")
为提高灵活性,请使用 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");
Imports System.Linq
' Stamp on all even pages from 2 to 10
Dim evenPages = Enumerable.Range(1, 10).Where(Function(x) x Mod 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
Dim selectedPages = New List(Of Integer) From {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</system.int32> 类型,从 1 开始计数)。password:用于密码保护的 PDF 文件的 System.String 对象(可选)。
StampToExistingPdfPages()
StampToExistingPdfPage()
GeneratedBarcode
为什么使用 StampToExistingPdfPages 而不是循环?
这种方法可在多个页面上提供高效的条形码戳记,无需手动迭代,提高了代码的可读性和性能。 内部实施优化了 PDF 处理,从而实现了以下结果:
- 执行速度更快:PDF 一次打开和处理,而不是多次打开和处理
- 降低内存使用率:大型 PDF 的高效资源管理
- 更简洁的代码:无需手动循环和错误处理管理
- 原子操作:所有页面在单个操作中盖章
高级冲压技术
打标前自定义 BarCode 外观
在 PDF 上印制条形码之前,请先自定义其外观。 IronBarcode 提供广泛的自定义选项:
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Co/de128, 250, 80);
// Customize the appearance
myBarcode.AddAnnotationTextAboveBarcode("Invoice Number");
myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy");
myBarcode.SetMargins(10);
myBarcode.ChangeBarcodeForegroundColor(System.Drawing.Co/lor.DarkBlue);
// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("INVOICE-2024-001", BarcodeEncoding.Co/de128, 250, 80);
// Customize the appearance
myBarcode.AddAnnotationTextAboveBarcode("Invoice Number");
myBarcode.AddAnnotationTextBelowBarcode("Scan for digital copy");
myBarcode.SetMargins(10);
myBarcode.ChangeBarcodeForegroundColor(System.Drawing.Co/lor.DarkBlue);
// Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x: 450, y: 700, pageNumber: 1);
Imports System.Drawing
Dim myBarcode As GeneratedBarcode = 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(Color.DarkBlue)
' Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x:=450, y:=700, pageNumber:=1)
使用不同的条形码类型
不同的场景需要不同的 BarCode 类型。QR 代码适用于 URL 和大型数据集,而 Code128 则适用于字母数字标识符。 了解有关 创建二维码 或探索其他格式的更多信息:
// 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);
' QR Code for contact information
Dim qrCode = BarcodeWriter.CreateBarcode("BEGIN:VCARD" & vbLf & "FN:John Doe" & vbLf & "TEL:555-1234" & vbLf & "END:VCARD",
BarcodeEncoding.QRCode, 150, 150)
qrCode.StampToExistingPdfPage("businesscard.pdf", x:=400, y:=50, pageNumber:=1)
' Data Matrix for product tracking
Dim dataMatrix = BarcodeWriter.CreateBarcode("PROD-2024-BATCH-789",
BarcodeEncoding.DataMatrix, 100, 100)
dataMatrix.StampToExistingPdfPage("product_sheet.pdf", x:=50, y:=750, pageNumber:=1)
错误处理和最佳实践
在使用 PDF 冲压操作时执行正确的错误处理:
try
{
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345",
BarcodeEncoding.Co/de128, 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.Co/de128, 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
}
Imports System
Imports System.IO
Try
Dim myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("DOCUMENT-ID-12345", BarcodeEncoding.Code128, 200, 60)
' Verify the PDF exists before attempting to stamp
If File.Exists("target.pdf") Then
myBarcode.StampToExistingPdfPage("target.pdf", x:=100, y:=100, pageNumber:=1)
Console.WriteLine("Barcode stamped successfully!")
Else
Console.WriteLine("PDF file not found!")
End If
Catch ex As Exception
Console.WriteLine($"Error stamping barcode: {ex.Message}")
' Log the error or handle it appropriately
End Try
性能考虑
在处理大型 PDF 文件或多次盖章操作时,请考虑以下提示:
- 批量操作:使用
StampToExistingPdfPages代替循环StampToExistingPdfPage - BARCODE缓存:创建一次并复用
GeneratedBarcode对象 3.坐标计算:预先计算一致的位置坐标 4.内存管理:批量处理超大型 PDF
如需了解在加盖戳记后从 PDF 中读取条形码的高级应用场景,请参阅我们的从 PDF 文档中读取条形码指南。
与其他 IronBarcode 功能集成
/csharp/barcode/ PDF 打标功能可与 IronBarcode 的其他功能无缝配合。 结合异步处理,以提高网络应用程序的性能:
// 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);
});
}
Imports System.Threading.Tasks
' Asynchronous PDF stamping
Public Async Function StampBarcodeAsync(pdfPath As String, barcodeData As String) As Task
Await Task.Run(Sub()
Dim barcode = BarcodeWriter.CreateBarcode(barcodeData, BarcodeEncoding.QRCode, 200, 200)
barcode.StampToExistingPdfPage(pdfPath, x:=100, y:=100, pageNumber:=1)
End Sub)
End Function
此外,当处理扫描的 PDF 文件时,可能需要在条码盖印之前或之后进行增强,请利用 IronBarcode 的图像校正功能。
ABCpdf 常见问题和解决方案
如果在 PDF 上加盖 BarCode 时遇到问题,这里有解决方案:
1.坐标问题:PDF 坐标从左下角开始,而不是从左上角开始 2.受密码保护的 PDF:确保加密 PDF 的密码参数正确无误 3.文件大小:有关优化和处理技巧,请参阅我们的故障排除指南。 4.字体或编码问题:对于特殊字符或 Unicode,请查看我们的编写 Unicode 条形码指南
遵循这些准则并利用 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 密码作为可选参数即可。
IronBarcode如何帮助提高业务流程的效率?
IronBarcode通过快速准确的条码生成和读取提高了业务流程效率,减少了手动数据输入错误,并改善了库存和资产跟踪。
在项目中实现IronBarcode需要哪些编程技能?
了解C#编程的基础知识就足以在项目中实现IronBarcode,因为它提供了简单的方法和全面的文档来指导开发人员。
IronBarcode适合小项目和大型企业应用吗?
IronBarcode被设计为可扩展且多功能,适合需要强大条码解决方案的小项目和大型企业应用。

