How to print the barcode in Windows Forms?
Syncfusion WinForms Barcode control helps rendering barcodes in desktop application. The control can be merged with any desktop application and easy to encode text using the supported symbol types. The basic structure of a barcode consists of a leading and trailing quiet zone, a start pattern, one or more data characters, optionally one or two check characters, and a stop pattern. Using this control, you can print the barcode in Windows Forms.
Steps to print the barcode 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.
- Include the following namespaces in the Form1.Designer.cs file.
// [C# Code] using System.Drawing; using System.Drawing.Printing; using Syncfusion.Windows.Forms.Barcode; using System.Windows.Forms;
' [VB Code] Imports System.Drawing Imports System.Drawing.Printing Imports Syncfusion.Windows.Forms.Barcode Imports System.Windows.Forms
- Use the following code snippet to print the barcode.
// [C# Code] //Initialize the Barcode control SfBarcode barcode = new SfBarcode(); //Set the barcode symbology type barcode.Symbology = BarcodeSymbolType.Code128A; //Set the input text barcode.Text = textBox1.Text; //Set the barcode size barcode.Size = pictureBox1.Size; //Export the Barcode control as image pictureBox1.Image = barcode.ToImage(); bitmap = new Bitmap(barcode.ToImage()); //Initialize the print document PrintDocument printDocument = new PrintDocument(); PrintDialog pdialog = new PrintDialog(); if (pdialog.ShowDialog() == DialogResult.OK) { printDocument.PrinterSettings.PrinterName = pdialog.PrinterSettings.PrinterName; //Triggers to print current page printDocument.PrintPage += printDocument_PrintPage; printDocument.Print(); } // Event handler to save the PrintDocument page as image void printDocument_PrintPage(object sender, PrintPageEventArgs e) { //Draw barcode into PDF page e.Graphics.DrawImage(bitmap, 0, 0); e.HasMorePages = false; }
' [VB Code] 'Initialize the Barcode control Dim barcode As New SfBarcode() 'Set the barcode symbology type barcode.Symbology = BarcodeSymbolType.Code128A 'Set the input text barcode.Text = TextBox1.Text 'Set the barcode size barcode.Size = PictureBox1.Size 'Export the Barcode control as image PictureBox1.Image = barcode.ToImage() Bitmap = New Bitmap(barcode.ToImage()) 'Initialize the print document Dim printDocument As New PrintDocument() Dim pdialog As New PrintDialog() If pdialog.ShowDialog() = DialogResult.OK Then printDocument.PrinterSettings.PrinterName = pdialog.PrinterSettings.PrinterName 'Triggers to print current page AddHandler printDocument.PrintPage, AddressOf printDocument_PrintPage printDocument.Print() End If 'Event handler to save the PrintDocument page as image Private Sub printDocument_PrintPage(sender As Object, e As PrintPageEventArgs) 'Draw barcode into PDF page e.Graphics.DrawImage(Bitmap, 0, 0) e.HasMorePages = False End Sub
Download the complete work sample from BarcodeSample.zip.
Take a moment to peruse the documentation, where you can find barcode customization, symbology settings for 1D and 2D barcodes with code examples.
Refer here to explore the barcode control for Windows Forms.
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.