How to print code 3 of 9 barcode to normal printer in win form application - vb.net

I want to print code 3 of 9 barcode from my vb.net aplication to printer.
i want to check wether this is printing to correct format or not.
i am calling this function in my print button click, how i can pass this value to my normal printer, i want to just print this A4 sheet
Dim PrinterName As String
Dim Printlabel As New MyPrinter
PrinterName = "Sharp AR-M318 PCL 6"
Dim k As String = info
Dim stchar As String = String.Empty
Dim addStar As String = "*"
Dim full As String = addStar & info & addStar
info = full
Dim bc As Drawing.Bitmap = New Drawing.Bitmap(1, 1)
'Dim myf As Font = New Font("Arial", 12, FontStyle.Regular) ', GraphicsUnit.Point)
Dim ft As Drawing.Font = New Drawing.Font("Free 3 of 9 Extended", 60, Drawing.FontStyle.Regular, Drawing.GraphicsUnit.Point)
Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(bc)
Dim infoSize As Drawing.SizeF = g.MeasureString(info, ft)
bc = New Drawing.Bitmap(bc, infoSize.ToSize)
g = Drawing.Graphics.FromImage(bc)
g.Clear(Drawing.Color.White)
g.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixel '
For Each chr As Char In info
stchar &= chr.ToString & " "
Next
g.DrawString(stchar, ft, New Drawing.SolidBrush(Drawing.Color.Black), 2, 3)
g.Flush()
ft.Dispose()
g.Dispose()
Return bc
Printlabel.prt(bc, PrinterName)

I recommend you using Zen Barcode. This is much easier to handle and you are not dependend that the font is installed on the PC where your software runs. Also you don't have to bother with checksums.
Here you will find the nuget-package: https://www.nuget.org/packages/Zen.Barcode.Rendering.Framework/

Related

VB.NET RichTextBox and TextBox Separation

For my company, we receive our time sheets in the following format
Name:Hours
Name:Hours
Name:Hours
I have the Timesheet.txt loading into the RichTextBox1 fine, but, I want when I click a button to load them into two different textboxes. I want the Names to load into TextBox1 and Hours to load into TextBox2
Then it deletes the line. It will go onto the next line when I click the button again.
Any help?
Simple, but will do:
Dim sInput As String = "Name:10"
Dim sSplitArray() As String = sInput.Split(New Char() {":"c})
Dim sName As String = sSplitArray(0)
Dim sHours As String = sSplitArray(1)
String's Split function splits a string up by the character you pass to it.
So if you have this:
sInput = "Name:10:Zebra:Kazaam"
When you split that string by the ":" separator, it will give you an array with:
Name
10
Zebra
Kazaam
Hope that this will give the complete solution for your problem
Dim x, y As Integer
Dim textbox1() As TextBox
x = 430
y = 265
Dim str As String
Dim result(), output(1) As String'str=RitchText1.text 'load input string to str
result = str.Split(System.Environment.NewLine) ' split into array based on new line
For i As Integer = 0 To result.Length - 1 'execute up to array limit
output = result(i).Split(":") 'split again based on :
textbox1(i).Text = output(0) 'Name part into dynamic textbox 1
textbox1(i).Location = New Point(x, y)
textbox1(i + 1).Text = output(1) 'Name part into dynamic textbox 2
textbox1(i + 1).Location = New Point(x + 40, y)
Me.Controls.Add(textbox1(i))
textbox1(i).Visible = True
Me.Controls.Add(textbox1(i + 1))
textbox1(i + 1).Visible = True
y = y + 20
Next

twain ocr function returns empty byte

