How to print barcode on a printer using C# and VB.NET
The Syncfusion Essential Barcode control is used to create various types of barcodes. Using this control, you can print barcode on a printer using C# and VB.NET.
Steps to print barcode on a printer programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.SfBarcode.Windows NuGet package as reference to your .NET Framework application from NuGet.org.
- The SfBarcode control can be added to the application by dragging it from toolbox and dropping in designer. The required assembly references will be added automatically.
- Use the following code snippet to load and print barcode on a printer.
C#
public Form1()
{
InitializeComponent();
//Set text to the encode
this.sfBarcode1.Text = "Syncfusion Essential Studio® Enterprise edition $995";
}
private void button1_Click(object sender, EventArgs e)
{
//Create a PrintDocument object
PrintDocument pd = new PrintDocument();
//Add PrintPage event handler
pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
//Call Print Method
pd.Print();
}
private void PrintTextFileHandler(object sender, PrintPageEventArgs eventArgs)
{
int size = eventArgs.MarginBounds.Width;
//Create a graphics with the size of the printer page's width
Bitmap imageToPr = new Bitmap(size, size);
Graphics graphics = Graphics.FromImage(imageToPr);
graphics.DrawImage(sfBarcode1.ToImage(sfBarcode1.Size), new Rectangle(0, 0, size, size));
//Draw barcode image into the printer graphics
eventArgs.Graphics.DrawImage(imageToPr, new Rectangle((int)eventArgs.MarginBounds.Left, (int)eventArgs.MarginBounds.Top, imageToPr.Width, imageToPr.Height), 0, 0, size, size, GraphicsUnit.Pixel);
}
VB.NET
Sub New()
InitializeComponent()
sfBarcode1.Text = "http://www.google.com"
End Sub
Sub button1_Click(sender As Object, eventArgs As EventArgs)
'Create a PrintDocument object
Dim pd As New PrintDocument()
'Add PrintPage event handler
AddHandler pd.PrintPage, New PrintPageEventHandler(AddressOf PrintTextFileHandler)
'Call Print Method
pd.Print()
End Sub
Private Sub PrintTextFileHandler(sender As Object, eventArgs As PrintPageEventArgs)
Dim size As Integer = eventArgs.MarginBounds.Width
'Create a graphics with the size of the printer page's width
Dim imageToPr As New Bitmap(size, size)
Dim graphics__1 As Graphics = Graphics.FromImage(imageToPr)
graphics__1.DrawImage(sfBarcode1.ToImage(sfBarcode1.Size), New Rectangle(0, 0, size, size))
Dim rect As RectangleF = New Rectangle(CInt(eventArgs.MarginBounds.Left), CInt(eventArgs.MarginBounds.Top), imageToPr.Width, imageToPr.Height)
'Draw barcode image into the printer graphics
eventArgs.Graphics.DrawImage(imageToPr, rect, 0, 0, size, size,
GraphicsUnit.Pixel)
End Sub
A complete working sample can be downloaded from PrintBarcodeOnPrinter.zip.
Take a moment to peruse the documentation for Barcode, where you will find other options like creating one and two dimensional barcodes.
Refer here to explore the Syncfusion Essential Barcode control for Windows Forms.
An online sample link to create one and two dimensional barcode.
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.
I'm using visual studio 2010... Can u give me a idea to print barcode form with multiple entries?