using IronPdf;
using IronPdf.Editing;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.WaitFor.RenderDelay(500);
// Load barcode via HTML
var barcodeHtml = @"<link href=""https://fonts.googleapis.com/css?family=Libre%20Barcode%20128""rel = ""stylesheet"" ><p style = ""font-family: 'Libre Barcode 128', serif; font-size:30px;""> Hello Google Fonts</p>";
var doc = renderer.RenderHtmlAsPdf(barcodeHtml);
// or use the BarcodeStamper
var barcodeStamp = new BarcodeStamper("Hello World", BarcodeEncoding.Code39);
doc.ApplyStamp(barcodeStamp);
// Export to a file or Stream
doc.SaveAs("bc-test.pdf");
Imports IronPdf
Imports IronPdf.Editing
' Instantiate Renderer
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.WaitFor.RenderDelay(500)
' Load barcode via HTML
Dim barcodeHtml As String = "<link href=""https://fonts.googleapis.com/css?family=Libre%20Barcode%20128"" rel=""stylesheet""><p style=""font-family: 'Libre Barcode 128', serif; font-size:30px;""> Hello Google Fonts</p>"
Dim doc As PdfDocument = renderer.RenderHtmlAsPdf(barcodeHtml)
' or use the BarcodeStamper
Dim barcodeStamp As New BarcodeStamper("Hello World", BarcodeEncoding.Code39)
doc.ApplyStamp(barcodeStamp)
' Export to a file or Stream
doc.SaveAs("bc-test.pdf")
Install-Package IronPdf
在HTML中添加条形码到PDF
除了提供对谷歌字体等自定义字体的广泛支持外,IronPDF还支持在您的PDF文档上加盖条形码。 这使得开发人员可以轻松地将条形码信息包含在渲染的 PDF 中。
使用 IronPDF 生成带有条形码的 PDF 的步骤
首先,请确保已安装 IronPdf 库并将其导入您的项目。 随后,初始化 ChromePdfRenderer 类的实例,从而能够将 HTML 内容渲染为高质量的 PDF 文档。
接下来,您需要创建您的条形码内容。 在我们的例子中,我们创建了一个包含 Google 字体 "Libre Barcode 128" 链接的 HTML 字符串。 随后,使用 RenderHtmlAsPdf 方法将 HTML 内容转换为新的 PDF 文档。
在 HTML 渲染之后,将生成一个 BarCode 水印,内容为"Hello World",采用 Code39 编码格式。 随后,该水印将通过 ApplyStamp 方法应用到 PDF 文档中。
最后,生成的 PDF 文件将通过 SaveAs 方法保存为名为 "bc-test.pdf" 的文件。 此简单过程使您能够在 C# 应用程序中高效地生成包含样式文本和条形码内容的 PDF。 需要注意的是,在某些情况下,这种功能不支持您的条形码类型,例如二维码。 在这些情况下,一个有效的解决方案是使用 IronBarcode 来生成此条形码或二维码,然后将其作为图像添加到 PDF 中。