i have the below code:
ddlResultFormat.Items.Add("Text")
dynamicDotNetTwain1.LoadImage("C:\Users\elj\Desktop\3b072e9b-76b4-4776-bd3c-5984074d8ecd.png")
Dim r = dynamicDotNetTwain1.GetImage(dynamicDotNetTwain1.CurrentImageIndexInBuffer).Width.ToString()
Dim l = dynamicDotNetTwain1.GetImage(dynamicDotNetTwain1.CurrentImageIndexInBuffer).Height.ToString()
Dim languageFolder As String
languageFolder = "C:\Program Files (x86)\Dynamsoft\Dynamic .NET TWAIN 5.2 Trial\Samples\Bin\"
Dim languages As New Dictionary(Of String, String)
languages.Add("English", "eng")
Me.dynamicDotNetTwain1.OCRTessDataPath = languageFolder
Me.dynamicDotNetTwain1.OCRLanguage = "English"
Dim ocrResultFormat As Dynamsoft.DotNet.TWAIN.OCR.ResultFormat
ocrResultFormat = CType(System.Enum.Parse(GetType(Dynamsoft.DotNet.TWAIN.OCR.ResultFormat), Val("&H" & 0)), Dynamsoft.DotNet.TWAIN.OCR.ResultFormat)
Me.dynamicDotNetTwain1.OCRResultFormat = ResultFormat.Text
Dim strDllPath As String
strDllPath = "C:\Program Files (x86)\Dynamsoft\Dynamic .NET TWAIN 5.2 Trial\Redistributable\OCRResources\"
dynamicDotNetTwain1.OCRDllPath = strDllPath
If (Me.dynamicDotNetTwain1.CurrentImageIndexInBuffer < 0) Then
Dim jajajja = "Please load an image before doing OCR!"
End If
Dim sbytes As Byte()
sbytes = Me.dynamicDotNetTwain1.OCR(Me.dynamicDotNetTwain1.CurrentSelectedImageIndicesInBuffer)
the problem that i'm facing is that the sbytes byte array is being always set to empty although the indice in the buffer is correct and the paths and the dll are well implemented but the convert to the byte array is not being successful i'm not getting any exception but the length of the byte is 0
any idea how can i fix this?
Please update the following line of code and try again.
Me.dynamicDotNetTwain1.OCRLanguage = "eng" 'not English

ReportViewer.LocalReport.Render("PDF")

