How to Add or Draw Barcode in WinForms PDF Using C# and VB.NET?
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:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- 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
- 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.
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.
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
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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!