Link Search Menu Expand Document

Print Labels - VB.NET

BarCode SDK sample in VB.NET demonstrating ‘Print Labels’

Form1.Designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
		Me.buttonPrint = New System.Windows.Forms.Button()
		Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog()
		Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument()
		Me.PrintDialog1 = New System.Windows.Forms.PrintDialog()
		Me.SuspendLayout()
		'
		'buttonPrint
		'
		Me.buttonPrint.Location = New System.Drawing.Point(12, 12)
		Me.buttonPrint.Name = "buttonPrint"
		Me.buttonPrint.Size = New System.Drawing.Size(260, 57)
		Me.buttonPrint.TabIndex = 1
		Me.buttonPrint.Text = "Draw And Print Cards"
		Me.buttonPrint.UseVisualStyleBackColor = True
		'
		'PrintPreviewDialog1
		'
		Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
		Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
		Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
		Me.PrintPreviewDialog1.Document = Me.PrintDocument1
		Me.PrintPreviewDialog1.Enabled = True
		Me.PrintPreviewDialog1.Icon = CType(resources.GetObject("PrintPreviewDialog1.Icon"), System.Drawing.Icon)
		Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
		Me.PrintPreviewDialog1.Visible = False
		'
		'PrintDocument1
		'
		'
		'PrintDialog1
		'
		Me.PrintDialog1.Document = Me.PrintDocument1
		Me.PrintDialog1.UseEXDialog = True
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 149)
		Me.Controls.Add(Me.buttonPrint)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub
	Private WithEvents buttonPrint As System.Windows.Forms.Button
	Friend WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
	Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
	Friend WithEvents PrintDialog1 As System.Windows.Forms.PrintDialog

End Class

Form1.vb
Imports System.Drawing.Drawing2D
Imports Bytescout.BarCode

Public Class Form1

	Dim PaperSize As SizeF = New SizeF(5.5F, 8.75F)	' 5.5 x 8.75 inches
	Const PrintingResolution As Integer = 300 ' 300 dots per inch

	Public Sub New()

		' This call is required by the designer.
		InitializeComponent()

		' Make the print preview dialog larger by default
		PrintPreviewDialog1.MinimumSize = New Size(800, 600)

	End Sub

	Private Sub buttonPrint_Click(sender As Object, e As EventArgs) Handles buttonPrint.Click

		' Show print setup dialog, then print preview
		If PrintDialog1.ShowDialog() = DialogResult.OK Then
			PrintPreviewDialog1.ShowDialog()
		End If

	End Sub

	Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

		' Draw page on the printer device context
		Dim pageBitmap = DrawPage()
		e.Graphics.DrawImage(pageBitmap, 0, 0)

	End Sub

	' Draw cards on a bitmap of custom size
	Private Function DrawPage() As Bitmap

		Dim cardSize As SizeF = New SizeF(PaperSize.Width / 4, PaperSize.Height / 4) ' 4 x 4 cards on a page

		' Prepare constant and variable labels
		Dim strBrand = "CJ SHOES"
		Dim strModel = "ARTHUR-1N"
		Dim strColor = "BLK"
		Dim shoeSizeStart As Single = 5.5F
		Dim shoeSizeStep As Single = 0.5F
		Dim barcodeStartValue As Long = 4611030000
		Dim barcodeValueStep As Integer = 1

		' Prepare fonts
		Dim font1 = New Font("Arial", 0.12F, GraphicsUnit.Inch)
		Dim font2 = New Font("Arial", 0.1F, GraphicsUnit.Inch)
		Dim font3 = New Font("Arial", 0.09F, GraphicsUnit.Inch)
		Dim font4 = New Font("Arial", 0.15F, GraphicsUnit.Inch)

		' Prepare barcode generator
		Dim barcode = New Barcode("demo", "demo")
		barcode.Symbology = SymbologyType.I2of5
		barcode.NarrowBarWidth = 2
		barcode.DrawCaption = False

		Dim cardIndex = 0

		' Create bitmap for the page
		Dim pageBitmap = New Bitmap(CType((PaperSize.Width * PrintingResolution), Integer), CType((PaperSize.Height * PrintingResolution), Integer))
		pageBitmap.SetResolution(PrintingResolution, PrintingResolution)

		Using pageCanvas As Graphics = Graphics.FromImage(pageBitmap)

			pageCanvas.InterpolationMode = InterpolationMode.HighQualityBicubic
			pageCanvas.CompositingQuality = CompositingQuality.HighQuality

			' Setup page units to inches
			pageCanvas.PageUnit = GraphicsUnit.Inch
			' Fill background with white color
			pageCanvas.Clear(Color.White)

			' Draw cards
			For row As Integer = 0 To 3
				For column As Integer = 0 To 3

					' Create bitmap for card
					Dim cardBitmap = New Bitmap(CType((cardSize.Width * PrintingResolution), Integer), CType((cardSize.Height * PrintingResolution), Integer))
					cardBitmap.SetResolution(PrintingResolution, PrintingResolution)

					Using cardCanvas As Graphics = Graphics.FromImage(cardBitmap)

						' Setup page units to inches
						cardCanvas.PageUnit = GraphicsUnit.Inch

						' Setup drawing quality
						cardCanvas.SmoothingMode = SmoothingMode.HighQuality
						cardCanvas.InterpolationMode = InterpolationMode.HighQualityBicubic
						cardCanvas.CompositingQuality = CompositingQuality.HighQuality

						Dim stringFormat = New StringFormat()
						stringFormat.Alignment = StringAlignment.Center

						' Draw static labels
						cardCanvas.DrawString(strBrand, font1, Brushes.Black, cardSize.Width / 2, 0.1F, stringFormat)
						cardCanvas.DrawString(strModel, font2, Brushes.Black, cardSize.Width / 2, 0.4F, stringFormat)
						cardCanvas.DrawString(strColor, font1, Brushes.Black, cardSize.Width / 2, 0.7F, stringFormat)

						' Generate barcode image
						barcode.Value = (barcodeStartValue + cardIndex * barcodeValueStep).ToString()
						barcode.PreserveMinReadableSize = False
						barcode.ResolutionX = PrintingResolution
						barcode.ResolutionY = PrintingResolution
						barcode.FitInto(cardSize.Width, 0.5F, UnitOfMeasure.Inch)
						Dim barcodeImage = barcode.GetImage()
						' Draw barcode
						cardCanvas.DrawImage(barcodeImage, 0, 1.0F)
						' Draw barcode label
						cardCanvas.DrawString(barcode.Value, font3, Brushes.Black, cardSize.Width / 2, 1.4F, stringFormat)

						' Draw shoe size label
						cardCanvas.DrawString((shoeSizeStart + cardIndex * shoeSizeStep).ToString(), font4, Brushes.Black, _
							cardSize.Width / 2, 1.7F, stringFormat)

					End Using

					' Draw card on the page
					pageCanvas.DrawImage(cardBitmap, column * cardSize.Width, row * cardSize.Height)

					cardIndex = cardIndex + 1
				Next
			Next

		End Using

		Return pageBitmap

	End Function

