|
-
Oct 2nd, 2012, 10:43 AM
#1
[RESOLVED] First attempt at printing in VB.Net 2005
Hi guys
I am working on a project where I need to send some plain text data to a printer using the generic text only print driver. I have done this many times in VB6 but am a little lost in VB.Net as to how to work with the printer.
For example in VB6 my code may look something like this
Code:
Dim ZPLString As String
For x = 0 To List2.ListCount - 1
ZPLString = "^FO" & CStr(lblParts.BCLeft) & "," & CStr(lblParts.BCTop)
ZPLString = ZPLString & "^B3N,Y," & CStr(lblParts.BCHeight) & ",N,N"
ZPLString = ZPLString & "^FD" & List2.List(x) & "^FS" & vbCrLf
ZPLString = ZPLString & "^FO" & CStr(lblParts.TextLeft) & "," & CStr(lblParts.TextTop)
ZPLString = ZPLString & "^A0,N," & CStr(lblParts.TextSize) & "," & CStr(lblParts.TextWidth)
ZPLString = ZPLString & "^FD" & List2.List(x) & " " & "^FS"
For y = 1 To Val(Text1.Text)
Printer.Print "^XA"
Printer.Print ZPLString
Printer.Print "^XZ"
Next
Next
Printer.EndDoc
Can anyone assist me in how I should do something like this in VB.Net 2005?
-
Oct 2nd, 2012, 10:50 AM
#2
Re: First attempt at printing in VB.Net 2005
btw: I did not include it above but the project requires me to conditionally print to one of two different printers so I also need to know how to select the printer that I will be sending data to. Again I have did this many times in VB6 but the Printer seems much different in VB.Net
-
Oct 2nd, 2012, 11:19 AM
#3
Re: First attempt at printing in VB.Net 2005
Printing is a whole different animal in .NET.
This should get you started. First, Add a PrintDocument to your form....then try something like
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim fnt As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)
Dim ListBoxItem As String = String.Empty
For Each LBItem As String In ListBox1.Items
ListBoxItem = ListBoxItem & vbCrLf & LBItem
Next
ListBoxItem = ListBoxItem.Substring(vbCrLf.Length)
e.Graphics.DrawString(ListBoxItem, fnt, Brushes.Black, 0, 0)
e.HasMorePages = False
End Sub
-
Oct 2nd, 2012, 11:37 AM
#4
Re: First attempt at printing in VB.Net 2005
Thanks,
I'll give that a go here in a minute and see what happens
I should mention that this project is a service so there will be no forms in it, data will be coming from a SQL Server query and sent to the printer as needed.
-
Oct 2nd, 2012, 11:41 AM
#5
Re: First attempt at printing in VB.Net 2005
First you're gonna need a print event designed for string printing ...
vb.net Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim numChars As Integer
Dim numLines As Integer
Dim stringForPage As String
Dim strFormat As New StringFormat()
Dim PrintFont As Font
'PrintFont = some font
Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
strFormat.Trimming = StringTrimming.Word
e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
stringForPage = StringToPrint.Substring(0, numChars)
e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
If numChars < StringToPrint.Length Then
StringToPrint = StringToPrint.Substring(numChars)
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
Imposing isn't it. I have to confess to stealing it rather than programming it myself! Then you're gonna need a global variable ....
vb.net Code:
Dim StringToPrint As String
Then a command ....
vb.net Code:
StringToPrint = ZPLString
PrintDocument1.Print()
And then you're probably gonna need a nice lie-down in a darkened room!
Last edited by dunfiddlin; Oct 2nd, 2012 at 11:48 AM.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Oct 2nd, 2012, 11:44 AM
#6
Re: First attempt at printing in VB.Net 2005
 Originally Posted by DataMiser
I should mention that this project is a service so there will be no forms in it
Yeah, that helps to know because if there are no forms, then there is nothing to put the PrintDocument on, so my solution isn't going to work, and dunfiddlin is using a printdocument in his example, so that ain't gonna work either.
-
Oct 2nd, 2012, 11:47 AM
#7
Re: First attempt at printing in VB.Net 2005
Ok as a test I created a new project with a form, listbox, button and print document then added 3 items to the listbox. I pasted in the code for printing from post #2 and set my default printer to the generic text only and had it output to a file. The first part looks good but then there is a bunch of extra line feeds ( over 60 of them) at the end which could pose a problem.
Is there a way to surpress these extra line feeds?
-
Oct 2nd, 2012, 11:48 AM
#8
Re: First attempt at printing in VB.Net 2005
 Originally Posted by Hack
Yeah, that helps to know because if there are no forms, then there is nothing to put the PrintDocument on, so my solution isn't going to work, and dunfiddlin is using a printdocument in his example, so that ain't gonna work either.
Actually it does allow me to add a Printdocument to my service designer page so it might work if I can get rid of the extra line feeds
-
Oct 2nd, 2012, 11:51 AM
#9
Re: First attempt at printing in VB.Net 2005
Do you have e.HasMorePages set to False?
-
Oct 2nd, 2012, 12:12 PM
#10
Re: First attempt at printing in VB.Net 2005
I tried first with the e.HasMorePages=False and then tried with that line commented out in both cases the output looks like this when viewed as hex.

