如何使用 C# 在 PDF 上加印 BarCode;
使用IronBarcode的StampToExistingPdfPages针对多页,指定坐标和页码。
这个示例演示了使用IronBarcode的CreateBarcode生成条形码并将其加盖到现有PDF页面上。 提供 PDF 路径、位置坐标和页码。
-
1Install IronBarcode with NuGet Package Manager
-
2复制并运行这段代码。
IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1);C# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronBarcode
StampToExistingPdfPage
StampToExistingPdfPages
最小工作流程(5 个步骤)
- 下载 C# 库,用于在 PDF 上添加条形码
- 创建具有指定条码类型和值的条形码
- 指定条形码大小
- 使用
StampToExistingPdfPage方法将条形码添加到单个 PDF 页面上 - 使用
StampToExistingPdfPages方法将条形码添加到多个 PDF 页面上
如何在现有 PDF 页面上加印条形码?
除了将条形码导出为PDF外,IronBarcode还可将GeneratedBarcode直接加盖到现有PDF文档。 该功能在为现有报告、发票或表格添加跟踪代码、库存标签或文档标识符时非常有用。 下面的代码片段演示了这项任务。
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
代码片段通过StampSettings对象,将对象加盖到PDF文档上。 这种方法既灵活又简单。 以下是方法参数:
pdfFilePath:一个System.String,表示PDF文档路径(相对或绝对)。x:一个System.Int32,表示从左边缘向右的水平位置(以像素为单位)。y:一个System.Int32,表示从底部向上的垂直位置(以像素为单位)。pageNumber:一个System.Int32,指明页码(1为起始页)。password:一个System.String,用于加密PDF(可选)。
何时应使用 StampToExistingPdfPage?
运行代码片段将GeneratedBarcode直接加盖到PDF文档中而无需中间保存。 这种方法适用于以下要求的场景:
StampToExistingPdfPages()
GeneratedBarcode
- 运输标签或交货文件上的唯一跟踪代码
- 生产报告中的批号
- 法律或监管表格上的文件控制号
- 用于数字认证或快速访问链接的 QR 码
直接加盖公章的方法可节省处理时间,并消除临时文件。 有关不同条形码类型的信息,请参阅支持的条形码格式指南。
如何在多个 PDF 页面上加盖条形码?
有时,同一个 BarCode 需要在多个页面上盖章。 常见用途包括在多页报告的每一页上应用文档标识符,在整个技术文档中添加版本控制代码,或在机密材料的每一页上插入安全条形码。 而不是循环单页方法,使用BarcodeStamper类。 下面的代码片段演示了这种方法。
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");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:一个System.String,表示PDF文档路径。x:一个System.Int32,用于表示水平位置(以像素为单位)。y:一个System.Int32,用于表示垂直位置(以像素为单位)。pageNumbers:一个**IEnumerable<System.Int32>**,用于加盖的页码(从1开始)。password:一个System.String,用于加密PDF(可选)。
StampToExistingPdfPages()
StampToExistingPdfPage()
GeneratedBarcode
为什么使用 StampToExistingPdfPages 而不是循环?
这种方法可在多个页面上提供高效的条形码戳记,无需手动迭代,提高了代码的可读性和性能。 内部实施优化了 PDF 处理,从而实现了以下结果:
- 执行速度更快:PDF 一次打开和处理,而不是多次打开和处理
- 降低内存使用率:大型 PDF 的高效资源管理
- 更简洁的代码:无需手动循环和错误处理管理
- 原子操作:所有页面在单个操作中盖章
高级冲压技术
打标前自定义 BarCode 外观
在 PDF 上印制条形码之前,请先自定义其外观。 IronBarcode 提供广泛的自定义选项:
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.ChangeBarCodeColor(IronSoftware.Drawing.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
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.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
}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 文件或多次盖章操作时,请考虑以下提示:
- 批处理操作:使用
StampToExistingPdfPage - 条形码缓存:创建一次并重用
GeneratedBarcode对象 3.坐标计算:预先计算一致的位置坐标 4.内存管理:批量处理超大型 PDF
如需了解在加盖戳记后从 PDF 中读取条形码的高级应用场景,请参阅我们的从 PDF 文档中读取条形码指南。
与其他 IronBarcode 功能集成
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);
});
}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?
using 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 密码作为可选参数即可。
Why should you use the StampToExistingPdfPages method instead of looping for multiple pages?
The `StampToExistingPdfPages` method is preferred for stamping on multiple pages because it is more efficient, reduces processing time, and optimizes resource usage compared to manually looping through pages.
Are there performance considerations when stamping barcodes on large PDFs with IronBarcode?
Yes, when stamping barcodes on large PDFs, it's recommended to use batch operations, cache barcodes, and manage memory efficiently for better performance with IronBarcode.
What should be done if a barcode stamping operation fails in IronBarcode?
If a barcode stamping operation fails, implement error handling using `try-catch` blocks to log errors and ensure that the PDF file exists before stamping with IronBarcode.