End Class

Resources.Designer.vb
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:4.0.30319.42000
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On


Namespace My.Resources
    
    'This class was auto-generated by the StronglyTypedResourceBuilder
    'class via a tool like ResGen or Visual Studio.
    'To add or remove a member, edit your .ResX file then rerun ResGen
    'with the /str option, or rebuild your VS project.
    '''<summary>
    '''  A strongly-typed resource class, for looking up localized strings, etc.
    '''</summary>
    <Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0"), _
     Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
     Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute(), _
     Global.Microsoft.VisualBasic.HideModuleNameAttribute()> _
    Friend Module Resources

        Private resourceMan As Global.System.Resources.ResourceManager

        Private resourceCulture As Global.System.Globalization.CultureInfo

        '''<summary>
        '''  Returns the cached ResourceManager instance used by this class.
        '''</summary>
        <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
        Friend ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager
            Get
                If Object.ReferenceEquals(resourceMan, Nothing) Then
                    Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("PrintLabels.Resources", GetType(Resources).Assembly)
                    resourceMan = temp
                End If
                Return resourceMan
            End Get
        End Property

        '''<summary>
        '''  Overrides the current thread's CurrentUICulture property for all
        '''  resource lookups using this strongly typed resource class.
        '''</summary>
        <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
        Friend Property Culture() As Global.System.Globalization.CultureInfo
            Get
                Return resourceCulture
            End Get
            Set(ByVal value As Global.System.Globalization.CultureInfo)
                resourceCulture = value
            End Set
        End Property
    End Module
End Namespace

Settings.Designer.vb
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:4.0.30319.42000
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On


Namespace My

    <Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute(), _
     Global.System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0"), _
     Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
    Partial Friend NotInheritable Class MySettings
        Inherits Global.System.Configuration.ApplicationSettingsBase

        Private Shared defaultInstance As MySettings = CType(Global.System.Configuration.ApplicationSettingsBase.Synchronized(New MySettings), MySettings)

#Region "My.Settings Auto-Save Functionality"
#If _MyType = "WindowsForms" Then
        Private Shared addedHandler As Boolean

        Private Shared addedHandlerLockObject As New Object

        <Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _
        Private Shared Sub AutoSaveSettings(ByVal sender As Global.System.Object, ByVal e As Global.System.EventArgs)
            If My.Application.SaveMySettingsOnExit Then
                My.Settings.Save()
            End If
        End Sub
#End If
#End Region

        Public Shared ReadOnly Property [Default]() As MySettings
            Get

#If _MyType = "WindowsForms" Then
                   If Not addedHandler Then
                        SyncLock addedHandlerLockObject
                            If Not addedHandler Then
                                AddHandler My.Application.Shutdown, AddressOf AutoSaveSettings
                                addedHandler = True
                            End If
                        End SyncLock
                    End If
#End If
                Return defaultInstance
            End Get
        End Property
    End Class
End Namespace

Namespace My
    
    <Global.Microsoft.VisualBasic.HideModuleNameAttribute(),  _
     Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
     Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute()>  _
    Friend Module MySettingsProperty
        
        <Global.System.ComponentModel.Design.HelpKeywordAttribute("My.Settings")>  _
        Friend ReadOnly Property Settings() As Global.PrintLabels.My.MySettings
            Get
                Return Global.PrintLabels.My.MySettings.Default
            End Get
        End Property
    End Module
End Namespace

Download Source Code (.zip)

Return to the previous page Explore BarCode SDK


Copyright © 2016 - 2021 ByteScout