When using the example in post 5 there are more unwanted characters output looks like this
-
Oct 2nd, 2012, 12:17 PM
#11
Re: First attempt at printing in VB.Net 2005
Actually, this may not be an issue in the code at all. As a test I just created a little notepad document and sent it to the printer with output to a file and see the same leading characters as the second image above as well as the extra line feeds at the bottom so now am thinking they may be added by the print driver?
I'll try a quick test in VB6 and see if I get the same output.
-
Oct 2nd, 2012, 12:21 PM
#12
Re: First attempt at printing in VB.Net 2005
 Originally Posted by Hack
Yeah, that helps to know because if there are no forms, then there is nothing to put the PrintDocument on, so my solution isn't going to work, and dunfiddlin is using a printdocument in his example, so that ain't gonna work either.
What sort of defeatist talk is that?
vb.net Code:
Imports System.Drawing.Printing
Public Class FormlessApp
Dim WithEvents PrintDocument1 As New PrintDocument
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Oct 2nd, 2012, 12:25 PM
#13
Re: First attempt at printing in VB.Net 2005
Ok After running a test in VB6 I am seeing those same LineFeeds at the end so there is no problem with the code, looks like it should work just fine if I can get it to work in my service.
I do not have the actual printer connected to the system yet so all I could do was look at the text output and did not expect to see those line feeds, thought it may be a problem but appranently not as I have did this successfully many times using the VB6 method. The extra line feeds should not cause a problem.
Thanks and rep to both of you for the help, I'll be back if I run into an issue but for now it looks like I am on the right track.
-
Oct 2nd, 2012, 01:32 PM
#14
Re: First attempt at printing in VB.Net 2005
Looks like I have an issue somewhere. I have tried this code with and without the has more pages part seems to give the same result in either case and I am stumped as to what is going on here.
Code:
While Dr.Read
Ticket = "^XA" & vbNewLine
Ticket &= "^FO"
Ticket &= "^FD" & "Ticket #:" & Dr(0).ToString.Trim & " ^FS" & vbNewLine
Ticket &= "^XZ" & vbCrLf
MessageBox.Show(Ticket)
e.Graphics.DrawString(Ticket, fnt, Brushes.Black, 0, 0)
e.HasMorePages = True
End While
The message box for the first record shows

There are 2 records total. The output file looks like

I'm not sure what is going on, it almost looks like it is somehow super imposing the second record over the first ...
Any idea what I am doing wrong?
-
Oct 2nd, 2012, 01:54 PM
#15
Re: First attempt at printing in VB.Net 2005
How are you obtaining the 'output file'? What are you expecting to see in the MessageBox? It looks exactly like it should to me. Is the second one different (apart from the actual data obviously)?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Oct 2nd, 2012, 01:57 PM
#16
Re: First attempt at printing in VB.Net 2005
Ok I think I have a handle on it now, Looks like I need to loop through the recordset in a different routine then call this routine to print a ticket or group of tickets.
As a test I added an exit while into the loop so it would stop at one record and it looked ok so apparently the loop was corrupting my output where it called the drawstring a second time.
Hopefully I can get this to work the way I want now.
Will post back if I have additional issues or will post a sample if I get it working correctly.
Thanks
Edit to respond to post above
How are you obtaining the 'output file'? What are you expecting to see in the MessageBox? It looks exactly like it should to me. Is the second one different (apart from the actual data obviously)?
The output file is from the printer. I have a generic textonly driver installed set to print to file which I opened with ultraedit and viewed as hex.
The message box was correct. The output file was not. I was expecting to get 2 instances of something that looked like that in the message box but the 2 got jumbled together and came out a mess. When I added the exit so it would only try one record it came out fine so that 2nd call to draw must have merged the two rather than append
I think I have a working idea of how to get it to work as I need now,
Last edited by DataMiser; Oct 2nd, 2012 at 02:04 PM.
-
Oct 2nd, 2012, 02:13 PM
#17
Re: First attempt at printing in VB.Net 2005
Ok. When it comes to selecting printers ....
vb.net Code:
Dim printers As New List(Of String)
For Each p In PrinterSettings.InstalledPrinters 'gets all printers currently installed
printers.Add(p)
Next
PrintDocument1.PrinterSettings.PrinterName = printers(i) 'sets printer (i = list index) to be used for the document
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Oct 2nd, 2012, 02:30 PM
#18
Re: First attempt at printing in VB.Net 2005
Thanks,
I'm going to try to put the code into a service later today.
I have modified my code and now the output file looks correct.
segments of the new code
Code:
Private TicketData As String = String.Empty
Code:
If Dr.HasRows Then
While Dr.Read
If LastTicket = String.Empty Then
LastTicket = Dr(0).ToString
Else
If Not LastTicket = Dr(0).ToString Then
TicketData = Ticket.ToString
Ticket.Remove(1, Ticket.Length)
PrintDocument1.Print()
End If
End If
Ticket.Append("^XA")
Ticket.AppendLine()
Ticket.Append("^FO")
Ticket.AppendLine()
Ticket.Append("^FD" & "Ticket #:" & Dr(0).ToString.Trim & " ^FS")
Ticket.AppendLine()
Ticket.Append("^XZ")
Ticket.AppendLine()
End While
TicketData = Ticket.ToString
PrintDocument1.Print()
End If
Code:
Dim fnt As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)
e.Graphics.DrawString(TicketData, fnt, Brushes.Black, 0, 0)
e.HasMorePages = False
output now when viewed as text
^XA
^FO
^FDTicket #:T90 ^FS
^XZ
^XA
^FO
^FDTicket #:T90 ^FS
^XZ
Still have a lot to add to the ticket format but I think the hard part is done and with the info on choosing the printer it should be smooth sailing from here on in.
Thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|