Working with Barcode
2 Sep 202424 minutes to read
Essential PDF provides support to add barcodes to the PDF document. The following barcode types are supported.
- 10 one-dimensional barcodes including Code 39 and Code 32 barcodes.
- 2 two-dimensional barcodes such as QR, DataMatrix and PDF417 barcodes.
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.
//Creating new PDF Document
PdfDocument doc = new PdfDocument();
//Adding new page to PDF document
PdfPage page = doc.Pages.Add();
//Drawing Code39 barcode
PdfCode39Barcode barcode = new PdfCode39Barcode();
//Setting height of the barcode
barcode.BarHeight = 45;
barcode.Text = "CODE39$";
//Printing barcode to the PDF document
barcode.Draw(page, new PointF(25, 70));
//Save the document into stream.
MemoryStream stream = new MemoryStream();
doc.Save(stream);
stream.Position = 0;
//Close the documents.
doc.Close(true);
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = " CODE39.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Creating new PDF Document
PdfDocument doc = new PdfDocument();
//Adding new page to PDF document
PdfPage page = doc.Pages.Add();
//Drawing Code39 barcode
PdfCode39Barcode barcode = new PdfCode39Barcode();
//Setting height of the barcode
barcode.BarHeight = 45;
barcode.Text = "CODE39$";
//Printing barcode to the PDF document.s
barcode.Draw(page, new PointF(25, 70 ));
//Saving the Document
doc.Save("CODE39.pdf");
//Close the PDF document
doc.Close(true);
'Creating new PDF Document
Dim doc As New PdfDocument()
'Adding new page to PDF document
Dim page As PdfPage = doc.Pages.Add()
'Drawing Code39 barcode
Dim barcode As New PdfCode39Barcode()
'Setting height of the barcode
barcode.BarHeight = 45
barcode.Text = "CODE39$"
'Printing barcode to the PDF document.
barcode.Draw(page, New PointF(25, 70))
'Save the Document
doc.Save("CODE39.pdf")
'Close the PDF document
doc.Close(true);
You can download a complete working sample from GitHub.
The below code snippet shows how to add PdfEan13 barcode using the PdfEan13Barcode class to a PDF document.
//Creates a new PdfEan13 Barcode.
PdfEan13Barcode barcode = new PdfEan13Barcode ();
//Set height of the barcode.
barcode.BarHeight = 50;
//Set the barcode text.
barcode.Text = "400638133393";
//Creating new PDF document.
PdfDocument document = new PdfDocument();
//Adding new page to PDF document.
PdfPage page = document.Pages.Add();
//Printing barcode to the PDF document.
barcode.Draw(page, new PointF(25,70));
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into stream.
document.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close(true);
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "EAN13Barcode.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Creates a new PdfEan13 Barcode.
PdfEan13Barcode barcode = new PdfEan13Barcode ();
//Set height of the barcode.
barcode.BarHeight = 50;
//Set the barcode text.
barcode.Text = "400638133393";
//Creating new PDF document.
PdfDocument document = new PdfDocument();
//Adding new page to PDF document.
PdfPage page = document.Pages.Add();
//Printing barcode to the PDF document.
barcode.Draw(page, new PointF(25,70));
//Saving the document.
document.Save("EAN13Barcode.pdf");
//Close the document.
document.Close(true);
'Creates a new PdfEan13 Barcode.
Dim barcode As New PdfEan13Barcode ()
'Set height of the barcode.
barcode.BarHeight = 50
'Set the barcode text
barcode.Text = "400638133393"
'Creating new PDF document.
Dim document As New PdfDocument()
'Adding new page to PDF document.
Dim page As PdfPage = document.Pages.Add()
'Printing barcode to the PDF document.
barcode.Draw(page, New PointF(25, 70))
'Saving the document.
document.Save("EAN13Barcode.pdf")
'Close the document.
document.Close(True)
You can download a complete working sample from GitHub.
The below code example shows how to add PdfEan8 barcode using the PdfEan8Barcode class to a PDF document.
//Creates a new PdfEan8 Barcode.
PdfEan8Barcode barcode = new PdfEan8Barcode ();
//Set height of the barcode.
barcode.BarHeight = 50;
//Set the barcode text.
barcode.Text = "1234567";
//Creating new PDF document.
PdfDocument document = new PdfDocument();
//Adding new page to PDF document.
PdfPage page = document.Pages.Add();
//Printing barcode to the PDF document.
barcode.Draw(page, new PointF(25,70));
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into stream.
document.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for PDF file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "EAN8Barcode.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Creates a new PdfEan8 Barcode.
PdfEan8Barcode barcode = new PdfEan8Barcode ();
//Set height of the barcode.
barcode.BarHeight = 50;
//Set the barcode text
barcode.Text = "1234567";
//Creating new PDF document.
PdfDocument document = new PdfDocument();
//Adding new page to PDF document.
PdfPage page = document.Pages.Add();
//Printing barcode to the PDF document.
barcode.Draw(page, new PointF(25,70));
//Saving the document.
document.Save("EAN8Barcode.pdf");
//Close the document
document.Close(true);
'Creates a new PdfEan8 Barcode.
Dim barcode As New PdfEan8Barcode ()
'Set height of the barcode.
barcode.BarHeight = 50
'Set the barcode text.
barcode.Text = "1234567"
'Creating new PDF document.
Dim document As New PdfDocument()
'Adding new page to PDF document.
Dim page As PdfPage = document.Pages.Add()
'Printing barcode to the PDF document.
barcode.Draw(page, New PointF(25, 70))
'Saving 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 below code snippet shows how to add a QR code using PdfQRBarcode class to the PDF document.
//Drawing QR Barcode
PdfQRBarcode barcode = new PdfQRBarcode();
//Set Error Correction Level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
//Set XDimension
barcode.XDimension = 3;
barcode.Text = "http://www.syncfusion.com";
barcode.Text = "http://www.syncfusion.com";
//Creating new PDF Document
PdfDocument doc = new PdfDocument();
//Adding new page to PDF document
PdfPage page = doc.Pages.Add();
//Printing barcode to the PDF
barcode.Draw(page, new PointF(25, 70));
//Save the document into stream
MemoryStream stream = new MemoryStream();
doc.Save(stream);
stream.Position = 0;
//Close the documents
doc.Close(true);
//Defining the ContentType for PDF file
string contentType = "application/pdf";
//Define the file name.
string fileName = " QRBarcode.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Drawing QR Barcode
PdfQRBarcode barcode = new PdfQRBarcode();
//Set Error Correction Level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
//Set XDimension
barcode.XDimension = 3;
barcode.Text = "http://www.syncfusion.com";
//Creating new PDF Document
PdfDocument doc = new PdfDocument();
//Adding new page to PDF document
PdfPage page = doc.Pages.Add();
//Printing barcode to the PDF document
barcode.Draw(page, new PointF(25, 70));
//Saving the document
doc.Save("QRBarcode.pdf");
//Close the document
doc.Close(true);
'Drawing QR Barcode
Dim barcode As New PdfQRBarcode()
'Set Error Correction Level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High
'Set XDimension
barcode.XDimension = 3
barcode.Text = "http://www.syncfusion.com"
'Creating new PDF Document
Dim doc As New PdfDocument()
'Adding new page to PDF document
Dim page As PdfPage = doc.Pages.Add()
'Printing barcode to the PDF document
barcode.Draw(page, New PointF(25, 70))
'Saving the document
doc.Save("QRBarcode.pdf")
'Close the document
doc.Close(true)
You can download a complete working sample from GitHub.
The below code example shows how to add a Pdf417 barcode code using Pdf417Barcode class to the PDF document.
//Creates a new Pdf417 Barcode.
Pdf417Barcode barcode = new Pdf417Barcode();
//Set error correction level.
barcode.ErrorCorrectionLevel= Pdf417ErrorCorrectionLevel.Auto;
//Set XDimension.
barcode.XDimension = 2;
//Set the barcode text.
barcode.Text = "http://www.syncfusion.com";
//Creating new PDF document.
PdfDocument document = new PdfDocument();
//Adding new page to PDF document.
PdfPage page = document.Pages.Add();
//Printing barcode on to the PDF.
barcode.Draw(page, new PointF(25,70));
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into stream.
document.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close(true);
//Defining the ContentType for PDF file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "417Barcode.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Creates a new Pdf417 Barcode.
Pdf417Barcode barcode = new Pdf417Barcode();
//Set error correction level.
barcode.ErrorCorrectionLevel= Pdf417ErrorCorrectionLevel.Auto;
//Set XDimension
barcode.XDimension = 2;
//Set the barcode text.
barcode.Text = "http://www.syncfusion.com";
//Creating new PDF document.
PdfDocument document = new PdfDocument();
//Adding new page to PDF document.
PdfPage page = document.Pages.Add();
//Printing barcode on to the PDF.
barcode.Draw(page, new PointF(25,70));
//Saving the document.
document.Save("417Barcode.pdf");
//Close the document.
document.Close(true);
'Creates a new Pdf417 Barcode.
Dim barcode As New Pdf417Barcode()
'Set error correction level.
barcode.ErrorCorrectionLevel = Pdf417ErrorCorrectionLevel.Auto
'Set XDimension.
barcode.XDimension = 2
'Set the barcode text.
barcode.Text = "http://www.syncfusion.com"
'Creating new PDF document.
Dim document As New PdfDocument()
'Adding new page to PDF document.
Dim page As PdfPage = document.Pages.Add()
'Printing barcode on to the PDF.
barcode.Draw(page, New PointF(25, 70))
'Saving the document.
document.Save("417Barcode.pdf")
'Close the document.
document.Close(True)
You can download a complete working sample from GitHub.
Set location and size to the barcode
The following code snippets show how to set Size and Location for Codabar barcode using PdfCodabarBarcode class to a PDF document.
//Creating new PDF document
PdfDocument doc = new PdfDocument();
//Adding new page to PDF document
PdfPage page = doc.Pages.Add();
//Create new instance for Codabar barcode.
PdfCodabarBarcode barcode = new PdfCodabarBarcode();
//Setting location of the barcode.
barcode.Location = new PointF(100, 100);
//Setting size of the barcode.
barcode.Size = new SizeF(200, 100);
barcode.Text = "123456789$";
//Printing barcode on to the PDF.
barcode.Draw(page);
//Save the document into stream.
MemoryStream stream = new MemoryStream();
doc.Save(stream);
stream.Position = 0;
//Close the documents.
doc.Close(true);
//Defining the ContentType for PDF file.
string contentType = "application/pdf";
//Define the file name.
string fileName = " CODABAR.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Creating new PDF document.
PdfDocument doc = new PdfDocument();
//Adding new page to PDF document.
PdfPage page = doc.Pages.Add();
//Create new instance for Codabar barcode.
PdfCodabarBarcode barcode = new PdfCodabarBarcode();
//Setting location of the barcode.
barcode.Location = new PointF(100, 100);
//Setting size of the barcode.
barcode.Size = new SizeF(200, 100);
barcode.Text = "123456789$";
//Printing barcode on to the PDF.
barcode.Draw(page);
//Save and close the document.
doc.Save("CODABAR.pdf");
doc.Close(true);
'Creating new PDF document.
Dim doc As New PdfDocument()
'Adding new page to PDF document.
Dim page As PdfPage = doc.Pages.Add()
'Create new instance for Codabar barcode.
Dim barcode As PdfCodabarBarcode = New PdfCodabarBarcode()
'Setting location of the barcode.
barcode.Location = New PointF(100, 100)
'Setting size of the barcode.
barcode.Size = New SizeF(200, 100)
barcode.Text = "123456789$"
'Printing barcode on to the PDF.
barcode.Draw(page)
'Save and close the document.
doc.Save("CODABAR.pdf")
doc.Close(True)
You can download a complete working sample from GitHub.
Adding a barcode to the PDF document without displaying the barcode text
The following code example shows how to add a barcode to the PDF document without displaying the barcode text by specifying the TextDisplayLocation as None through the TextLocation Enum.
//Creating new PDF document
PdfDocument doc = new PdfDocument();
//Adding a new page to the PDF document
PdfPage page = doc.Pages.Add();
//Create a new instance for the Codabar 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;
//Printing barcode on to the Pdf
barcode.Draw(page);
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document into stream
doc.Save(stream);
//If the position is not set to '0' then the PDF will be empty
stream.Position = 0;
//Close the document
doc.Close(true);
//Defining the ContentType for pdf file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Creating a new PDF document
PdfDocument doc = new PdfDocument();
//Adding a new page to the PDF document
PdfPage page = doc.Pages.Add();
//Create a new instance for the Codabar 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;
//Printing barcode on to the PDF
barcode.Draw(page);
//Save the PDF document
doc.Save("Output.pdf");
//Close the PDF document
doc.Close(true);
'Creating a new PDF document
Dim doc As PdfDocument = New PdfDocument()
'Adding a new page to the PDF document
Dim page As PdfPage = doc.Pages.Add()
'Create a new instance for the Codabar barcode
Dim barcode As PdfCode39Barcode = 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
'Printing barcode on to the PDF
barcode.Draw(page)
'Save the PDF document
doc.Save("Output.pdf")
'Close the PDF document
doc.Close(True)
You can download a complete working sample from GitHub.
Export Barcode as 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 barcodes to image using the ToImage() method of PdfUnidimensionalBarcode instance.
NOTE
To export barcode as image in .NET Core, the following assembly should be referenced in your application Syncfusion.Pdf.Imaging.Portable .
The below code example shows how to convert one dimensional barcode to
//Initialize a new PdfCode39Barcode instance
PdfCode39Barcode barcode = new PdfCode39Barcode();
//Set the height and text for barcode
barcode.BarHeight = 45;
barcode.Text = "CODE39$";
//Convert the barcode to image
Stream imageStream = barcode.ToImage(new SizeF(300, 300));
//Create file stream to save the image file
using (FileStream outputFileStream = new FileStream(@"Image.png", FileMode.Create, FileAccess.ReadWrite))
{
//Save the image to disk
imageStream.CopyTo(outputFileStream);
}
//Initialize a new PdfCode39Barcode instance
PdfCode39Barcode barcode = new PdfCode39Barcode();
//Set the height and text for barcode
barcode.BarHeight = 45;
barcode.Text = "CODE39$";
//Convert the barcode to image
Image barcodeImage = barcode.ToImage(new SizeF(300, 200));
//Save the image
barcodeImage.Save("Image.png", ImageFormat.Png);
'Initialize a new PdfCode39Barcode instance
Dim barcode As PdfCode39Barcode = New PdfCode39Barcode
'Set the height and text for barcode
barcode.BarHeight = 45
barcode.Text = "CODE39$"
'Convert the barcode to 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 supports converting two-dimensional barcodes such as QR Code and Data Matrix barcode to image. The following code snippet explains how to convert a QR code to image using the ToImage method of PdfQRBarcode instance.
//Initialize a new PdfQRBarcode instance
PdfQRBarcode barcode = new PdfQRBarcode();
//Set the XDimension and text for barcode
barcode.XDimension = 3;
barcode.Text = "http://www.google.com";
//Convert the barcode to image
Stream imageStream = barcode.ToImage(new Syncfusion.Drawing.SizeF(300, 300));
//Create file stream to save the image file
using (FileStream outputFileStream = new FileStream("Output.png", FileMode.Create, FileAccess.ReadWrite))
{
//Save the image to disk
imageStream.CopyTo(outputFileStream);
}
//Initialize a new PdfQRBarcode instance
PdfQRBarcode barcode = new PdfQRBarcode();
//Set the XDimension and text for barcode
barcode.XDimension = 3;
barcode.Text = "http://www.google.com";
//Convert the barcode to image
Image barcodeImage = barcode.ToImage(new SizeF(300, 300));
//Save the image
barcodeImage.Save("Image.png", ImageFormat.Png);
'Initialize a new PdfQRBarcode instance
Dim barcode As PdfQRBarcode = New PdfQRBarcode
'Set the XDimension and text for barcode
barcode.XDimension = 3
barcode.Text = "http://www.google.com"
'Convert the barcode to image
Dim barcodeImage As Image = barcode.ToImage(New SizeF(300, 300))
'Save the image
barcodeImage.Save("Image.jpg", ImageFormat.Png)
You can download a complete working sample from GitHub.
Customizing the barcode appearance
The height of the barcode can be changed using the BarHeight property. The equivalent property to change the block size for two dimensional barcode is XDimension. You can also customize the barcode color by changing the DarkBarColor and LightBarColor properties.
NOTE
This color customization is possible only for one dimensional barcodes and it is not supported for two dimensional barcodes.
The following code sample explains how to customize the one-dimensional barcode such as Code 39 using the BarHeight, BarColor, etc. in PdfCode93Barcode class.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Creates a new page.
PdfPage page = document.Pages.Add();
//Creates a new PdfCode93Barcode.
PdfCode93Barcode code93 = new PdfCode93Barcode("CODE93");
//Set the barcode height.
code93.BarHeight = 40;
//Set the barcode color.
code93.BarColor = Color.Blue;
//Draws a barcode on the new Page.
code93.Draw(page, new PointF(25, 500));
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document into stream
document.Save(stream);
//If the position is not set to '0' then the PDF will be empty
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for pdf file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Creates a new page.
PdfPage page = document.Pages.Add();
//Creates a new PdfCode93Barcode.
PdfCode93Barcode code93 = new PdfCode93Barcode("CODE93");
//Set the barcode height.
code93.BarHeight = 40;
//Set the barcode color.
code93.BarColor = Color.Blue;
//Draws a barcode on the new Page.
code93.Draw(page, new PointF(25, 500));
//Save document to disk.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument()
'Creates a new page.
Dim page As PdfPage = document.Pages.Add()
'Creates a new PdfCode93Barcode.
Dim code93 As PdfCode93Barcode = New PdfCode93Barcode("CODE93")
'Set the barcode height.
code93.BarHeight = 40
'Set the barcode color.
code93.BarColor = Color.Blue
'Draws a barcode on the new Page.
code93.Draw(page, New PointF(25, 500))
'Save document to disk.
document.Save("Output.pdf")
'Close the document.
document.Close(True)
The following code example shows how to customize the two-dimensional barcode such as QR code using the XDimension property in PdfBidimensionalBarcode class.
//Drawing QR Barcode
PdfQRBarcode barcode = new PdfQRBarcode();
//Set Error Correction Level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
//Set XDimension
barcode.XDimension = 3;
barcode.Text = "http://www.syncfusion.com";
//Set the background color
barcode.BackColor = Color.Green;
//Creating new PDF document
PdfDocument document = new PdfDocument();
//Adding new page to PDF document
PdfPage page = document.Pages.Add();
//Printing barcode on to the PDF.
barcode.Draw(page, new PointF(25, 70));
//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the documents.
document.Close(true);
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = " CODE39.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Drawing QR Barcode
PdfQRBarcode barcode = new PdfQRBarcode();
//Set Error Correction Level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
//Set XDimension
barcode.XDimension = 3;
barcode.Text = "http://www.syncfusion.com";
//Set the background color
barcode.BackColor = Color.Green;
//Creating new PDF document
PdfDocument document = new PdfDocument();
//Adding new page to PDF document
PdfPage page = document.Pages.Add();
//Printing barcode on to the PDF.
barcode.Draw(page, new PointF(25, 70));
//Saving the document
document.Save("QRBarcode.pdf");
//Close the document
document.Close(true);
'Drawing QR Barcode
Dim barcode As PdfQRBarcode = New PdfQRBarcode()
'Set Error Correction Level
barcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High
'Set XDimension
barcode.XDimension = 3
barcode.Text = "http://www.syncfusion.com"
'Set the background color
barcode.BackColor = Color.Green
'Creating new PDF Document
Dim document As PdfDocument = New PdfDocument()
'Adding new page to PDF document
Dim page As PdfPage = document.Pages.Add()
'Printing barcode on to the PDF.
barcode.Draw(page, New PointF(25, 70))
'Saving the Document
document.Save("QRBarcode.pdf")
'Close the document
document.Close(True)
Create QR code with logo
The QRCodeLogo
class serves as a representation of a logo image that can be utilized in a QR code, and the Logo
property is employed to set the logo image in the QR barcode. By leveraging these functionalities, users gain the capability to generate QR codes that seamlessly 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 the center of it.
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Creates a new page.
PdfPage page = document.Pages.Add();
//Creates a new PDF QR barcode.
PdfQRBarcode qrBarcode = new PdfQRBarcode();
//Set the barcode text.
qrBarcode.Text = "https://www.syncfusion.com/";
//Set the logo image to QR barcode.
FileStream imageStream = new FileStream("logo.png", FileMode.Open, FileAccess.Read);
//Create QR barcode logo.
QRCodeLogo qRCodeLogo = new QRCodeLogo(imageStream);
//Set the QR barcode logo.
qrBarcode.Logo = qRCodeLogo;
//Set the dimention of the barcode.
qrBarcode.XDimension = 5;
//Draw the barcode to PDF page.
qrBarcode.Draw(page);
//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document into stream
document.Save(stream);
//Close the document.
document.Close(true);
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Creates a new page.
PdfPage page = document.Pages.Add();
//Creates a new PDF QR barcode.
PdfQRBarcode qrBarcode = new PdfQRBarcode();
//Set the barcode text.
qrBarcode.Text = "https://www.syncfusion.com/";
//Create QR barcode logo.
QRCodeLogo qRCodeLogo = new QRCodeLogo("logo.png");
//Set the QR barcode logo.
qrBarcode.Logo = qRCodeLogo;
//Set the dimention of the barcode.
qrBarcode.XDimension = 5;
//Draw the barcode to PDF page.
qrBarcode.Draw(page);
//Save the document.
document.Save("Output.pdf");
//Close the document.
document.Close(true);
'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument()
'Creates a new page.
Dim page As PdfPage = document.Pages.Add()
'Creates a new PDF QR barcode.
Dim qrBarcode As PdfQRBarcode = New PdfQRBarcode()
'Set the barcode text.
qrBarcode.Text = "https://www.syncfusion.com/"
'Create QR barcode logo.
Dim qRCodeLogo As QRCodeLogo = New QRCodeLogo("logo.png")
'Set the QR barcode logo.
qrBarcode.Logo = qRCodeLogo
'Set the dimention of the barcode.
qrBarcode.XDimension = 5
'Draw the barcode to PDF page.
qrBarcode.Draw(page)
'Save the document.
document.Save("Output.pdf")
'Close the document.
document.Close(True)
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 |
|
|
[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 with one code) |
variable |
|
[0-9]; [A-Z]; [a-z]; Mixed; Punctuation; Byte |
variable |
|
[0-9]; |
7 or 8 |
|
[0-9]; |
12 or 13 |