Articles in this section
Category / Section

How to add or draw barcode in a PDF using C# and VB.NET?

3 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add or draw barcode in PDF document using C# and VB.NET.

Steps to add or draw barcode in PDF programmatically:

  1. Create a new C# console application project. Create a console application project

Create new console application
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet reference

Install nuget packages
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Barcode;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

 

VB.NET

Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Barcode
Imports Syncfusion.Pdf.Graphics

 

  1. Use the following code snippet to add or draw barcode in PDF document.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Adding new page to PDF document
PdfPage page = document.Pages.Add();
 
#region Code39
//Drawing Code39 barcode 
PdfCode39Barcode barcode = new PdfCode39Barcode();
//Set height of the barcode 
barcode.BarHeight = 45;
//Setting text of the barcode 
barcode.Text = "CODE39$";
//Draw string
page.Graphics.DrawString("Code39", new PdfStandardFont(PdfFontFamily.Helvetica, 10,PdfFontStyle.Bold), PdfBrushes.Black, new PointF(20,30));
//Printing barcode on to the PDF
barcode.Draw(page, new PointF(50, 70));
#endregion
 
#region QRBarcode
//Drawing QR barcode 
PdfQRBarcode qrBarcode = new PdfQRBarcode();
//Set Error Correction Level
qrBarcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
//Set XDimension
qrBarcode.XDimension = 3;
qrBarcode.Text = "http://www.syncfusion.com";
//Draw string
page.Graphics.DrawString("QR Barcode", new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold), PdfBrushes.Black, new PointF(20, 180));
//Printing barcode on to the PDF
qrBarcode.Draw(page, new PointF(50, 200));
#endregion
 
//Save the document
document.Save("Barcode.pdf");
//Close the document
document.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("Barcode.pdf");

 

VB.NET

'Create a new PDF document
Dim document As New PdfDocument()
'Adding new page to PDF document
Dim page As PdfPage = document.Pages.Add()
 
#Region "Code39"
'Drawing Code39 barcode 
Dim barcode As New PdfCode39Barcode()
'Set height of the barcode 
barcode.BarHeight = 45
'Setting text of the barcode 
barcode.Text = "CODE39$"
'Draw string
page.Graphics.DrawString("Code39", New PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold), PdfBrushes.Black, New PointF(20, 30))
'Printing barcode on to the PDF
barcode.Draw(page, New PointF(50, 70))
#End Region
 
#Region "QR Barcode"
'Drawing QR barcode 
Dim qrBarcode As New PdfQRBarcode()
'Set Error Correction Level
qrBarcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High
'Set XDimension
qrBarcode.XDimension = 3
qrBarcode.Text = "http://www.syncfusion.com"
'Draw string
page.Graphics.DrawString("QR Barcode", New PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold), PdfBrushes.Black, New PointF(20, 180))
'Printing barcode on to the PDF
qrBarcode.Draw(page, New PointF(50, 200))
#End Region
 
'Save the document
document.Save("Barcode.pdf")
'Close the document
document.Close(True)
'This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("Barcode.pdf")

 

A complete working sample can be downloaded from BarcodeSample.zip.

By executing the program, you will get the PDF document as follows. Screenshot of output PDF file

Take a moment to peruse the documentation, where you can find the options like adding one dimensional and two dimensional barcodes to the PDF document, set location and size to the barcode, and customizing the barcode appearance.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to creation of barcode in PDF document.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.


Conclusion

I hope you enjoyed learning about how to add or draw QR Code(barcode) in a PDF using C# and VB.NET. 

You can refer to our .NET PDF Framework’s feature tour page to know about its other groundbreaking feature representations and documentation to know about how to quickly getting started for configuration specifications. You can also explore our PDF example to understand how to create and manipulate data in .NET PDF. 

For current customers, you can check out our Document processing libraries from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our JavaScript Context Menu and other JavaScript controls.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied