如何在PDF上盖章条形码(使用C#)
使用IronBarcode的CreateBarcode方法,结合StampToExistingPdfPage用于单页或StampToExistingPdfPages用于多页,将条形码贴在现有PDF文档上,指定坐标和页码。
此示例演示如何使用IronBarcode的CreateBarcode生成条形码并将其盖在现有PDF页面上。 提供 PDF 路径、位置坐标和页码。
-
1使用 IronBarcode 安装 NuGet Package Manager
-
2复制并运行这段代码。
IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) .StampToExistingPdfPage("report.pdf", x: 50, y: 50, pageNumber: 1);IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) _ .StampToExistingPdfPage("report.pdf", x:=50, y:=50, pageNumber:=1)IronBarCode.BarcodeWriter.CreateBarcode("https://my.site", IronBarCode.BarcodeEncoding.QRCode, 150, 150) _ .StampToExistingPdfPage("report.pdf", x:=50, y:=50, pageNumber:=1) -
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");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")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方法,将对象盖在PDF文档上。 这种方法既灵活又简单。 以下是方法参数:
pdfFilePath:一个System.String,表示PDF文档路径(相对或绝对)。x:一个System.Double,表示从左边缘开始的水平像素位置。y:一个System.Double,表示从底部边缘开始的垂直像素位置。pageNumber:一个System.Int32,表示页码(从1开始的索引,第一页为1)。password:一个System.String,用于有密码保护的PDF(可选)。
什么时候应该使用StampToExistingPdfPage?
运行代码片段将GeneratedBarcode直接盖在PDF文档中,无需中间保存。 这种方法适用于以下要求的场景:
StampToExistingPdfPages()
GeneratedBarcode
- 唯一的跟踪代码可以用于运输标签或交付文件
- 批号可以用于制造报告
- 文件控制号码可以用于法律或监管表格
- QR码可以用于数字身份验证或快速访问链接
直接加盖公章的方法可节省处理时间,并消除临时文件。 有关不同条形码类型的信息,请参阅支持的条形码格式指南。
如何在多个PDF页面上盖章条形码?
有时,同一个 BarCode 需要在多个页面上盖章。 常见用途包括在多页报告的每一页上应用文档标识符,在整个技术文档中添加版本控制代码,或在机密材料的每一页上插入安全条形码。 可以在GeneratedBarcode对象上使用StampToExistingPdfPages方法,而不是循环单页方法。 下面的代码片段演示了这种方法。
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");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")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")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.Double,表示水平像素位置。y:一个System.Double,表示垂直像素位置。pageNumbers: 要盖的页面的IEnumerable<System.Int32>(以1为索引)。password:一个System.String,用于有密码保护的PDF(可选)。
StampToExistingPdfPages()
StampToExistingPdfPage()
GeneratedBarcode
为什么使用StampToExistingPdfPages而不是循环?
这种方法可在多个页面上提供高效的条形码戳记,无需手动迭代,提高了代码的可读性和性能。 内部实施优化了 PDF 处理,从而实现了以下结果:
- 更快的执行: PDF只打开和处理一次,不是多次
- 更低的内存消耗: 对大型PDF的高效资源管理
- 更简洁的代码: 无需手动循环和错误处理管理
- 原子操作: 所有页面在一次操作中完成盖章
高级盖章技巧
在盖章前自定义条形码外观
在 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);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);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.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkBlue)
' Now stamp the customized barcode
myBarcode.StampToExistingPdfPage("invoice.pdf", x:=450, y:=700, pageNumber:=1)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.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码的信息或探索其他格式:
// 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)' 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
}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 TryImports 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 - 条形码缓存: 创建一次并重用
GeneratedBarcode对象 - 坐标计算: 预先计算一致的位置坐标
- 内存管理: 批量处理非常大的PDF
有关盖章后从PDF中读取条形码的高级场景,请参阅我们的从PDF文档中读取条形码指南。
与其他IronBarcode功能的集成
PDF盖章功能与其他IronBarcode功能配合使用。 将其与异步处理结合使用,以提高Web应用程序性能:
// 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 FunctionImports 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的图像校正功能。
故障排除常见问题
如果在 PDF 上加盖 BarCode 时遇到问题,这里有解决方案:
- 坐标问题: PDF坐标从左下角开始,而不是左上角
- 有密码保护的PDF: 输入正确的密码参数以处理加密的PDF
- 大文件大小: 有关优化和处理的提示,请参阅我们的故障排除指南
- 字体或编码问题: 针对特殊字符或Unicode,请查看我们的书写Unicode条形码指南
遵循这些准则并利用 IronBarcode 的 PDF 打标功能,可在保持高性能和代码质量的同时,高效地将条码添加到现有的 PDF 文档中。
常见问题
使用IronBarcode在PDF上加盖条形码的主要方法是什么?
使用IronBarcode在PDF上加盖条形码的主要方法是StampToExistingPdfPage方法。
IronBarcode可以一次在多个PDF页面上盖上条形码吗?
是的,IronBarcode支持在多个PDF页面上盖上条形码,使用StampToExistingPdfPages方法。
在PDF上加盖条形码的一些常见用例是什么?
在PDF上加盖条形码的常见用例包括在报告、发票或表格中添加跟踪码、库存标签或文件标识符。
如何在IronBarcode中指定PDF页面上条形码的位置?
在IronBarcode中,您可以使用x和y参数指定PDF页面上条形码的位置,这些参数表示像素坐标。
IronBarcode在加盖条形码时是否支持密码保护的PDF?
是的,IronBarcode支持密码保护的PDF。您可以在使用StampToExistingPdfPage或StampToExistingPdfPages方法时提供密码作为可选参数。
IronBarcode可以生成哪些类型的条形码用于PDF加盖?
IronBarcode可以生成各种条形码类型,包括二维码和Code128,可以加盖到PDF上。
在IronBarcode中如何在将条形码盖到PDF上之前自定义其外观?
您可以在IronBarcode中使用例如AddAnnotationTextAboveBarcode、AddAnnotationTextBelowBarcode及ChangeBarCodeColor的方法来自定义条形码的外观,然后再盖印。
为什么应该使用StampToExistingPdfPages方法而不是循环处理多个页面?
StampToExistingPdfPages方法用于多页面加盖更为高效,减少处理时间,相比手动循环页更优化资源使用。
在使用IronBarcode在大型PDF上加盖条形码时是否有性能上的考虑?
是的,当在大型PDF上加盖条形码时,建议使用批量操作、缓存条形码并有效管理内存,以提高IronBarcode的性能。
如果IronBarcode中的条形码加盖操作失败,应当怎么办?
如果条形码加盖操作失败,使用 try-catch 块实现错误处理,以记录错误并确保在使用 IronBarcode 加盖之前 PDF 文件已存在。