Working with Barcode
23 Jul 202624 minutes to read
Essential® PDF provides support to add barcodes to a PDF document. The following barcode types are supported:
- One-dimensional barcodes, including Code 39 and Code 32.
- Two-dimensional barcodes, such as QR Code, Data Matrix, and PDF417.
To quickly get started with QR codes in PDF documents using the Syncfusion® .NET PDF Library, watch this video:
Adding a one dimensional barcode to the PDF document
The below code example shows how to add Code39 barcode using the PdfCode39Barcode class to a PDF document.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create the Code 39 barcode
PdfCode39Barcode barcode = new PdfCode39Barcode();
// Set the height of the barcode
barcode.BarHeight = 45;
// Set the barcode text
barcode.Text = "CODE39$";
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("CODE39.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create the Code 39 barcode
PdfCode39Barcode barcode = new PdfCode39Barcode();
// Set the height of the barcode
barcode.BarHeight = 45;
// Set the barcode text
barcode.Text = "CODE39$";
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("CODE39.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Create the Code 39 barcode
Dim barcode As New PdfCode39Barcode()
' Set the height of the barcode
barcode.BarHeight = 45
' Set the barcode text
barcode.Text = "CODE39$"
' Draw the barcode on the PDF page
barcode.Draw(page, New PointF(25, 70))
' Save the document
document.Save("CODE39.pdf")
' Close the document
document.Close(True)You can download a complete working sample from GitHub.
The following code snippet shows how to add an EAN-13 barcode to a PDF document using the PdfEan13Barcode class.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new EAN-13 barcode
PdfEan13Barcode barcode = new PdfEan13Barcode();
// Set the height of the barcode
barcode.BarHeight = 50;
// Set the barcode text
barcode.Text = "400638133393";
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("EAN13Barcode.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new EAN-13 barcode
PdfEan13Barcode barcode = new PdfEan13Barcode();
// Set the height of the barcode
barcode.BarHeight = 50;
// Set the barcode text
barcode.Text = "400638133393";
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("EAN13Barcode.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new EAN-13 barcode
Dim barcode As New PdfEan13Barcode()
' Set the height of the barcode
barcode.BarHeight = 50
' Set the barcode text
barcode.Text = "400638133393"
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Draw the barcode on the PDF page
barcode.Draw(page, New PointF(25, 70))
' Save the document
document.Save("EAN13Barcode.pdf")
' Close the document
document.Close(True)You can download a complete working sample from GitHub.
The following code example shows how to add an EAN-8 barcode to a PDF document using the PdfEan8Barcode class.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new EAN-8 barcode
PdfEan8Barcode barcode = new PdfEan8Barcode();
// Set the height of the barcode
barcode.BarHeight = 50;
// Set the barcode text
barcode.Text = "1234567";
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("EAN8Barcode.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new EAN-8 barcode
PdfEan8Barcode barcode = new PdfEan8Barcode();
// Set the height of the barcode
barcode.BarHeight = 50;
// Set the barcode text
barcode.Text = "1234567";
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("EAN8Barcode.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new EAN-8 barcode
Dim barcode As New PdfEan8Barcode()
' Set the height of the barcode
barcode.BarHeight = 50
' Set the barcode text
barcode.Text = "1234567"
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Draw the barcode on the PDF page
barcode.Draw(page, New PointF(25, 70))
' Save the document
document.Save("EAN8Barcode.pdf")
' Close the document
document.Close(True)You can download a complete working sample from GitHub.
Adding a two-dimensional barcode to a PDF document
The following code snippet shows how to add a QR code to a PDF document using the PdfQRBarcode class.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new QR barcode
PdfQRBarcode barcode = new PdfQRBarcode();
// Set the error correction level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
// Set the XDimension (block size) of the barcode
barcode.XDimension = 3;
// Set the barcode text
barcode.Text = "http://www.syncfusion.com";
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("QRBarcode.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new QR barcode
PdfQRBarcode barcode = new PdfQRBarcode();
// Set the error correction level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
// Set the XDimension (block size) of the barcode
barcode.XDimension = 3;
// Set the barcode text
barcode.Text = "http://www.syncfusion.com";
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("QRBarcode.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new QR barcode
Dim barcode As New PdfQRBarcode()
' Set the error correction level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High
' Set the XDimension (block size) of the barcode
barcode.XDimension = 3
' Set the barcode text
barcode.Text = "http://www.syncfusion.com"
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Draw the barcode on the PDF page
barcode.Draw(page, New PointF(25, 70))
' Save the document
document.Save("QRBarcode.pdf")
' Close the document
document.Close(True)You can download a complete working sample from GitHub.
The following code example shows how to add a PDF417 barcode to a PDF document using the Pdf417Barcode class.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF417 barcode
Pdf417Barcode barcode = new Pdf417Barcode();
// Set the error correction level
barcode.ErrorCorrectionLevel = Pdf417ErrorCorrectionLevel.Auto;
// Set the XDimension (block size) of the barcode
barcode.XDimension = 2;
// Set the barcode text
barcode.Text = "http://www.syncfusion.com";
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("417Barcode.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF417 barcode
Pdf417Barcode barcode = new Pdf417Barcode();
// Set the error correction level
barcode.ErrorCorrectionLevel = Pdf417ErrorCorrectionLevel.Auto;
// Set the XDimension (block size) of the barcode
barcode.XDimension = 2;
// Set the barcode text
barcode.Text = "http://www.syncfusion.com";
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("417Barcode.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new PDF417 barcode
Dim barcode As New Pdf417Barcode()
' Set the error correction level
barcode.ErrorCorrectionLevel = Pdf417ErrorCorrectionLevel.Auto
' Set the XDimension (block size) of the barcode
barcode.XDimension = 2
' Set the barcode text
barcode.Text = "http://www.syncfusion.com"
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Draw the barcode on the PDF page
barcode.Draw(page, New PointF(25, 70))
' Save the document
document.Save("417Barcode.pdf")
' Close the document
document.Close(True)You can download a complete working sample from GitHub.
Setting the location and size of a barcode
The following code snippets show how to set the Size and Location for a Codabar barcode using the PdfCodabarBarcode class.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create a new instance of the Codabar barcode
PdfCodabarBarcode barcode = new PdfCodabarBarcode();
// Set the location of the barcode
barcode.Location = new PointF(100, 100);
// Set the size of the barcode
barcode.Size = new SizeF(200, 100);
// Set the barcode text
barcode.Text = "123456789$";
// Draw the barcode on the PDF page
barcode.Draw(page);
// Save the document
document.Save("CODABAR.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create a new instance of the Codabar barcode
PdfCodabarBarcode barcode = new PdfCodabarBarcode();
// Set the location of the barcode
barcode.Location = new PointF(100, 100);
// Set the size of the barcode
barcode.Size = new SizeF(200, 100);
// Set the barcode text
barcode.Text = "123456789$";
// Draw the barcode on the PDF page
barcode.Draw(page);
// Save the document
document.Save("CODABAR.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Create a new instance of the Codabar barcode
Dim barcode As New PdfCodabarBarcode()
' Set the location of the barcode
barcode.Location = New PointF(100, 100)
' Set the size of the barcode
barcode.Size = New SizeF(200, 100)
' Set the barcode text
barcode.Text = "123456789$"
' Draw the barcode on the PDF page
barcode.Draw(page)
' Save the document
document.Save("CODABAR.pdf")
' Close the document
document.Close(True)You can download a complete working sample from GitHub.
Adding a barcode without displaying the barcode text
The following code example shows how to add a barcode to a PDF document without displaying the barcode text by setting TextDisplayLocation to TextLocation.None from the TextLocation enumeration.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create a new instance of the Code 39 barcode
PdfCode39Barcode barcode = new PdfCode39Barcode();
// Set the barcode location
barcode.Location = new PointF(10, 10);
// Set the barcode text
barcode.Text = "123456789";
// Disable the barcode text
barcode.TextDisplayLocation = TextLocation.None;
// Draw the barcode on the PDF page
barcode.Draw(page);
// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create a new instance of the Code 39 barcode
PdfCode39Barcode barcode = new PdfCode39Barcode();
// Set the barcode location
barcode.Location = new PointF(10, 10);
// Set the barcode text
barcode.Text = "123456789";
// Disable the barcode text
barcode.TextDisplayLocation = TextLocation.None;
// Draw the barcode on the PDF page
barcode.Draw(page);
// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Create a new instance of the Code 39 barcode
Dim barcode As New PdfCode39Barcode()
' Set the barcode location
barcode.Location = New PointF(10, 10)
' Set the barcode text
barcode.Text = "123456789"
' Disable the barcode text
barcode.TextDisplayLocation = TextLocation.None
' Draw the barcode on the PDF page
barcode.Draw(page)
' Save the document
document.Save("Output.pdf")
' Close the document
document.Close(True)You can download a complete working sample from GitHub.
Exporting a barcode as an image
Essential® PDF supports converting one-dimensional barcodes such as Code 39, Code 39 Extended, Code 11, Codabar, Code 32, Code 93, Code 93 Extended, Code 128A, Code 128B, UPC bar code, and Code 128C to an image using the ToImage() method of the PdfUnidimensionalBarcode class.
NOTE
To export a barcode as an image in cross-platform (.NET Core / .NET 5+) applications, the Syncfusion.Pdf.Imaging.Net.Core NuGet package must be referenced in your application.
The following code example shows how to convert a one-dimensional barcode to an image.
using System.IO;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Barcode;
// Initialize a new PdfCode39Barcode instance
PdfCode39Barcode barcode = new PdfCode39Barcode();
// Set the height and text for the barcode
barcode.BarHeight = 45;
barcode.Text = "CODE39$";
// Convert the barcode to an image stream
Stream imageStream = barcode.ToImage(new SizeF(300, 300));
// Save the image to disk
using (FileStream outputFileStream = new FileStream("Image.png", FileMode.Create, FileAccess.ReadWrite))
{
imageStream.CopyTo(outputFileStream);
}using System.Drawing;
using Syncfusion.PDF.Graphics;
using Syncfusion.Pdf.Barcode;
// Initialize a new PdfCode39Barcode instance
PdfCode39Barcode barcode = new PdfCode39Barcode();
// Set the height and text for the barcode
barcode.BarHeight = 45;
barcode.Text = "CODE39$";
// Convert the barcode to an image
Image barcodeImage = barcode.ToImage(new SizeF(300, 200));
// Save the image
barcodeImage.Save("Image.png", ImageFormat.Png);Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Syncfusion.Pdf.Barcode
' Initialize a new PdfCode39Barcode instance
Dim barcode As New PdfCode39Barcode()
' Set the height and text for the barcode
barcode.BarHeight = 45
barcode.Text = "CODE39$"
' Convert the barcode to an image
Dim barcodeImage As Image = barcode.ToImage(New SizeF(300, 200))
' Save the image
barcodeImage.Save("Image.png", ImageFormat.Png)You can download a complete working sample from GitHub.
Essential® PDF also supports converting two-dimensional barcodes such as QR Code and Data Matrix to an image. The following code snippet explains how to convert a QR code to an image using the ToImage method of the PdfQRBarcode class.
using System.IO;
using Syncfusion.Drawing;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Initialize a new PdfQRBarcode instance
PdfQRBarcode barcode = new PdfQRBarcode();
// Set the XDimension and text for the barcode
barcode.XDimension = 3;
barcode.Text = "http://www.google.com";
// Convert the barcode to an image stream
Stream imageStream = barcode.ToImage(new SizeF(300, 300));
// Save the image to disk
using (FileStream outputFileStream = new FileStream("Output.png", FileMode.Create, FileAccess.ReadWrite))
{
imageStream.CopyTo(outputFileStream);
}using System.Drawing;
using System.Drawing.Imaging;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
// Initialize a new PdfQRBarcode instance
PdfQRBarcode barcode = new PdfQRBarcode();
// Set the XDimension and text for the barcode
barcode.XDimension = 3;
barcode.Text = "http://www.google.com";
// Convert the barcode to an image
Image barcodeImage = barcode.ToImage(new SizeF(300, 300));
// Save the image
barcodeImage.Save("Image.png", ImageFormat.Png);Imports System.Drawing
Imports System.Drawing.Imaging
Imports Syncfusion.Pdf.Barcode
' Initialize a new PdfQRBarcode instance
Dim barcode As New PdfQRBarcode()
' Set the XDimension and text for the barcode
barcode.XDimension = 3
barcode.Text = "http://www.google.com"
' Convert the barcode to an image
Dim barcodeImage As Image = barcode.ToImage(New SizeF(300, 300))
' Save the image
barcodeImage.Save("Image.png", ImageFormat.Png)You can download a complete working sample from GitHub.
Customizing the barcode appearance
The height of a one-dimensional barcode can be changed using the BarHeight property. The equivalent property to change the block size for a two-dimensional barcode is XDimension. You can also customize the barcode color by changing the BarColor (for dark bars) and BackColor (for light bars) properties.
NOTE
This color customization is supported only for one-dimensional barcodes; it is not supported for two-dimensional barcodes.
The following code sample explains how to customize a one-dimensional barcode such as Code 93 using the BarHeight and BarColor properties of the PdfCode93Barcode class.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create a new PdfCode93Barcode
PdfCode93Barcode code93 = new PdfCode93Barcode("CODE93");
// Set the barcode height
code93.BarHeight = 40;
// Set the barcode color
code93.BarColor = Color.Blue;
// Draw the barcode on the PDF page
code93.Draw(page, new PointF(25, 500));
// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create a new PdfCode93Barcode
PdfCode93Barcode code93 = new PdfCode93Barcode("CODE93");
// Set the barcode height
code93.BarHeight = 40;
// Set the barcode color
code93.BarColor = Color.Blue;
// Draw the barcode on the PDF page
code93.Draw(page, new PointF(25, 500));
// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Create a new PdfCode93Barcode
Dim code93 As New PdfCode93Barcode("CODE93")
' Set the barcode height
code93.BarHeight = 40
' Set the barcode color
code93.BarColor = Color.Blue
' Draw the barcode on the PDF page
code93.Draw(page, New PointF(25, 500))
' Save the document
document.Save("Output.pdf")
' Close the document
document.Close(True)The following code example shows how to customize a two-dimensional barcode such as a QR code using the XDimension and BackColor properties of the PdfBidimensionalBarcode class.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new QR barcode
PdfQRBarcode barcode = new PdfQRBarcode();
// Set the error correction level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
// Set the XDimension (block size) of the barcode
barcode.XDimension = 3;
// Set the barcode text
barcode.Text = "http://www.syncfusion.com";
// Set the background color
barcode.BackColor = Color.Green;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("QRBarcode.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new QR barcode
PdfQRBarcode barcode = new PdfQRBarcode();
// Set the error correction level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
// Set the XDimension (block size) of the barcode
barcode.XDimension = 3;
// Set the barcode text
barcode.Text = "http://www.syncfusion.com";
// Set the background color
barcode.BackColor = Color.Green;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Draw the barcode on the PDF page
barcode.Draw(page, new PointF(25, 70));
// Save the document
document.Save("QRBarcode.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new QR barcode
Dim barcode As New PdfQRBarcode()
' Set the error correction level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High
' Set the XDimension (block size) of the barcode
barcode.XDimension = 3
barcode.Text = "http://www.syncfusion.com"
' Set the background color
barcode.BackColor = Color.Green
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Draw the barcode on the PDF page
barcode.Draw(page, New PointF(25, 70))
' Save the document
document.Save("QRBarcode.pdf")
' Close the document
document.Close(True)Creating a QR code with a logo
The QRCodeLogo class represents a logo image that can be embedded in a QR code, and the Logo property of the PdfQRBarcode class is used to assign the logo. By leveraging these features, you can generate QR codes that incorporate custom logos or images, resulting in a visually appealing and branded QR code experience.
The following code example demonstrates how to generate a QR barcode with a logo positioned at its center.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create a new PDF QR barcode
PdfQRBarcode qrBarcode = new PdfQRBarcode();
// Set the barcode text
qrBarcode.Text = "https://www.syncfusion.com/";
// Create the QR barcode logo
QRCodeLogo qrCodeLogo = new QRCodeLogo("logo.png");
// Set the QR barcode logo
qrBarcode.Logo = qrCodeLogo;
// Set the XDimension (block size) of the barcode
qrBarcode.XDimension = 5;
// Draw the barcode on the PDF page
qrBarcode.Draw(page);
// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a new page to the PDF document
PdfPage page = document.Pages.Add();
// Create a new PDF QR barcode
PdfQRBarcode qrBarcode = new PdfQRBarcode();
// Set the barcode text
qrBarcode.Text = "https://www.syncfusion.com/";
// Create the QR barcode logo
QRCodeLogo qrCodeLogo = new QRCodeLogo("logo.png");
// Set the QR barcode logo
qrBarcode.Logo = qrCodeLogo;
// Set the XDimension (block size) of the barcode
qrBarcode.XDimension = 5;
// Draw the barcode on the PDF page
qrBarcode.Draw(page);
// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics
' Create a new PDF document
Dim document As New PdfDocument()
' Add a new page to the PDF document
Dim page As PdfPage = document.Pages.Add()
' Create a new PDF QR barcode
Dim qrBarcode As New PdfQRBarcode()
' Set the barcode text
qrBarcode.Text = "https://www.syncfusion.com/"
' Create the QR barcode logo
Dim qrCodeLogo As New QRCodeLogo("logo.png")
' Set the QR barcode logo
qrBarcode.Logo = qrCodeLogo
' Set the XDimension (block size) of the barcode
qrBarcode.XDimension = 5
' Draw the barcode on the PDF page
qrBarcode.Draw(page)
' Save the document
document.Save("Output.pdf")
' Close the document
document.Close(True)Supported QR code error correction levels
The ErrorCorrectionLevel property can be used to specify the error correction level when generating QR codes, ensuring the QR code can withstand potential damage based on its intended usage.
| Error Correction Level | Tolerance to Damage | Description |
|---|---|---|
| Low | Up to 7% | This is the lowest error correction level. It allows the QR code to store the maximum amount of data but can tolerate only minimal damage or obstruction (up to 7%). It is less resilient to damage. |
| Medium | Up to 15% | The medium error correction level balances data capacity and error resilience. It can recover from up to 15% damage or obstruction in the QR code. |
| Quartile | Up to 25% | The quartile level can tolerate moderate damage (up to 25%). This level is useful in environments where the QR code is exposed to significant wear or damage. It reduces data capacity but increases resilience. |
| High | Up to 30% | The highest error correction level provides the maximum resilience to damage. It can recover from up to 30% damage or obstruction, but it stores the least amount of data. It is used when durability is more important than data capacity. |
Supported barcode types
The following table contains the supported types and associated valid characters.
|
Symbol |
Supported characters |
Length |
|---|---|---|
|
|
[0-9]; [A-Z (upper-case only)]; [space $ % * + - . / , :]; [Shift JIS characters] |
variable |
|
|
All ASCII characters |
variable |
|
|
[0-9]; [A-Z]; [- . $ / + % SPACE] |
variable |
|
|
[0-9]; [A-Z]; [a-z] |
variable |
|
|
[0-9]; [-] |
variable |
|
|
[0-9]; [- $ : / . +] |
variable |
|
Code 32 |
[0-9] |
8 |
|
|
[0-9]; [A-Z]; [- . $ / + % SPACE] |
variable |
|
|
All 128 ASCII characters |
variable |
|
|
[0-9]; [A-Z]; [NUL (0x00) SOH (0x01) STX (0x02) ETX (0x03) EOT (0x04) ENQ (0x05) ACK (0x06) BEL (0x07) BS (0x08) HT (0x09) LF (0x0A) VT (0x0B) FF (0x0C) CR (0x0D) SO (0x0E) SI (0x0F) DLE (0x10) DC1 (0x11) DC2 (0x12) DC3 (0x13) DC4 (0x14) NAK (0x15) SYN (0x16) ETB (0x17) CAN (0x18) EM (0x19) SUB (0x1A) ESC (0x1B) FS (0x1C) GS (0x1D) RS (0x1E) US (0x1F) SPACE (0x20)]; [" ! # $ % & ' ( ) * + , - . / ; < = > ? @ [ / ] ^ _ ] |
variable |
|
|
[0-9]; [A-Z]; [a-z]; [SPACE (0x20) ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ / ] ^ _ ` { | } ~ DEL] |
variable |
|
|
ASCII 00-99 (encodes each two-digit group with one code) |
variable |
|
|
[0-9]; [A-Z]; [a-z]; Mixed; Punctuation; Byte |
variable |
|
|
[0-9]; |
7 or 8 |
|
|
[0-9]; |
12 or 13 |