Add Barcodes in HTML to PDF
Beyond providing extensive support for custom fonts such as Google fonts, IronPDF also supports stamping barcode onto your PDF documents. This allows developers to include barcode information in your rendered PDFs with ease.
Steps to Generate a PDF with a Barcode Using IronPDF
// Import the IronPDF library at the beginning of your file
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Engines;
/// <summary>
/// This example demonstrates how to use IronPDF to render barcodes into your PDF documents.
/// Ensure the IronPDF library is installed and correctly set up in your project.
/// </summary>
class Program
{
static void Main(string[] args)
{
// Initialize an instance of ChromePdfRenderer, which is used to render HTML to high-quality PDF
var renderer = new ChromePdfRenderer();
// Set rendering options, including a delay to ensure HTML content is fully loaded
renderer.RenderingOptions.WaitFor.RenderDelay = 500;
// Here's a sample HTML string to be converted to PDF, including links to Google Fonts
string barcodeHtml = "<html><head><link href='https://fonts.googleapis.com/css?family=Libre+Barcode+128' rel='stylesheet'></head><body><p style='font-family= \"Libre Barcode 128\", sans-serif;'>1234567890</p></body></html>";
// Render the HTML content to a PDF document
var doc = renderer.RenderHtmlAsPdf(barcodeHtml);
// Create a barcode stamp, specify the text and encoding (Code39 in this example)
var barcodeStamp = new BarcodeStamper("Hello World", IronBarCode.BarcodeEncoding.Code39);
// Apply the barcode stamp to the PDF document
doc.ApplyStamp(barcodeStamp);
// Save the generated PDF document to a file
doc.SaveAs("bc-test.pdf");
}
}
// Import the IronPDF library at the beginning of your file
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Engines;
/// <summary>
/// This example demonstrates how to use IronPDF to render barcodes into your PDF documents.
/// Ensure the IronPDF library is installed and correctly set up in your project.
/// </summary>
class Program
{
static void Main(string[] args)
{
// Initialize an instance of ChromePdfRenderer, which is used to render HTML to high-quality PDF
var renderer = new ChromePdfRenderer();
// Set rendering options, including a delay to ensure HTML content is fully loaded
renderer.RenderingOptions.WaitFor.RenderDelay = 500;
// Here's a sample HTML string to be converted to PDF, including links to Google Fonts
string barcodeHtml = "<html><head><link href='https://fonts.googleapis.com/css?family=Libre+Barcode+128' rel='stylesheet'></head><body><p style='font-family= \"Libre Barcode 128\", sans-serif;'>1234567890</p></body></html>";
// Render the HTML content to a PDF document
var doc = renderer.RenderHtmlAsPdf(barcodeHtml);
// Create a barcode stamp, specify the text and encoding (Code39 in this example)
var barcodeStamp = new BarcodeStamper("Hello World", IronBarCode.BarcodeEncoding.Code39);
// Apply the barcode stamp to the PDF document
doc.ApplyStamp(barcodeStamp);
// Save the generated PDF document to a file
doc.SaveAs("bc-test.pdf");
}
}
' Import the IronPDF library at the beginning of your file
Imports IronPdf
Imports IronPdf.Rendering
Imports IronPdf.Engines
''' <summary>
''' This example demonstrates how to use IronPDF to render barcodes into your PDF documents.
''' Ensure the IronPDF library is installed and correctly set up in your project.
''' </summary>
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Initialize an instance of ChromePdfRenderer, which is used to render HTML to high-quality PDF
Dim renderer = New ChromePdfRenderer()
' Set rendering options, including a delay to ensure HTML content is fully loaded
renderer.RenderingOptions.WaitFor.RenderDelay = 500
' Here's a sample HTML string to be converted to PDF, including links to Google Fonts
Dim barcodeHtml As String = "<html><head><link href='https://fonts.googleapis.com/css?family=Libre+Barcode+128' rel='stylesheet'></head><body><p style='font-family= ""Libre Barcode 128"", sans-serif;'>1234567890</p></body></html>"
' Render the HTML content to a PDF document
Dim doc = renderer.RenderHtmlAsPdf(barcodeHtml)
' Create a barcode stamp, specify the text and encoding (Code39 in this example)
Dim barcodeStamp = New BarcodeStamper("Hello World", IronBarCode.BarcodeEncoding.Code39)
' Apply the barcode stamp to the PDF document
doc.ApplyStamp(barcodeStamp)
' Save the generated PDF document to a file
doc.SaveAs("bc-test.pdf")
End Sub
End Class
This example demonstrates how to use IronPDF to easily render barcodes into your PDF documents. First, ensure the IronPDF library is installed and imported into your project. Then, an instance of the ChromePdfRenderer
class is initialized, which allows you to render HTML content into high-quality PDF documents.
Next, you need to create your barcode content. In our case, we have created an HTML string that incorporates a Google Fonts link for the "Libre Barcode 128" font. Then, use the RenderHtmlAsPdf
method to turn the HTML content into a new PDF document.
Following the HTML rendering, a barcode stamp is created with the text "Hello World" and the Code39 encoding format. This stamp is then applied to the PDF document using the ApplyStamp
method.
Finally, the generated PDF is saved to a file named "bc-test.pdf" using the SaveAs
method. This straightforward process enables you to produce a PDF with both styled text and barcode content efficiently in your C# applications. It should be noted that in some cases, this functionality won't support your barcode type, such as with QR Codes. In these instances, an efficient solution would be to use IronBarcode to generate this barcode or QR Code and then add it to your PDF as an image.
Click here to view the How-to Guide, including examples, sample code, and files