How to print barcode in .NET applications using Bytescout BarCode SDK for .NET

  • Home
  • /
  • Articles
  • /
  • How to print barcode in .NET applications using Bytescout BarCode SDK for .NET

Bytescout BarCode SDK is capable of generating printable images (PNG, JPG, BMP) and scalable EMF metafile images. You use built-in .NET printing functionality to print barcodes in .NET applications (Visual Basic and C#)

Printing in Visual Basic .NET:

BarCodePrinter.vb:

Imports System
Imports System.Collections.Generic
Imports System.Text

Imports Bytescout.BarCode
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.Drawing

Public Class BarcodePrinter
Private m_barcode As New Barcode()
Private m_widthInches As Single
Private m_heightInches As Single

Public Sub Print(ByVal type As SymbologyType, ByVal value As String, ByVal caption As String, ByVal widthInches As Single, ByVal heightInches As Single)
m_barcode.Symbology = type
m_barcode.Value = value
m_barcode.CustomCaption = caption

m_widthInches = widthInches
m_heightInches = heightInches

Dim printDoc As New PrintDocument()
AddHandler printDoc.PrintPage, New PrintPageEventHandler(AddressOf printDoc_PrintPage)

Dim dlgSettings As New PrintDialog()
dlgSettings.Document = printDoc

If dlgSettings.ShowDialog() = DialogResult.OK Then
printDoc.Print()
End If
End Sub

Private Sub printDoc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim position As New Point(100, 100)

m_barcode.FitInto(New SizeF(m_widthInches, m_heightInches), GraphicsUnit.Inch)
m_barcode.Draw(e.Graphics, position)
End Sub
End Class

Module1.vb:

Visual Basic Copy Code

Imports Bytescout.BarCode

Module Module1

Sub Main()
Dim bPrinter As New BarcodePrinter()
bPrinter.Print(SymbologyType.Code39, “0123456789”, “Case Number”, 3.5F, 1.0F)
End Sub

End Module

Printing in Visual C# .NET:

BarCodePrinter.cs:

using System;
using System.Collections.Generic;
using System.Text;

using Bytescout.BarCode;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing;

namespace printBarcodeCSharp
{
class BarcodePrinter
{
private Barcode m_barcode = new Barcode();
private float m_widthInches;
private float m_heightInches;

public void Print(SymbologyType type, string value, string caption, float widthInches, float heightInches)
{
m_barcode.Symbology = type;
m_barcode.Value = value;
m_barcode.CustomCaption = caption;

m_widthInches = widthInches;
m_heightInches = heightInches;

PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = printDoc;

if (dlgSettings.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}

void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Point position = new Point(100, 100);

m_barcode.FitInto(new SizeF(m_widthInches, m_heightInches), GraphicsUnit.Inch);
m_barcode.Draw(e.Graphics, position);
}
}
}

Program.cs:

using System;
using Bytescout.BarCode;

namespace printBarcodeCSharp2008
{
class Program
{
static void Main(string[] args)
{
BarcodePrinter bPrinter = new BarcodePrinter();
bPrinter.Print(SymbologyType.Code39, “0123456789”, “Case Number”, 3.5f, 1f);
}
}
}

prev
next