I am using Report Viewer for WinForms. The problem i am having is this: I have a form that contains a form which is used to view a local report which work fine, but when I try to render the same report as a PDF, it is cut-off, but in report viewer the same report renders a report on one page. When I render to PDF it cuts it off and the part of the report that was cut-off renders on a 2nd page. So in other words, part of the same report is on page 1, and 2nd half is on 2nd page in the PDF?
Code:
Private Function GetPDfReport() As String
Dim parameters = Me.GetReportParms()
Dim query = Me.GetReportQuery()
Dim rView As Microsoft.Reporting.WinForms.ReportViewer = New Microsoft.Reporting.WinForms.ReportViewer
rView.Dock = DockStyle.Fill
rView.SetDisplayMode(DisplayMode.PrintLayout)
Dim pnl As New Panel()
pnl.Name = "pnlMain"
pnl.Location = New System.Drawing.Point(0, 25)
pnl.Size = New System.Drawing.Size(734, 478)
pnl.Controls.Add(rView)
Dim dbReader As New dbReader()
Dim ds As DataSet = dbReader.DataSet(query)
Dim rds As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", ds.Tables(0))
rView.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
rView.LocalReport.DataSources.Add(rds)
rView.LocalReport.ReportEmbeddedResource = "EasyDose.rptIncident.rdlc"
If Not IsNothing(parameters) Then
Dim Bound0 As Integer = parameters.GetUpperBound(0)
Dim Bound1 As Integer = parameters.GetUpperBound(1)
For index = 0 To Bound0
Dim rParameter As New ReportParameter(parameters(index, 0), parameters(index, 1))
rView.LocalReport.SetParameters(rParameter)
Next
End If
Dim ps As PageSettings = rView.GetPageSettings
ps.Margins.Top = 0 ' 10mm approx
ps.Margins.Right = 0
ps.Margins.Bottom = 0
ps.Margins.Left = 0
ps.Landscape = False
'ps.PaperSize = New PaperSize("LetterExtra", (9.275 * 100), (12 * 100)) ' Letter paper (8.5 in. by 11 in.) ' Letter extra paper (9.275 in. by 12 in.)
ps.PaperSize = New PaperSize("A4", (8.27 * 100), (11.69 * 100))
rView.RefreshReport()
Dim exePath As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath)
Dim dir As New DirectoryInfo(System.IO.Path.Combine(exePath, "tmpDir"))
Dim file As New FileInfo(System.IO.Path.Combine( _
dir.FullName, String.Format("Patient_Details_{0:yyyyMMdd_hhmmss}.pdf", DateTime.Now)))
If Not dir.Exists Then
dir.Create()
End If
Dim bytes As Byte() = rView.LocalReport.Render("PDF")
Using fs As New System.IO.FileStream(file.FullName, System.IO.FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
End Using
Return file.FullName
End Function
are you seeing the local report in the embedded ReportViewer using the "Print Layout" option activated? That should show exactly the same output as your printed result.
If you have problems in the PDF is probably caused by the design of the report itself. Check the font, the page size and orientation, the margins, the page breaks.
uisng System.IO;
byte[] rep = reportViewer1.LocalReport.Render("pdf", deviceInfo: "");
// if a certificate warning appears just ignore and re-run
File.WriteAllBytes(filepath+filename+".pdf",rep);

Zebra Printer Outputing HEX when trying to print a bitmap

I am using a Zebra iMZ320 printer, a windows mobile device, CPCL and vb.net.
I am trying to get the code to load a bitmap image and then to print this using CPCL
I have previoulsy had a similar piece of code to that contaibed below working with no issue. I must be missing something obvious, but for the life of me I cannot see it.
My problem is the printer will only printout HEX instead of the image ! Has anyone come across this before ? Can you help ?
Public Sub DrawBitmap(ByVal xPosition As Integer, ByVal yPosition As Integer)
Dim bmp As Bitmap
bmp = New System.Drawing.Bitmap(GetLogo)
If bmp Is Nothing Then
Throw New ArgumentNullException("bmp")
End If
'Make sure the width is divisible by 8
Dim loopWidth As Integer = 8 - (bmp.Width Mod 8)
If loopWidth = 8 Then
loopWidth = bmp.Width
Else
loopWidth += bmp.Width
End If
cpclData = ""
cpclData = cpclData & "! 0 200 200 300 1 " & vbCr & vbLf
cpclData = cpclData & (String.Format("EG {0} {1} {2} {3} ", loopWidth \ 8, bmp.Height, xPosition, yPosition))
For y As Integer = 0 To bmp.Height - 1
Dim bit As Integer = 128
Dim currentValue As Integer = 0
For x As Integer = 0 To loopWidth - 1
Dim intensity As Integer
If x < bmp.Width Then
Dim color As Color = bmp.GetPixel(x, y)
Dim MyR As Integer = color.R
Dim MyG As Integer = color.G
Dim MyB As Integer = color.B
intensity = 255 - ((MyR + MyG + MyB) / 3)
Else
intensity = 0
End If
If intensity >= 128 Then
currentValue = currentValue Or bit
End If
bit = bit >> 1
If bit = 0 Then
cpclData = cpclData & (currentValue.ToString("X2"))
bit = 128
currentValue = 0
End If
'x
Next
Next
'y
cpclData = cpclData & vbCr & vbLf
cpclData = cpclData & "PRINT"
Print_Invoice()
End Sub
Public Shared Function StrToByteArray(ByVal str As String) As Byte()
Dim encoding As New System.Text.ASCIIEncoding()
Return encoding.GetBytes(str)
End Function
Private Sub Print_Invoice()
' Instantiate a connection
Dim thePrinterConn As ZebraPrinterConnection = New BluetoothPrinterConnection(MyMacAddress)
' Open the connection - physical connection is established here.
thePrinterConn.Open()
' Send the data to the printer as a byte array
thePrinterConn.Write(StrToByteArray(cpclData))
' Make sure the data got to the printer before closing the connection
Thread.Sleep(500)
' Close the connection to release resources.
thePrinterConn.Close()
' Debug output
txt_TestPrint.Text = cpclData.ToString
Dim objStreamWriter As StreamWriter
Dim file_name As String
'open dialog box for new file
SaveFileDialog1.InitialDirectory = "\Storage Card\"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
file_name = SaveFileDialog1.FileName
If Len(file_name) > 0 Then
objStreamWriter = New StreamWriter(file_name & ".txt")
'Write a line of text from list box.
objStreamWriter.WriteLine(txt_TestPrint.Text)
'Close the file.
objStreamWriter.Close()
Exit Sub
End If
End If
End Sub
The code produce this file as output if it helps.
! 0 200 200 300 1
EG 10 80 10 10 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF800FFF8001FFFFFFFFFE0001FC00007FFFFFFFF800007000001FFFFFFFF01F80703FF80FFFFFFFE07FF800FFFE07FFFFFFC1FFFC01FFFF87FFFFFFC3FFFC01FFFF83FFFFFF87FFFC01FFFFC3FFFFFF87FFFC11FFFFC3FFFFFF87FFFC39FFFFC1FFFFFF87FFFC7FFFFFC1FFFFFF87FFFCFFFFFFC1FFFFFF87FFFFFFFFFFC1FFFFFFC3FFFFFFFFFFC3FFFFFFC1FFFFFFFFFF83FFFFFFE1FFFFFFFFFF07FFFFFFE1FFFFFFFFFE07FFFFFFE1FFFFFFFFFC0FFFFFFFE1FFFFCF0FF81FFFFFFFC3FFFF8001C03FFFFFFFC3FFCF800000FFFFFFFFC1FF87C04003FFFFFFFFE0FF07FFF81FFFFFFFFFE0780FFFFC3FFFFFFFFFF0001FFFF83FFFFFFFFFFC003FFFF87FFFFFFFFFFF803FFFF87FFFFFFFFFFFFC1FFFF87FFFFFFFFFFFFC1FFFF07FFFFFFFFFFFFE0FFFF0FFFFFFFFFFFFFE0FC020FFFFFFFFFFFFFF000000FFFFFFFFFFFFFF000001FFFFFFFFFFFFFF8001C1FFFFFFFFFFFFFF83E3F9FFFFFFFFFFFFFFCFE3FFFFFFFFFFFFFFFFF9C39FFFFFFFFFFFFFFFF8C70FFFFFFFFFFFFFFFF9879FFFFFFFFFFFFFFFFF8FFFFFFFFFFFFFFFF7FF807FFFFFFFFFFFFFEFFF00FFF7FFFFFFFFFFCFFF03FFF3FFFFFFFFFFCFFFFFFFF3FFFFFFFFFFCFFC001FF3FFFFFFFFFFC7F8000FE3FFFFFFFFFFC1F0000783FFFFFFFFFFC000FF0003FFFFFFFFFFE001FF8007FFFFFFFFFFF003FFC00FFFFF3FFFFFFC07FFF03FFFFE1FFF7FFFFFFFFFFFFFC61FFE0FFFFFFFFFFFFC023FFE01FFFFFFFFFFF803FFFF003FFFFFFFFF0C0FFFFF800FFFFFFFF00C1FFFFF02041FFC7FE00C3FFFFF070000E001E0183FFFFE070400C001F4183FFFFE000F008001FE001FFFFE001F0783C1FE000FFFFC083F0783C1FF0003FFC01C1E0F07E3FF03007FC01E1E0F0700FF83803FE00C1E0F06007F80C07FF0001E0F06007F8043FFFF003E0F83C1FF007FFFFFE0700F83C1FE00FFFFFFFFE0038001FF0FFFFFFFFFF003C001FFFFFFFFFFFFFE03E001FFFFFFFFFFFFFFFFFCFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
PRINT
The iMZ printer comes pre configured to be in line mode. You have to change it to zpl mode so that it would parse either zpl or cpcl
Send this SGD to change the language of the printer.
! U1 setvar "device.languages" "zpl"
You convert the bitmap to a hex string
cpclData = cpclData & (currentValue.ToString("X2"))
then you encode this as byte[]
Return encoding.GetBytes(str)
end send the data to the printer:
thePrinterConn.Write(StrToByteArray(cpclData))
But I assume you have to encode the hex data string back to a byte array with the coresponding hex values converted back to a byte. In example a hex string of "FFFFFFFF" has to be converted back to byte[]{0xff,0xff,0xff,0xff}, exxcpet the printer language (CPCL?) reads hex string data and converts that back to byte itself.

Live text reader with highlighting in VB.net

I have a program which writes to a .log file as it is installing software.
Some of the lines will contain either WARNING or FAILED.
What I would like, is a window in my program which will read the .log file and display the content into this window as it is being written too. Any lines which contain WARNING or FAILED in them are highlighted yellow/red.
Does anyone know how to do this?
Create a FORM (I used VB 2010) and add this code.
it will write 3 lines on the form in 2 colours.
It might get you on your way. Ask MSDN help for each function that is new to you.
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim chRs(0) As CharacterRange
Dim sbRs(0) As SolidBrush
Dim flRs(0) As SolidBrush
Dim blueBrush As New SolidBrush(Color.Blue)
Dim whiteBrush As New SolidBrush(Color.White)
Dim redBrush As New SolidBrush(Color.Red)
Dim EditFont As New Font("Courier New", 9)
Dim stringFormat As New StringFormat
Dim aRectangle As Rectangle
Dim RectHeight As Integer = 20
For i = 1 To 3
Dim txt As String = "a string " & CStr(i)
If i = 2 Then
sbRs(0) = blueBrush
Else
sbRs(0) = redBrush
End If
flRs(0) = whiteBrush
chRs(0) = New CharacterRange(0, txt.Length())
Dim chRsa As Array = Array.CreateInstance(GetType(CharacterRange), 1)
Array.Copy(chRs, 0, chRsa, 0, 1)
aRectangle = New Rectangle(0, CInt((i - 1) * RectHeight), ClientRectangle.Size.Width, RectHeight) ' x, y, w, h
stringFormat.SetMeasurableCharacterRanges(chRsa)
Dim stringRegions As Array = Array.CreateInstance(GetType([Region]), 1)
stringRegions = e.Graphics.MeasureCharacterRanges(txt, EditFont, aRectangle, stringFormat)
Dim measureRect1 As RectangleF = stringRegions(0).GetBounds(e.Graphics)
Dim g As Graphics = e.Graphics
g.FillRectangle(flRs(0), measureRect1)
g.DrawString(txt.Substring(chRs(0).First, chRs(0).Length), EditFont, sbRs(0), CSng(measureRect1.X), CSng(measureRect1.Y))
Next
End Sub