Results 1 to 18 of 18

Thread: [RESOLVED] First attempt at printing in VB.Net 2005

  1. #1
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    Resolved [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?

  2. #2
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    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

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    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
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    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.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,506

    Re: First attempt at printing in VB.Net 2005

    First you're gonna need a print event designed for string printing ...

    vb.net Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.         Dim numChars As Integer
    3.         Dim numLines As Integer
    4.         Dim stringForPage As String
    5.         Dim strFormat As New StringFormat()
    6.         Dim PrintFont As Font
    7.         'PrintFont = some font
    8.         Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
    9.         Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
    10.         strFormat.Trimming = StringTrimming.Word
    11.         e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
    12.         stringForPage = StringToPrint.Substring(0, numChars)
    13.         e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
    14.         If numChars < StringToPrint.Length Then
    15.             StringToPrint = StringToPrint.Substring(numChars)
    16.             e.HasMorePages = True
    17.         Else
    18.             e.HasMorePages = False
    19.         End If
    20.     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:
    1. Dim StringToPrint As String

    Then a command ....

    vb.net Code:
    1. StringToPrint = ZPLString
    2.         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!"

    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!

  6. #6
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: First attempt at printing in VB.Net 2005

    Quote Originally Posted by DataMiser View Post
    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.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  7. #7
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    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?

  8. #8
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    Re: First attempt at printing in VB.Net 2005

    Quote Originally Posted by Hack View Post
    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

  9. #9
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: First attempt at printing in VB.Net 2005

    Do you have e.HasMorePages set to False?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  10. #10
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    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.
    Name:  testprn.jpg
Views: 248
Size:  39.8 KB

    When using the example in post 5 there are more unwanted characters output looks like this
    Name:  testprn2.jpg
Views: 247
Size:  46.6 KB

  11. #11
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    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.

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,506

    Re: First attempt at printing in VB.Net 2005

    Quote Originally Posted by Hack View Post
    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:
    1. Imports System.Drawing.Printing
    2.  
    3. Public Class FormlessApp
    4.  
    5.     Dim WithEvents PrintDocument1 As New PrintDocument
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    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!

  13. #13
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    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.

  14. #14
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    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
    Name:  msg.jpg
Views: 243
Size:  5.9 KB

    There are 2 records total. The output file looks like
    Name:  msg1.jpg
Views: 248
Size:  135.0 KB

    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?

  15. #15
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,506

    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!"

    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!

  16. #16
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    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.

  17. #17
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,506

    Re: First attempt at printing in VB.Net 2005

    Ok. When it comes to selecting printers ....

    vb.net Code:
    1. Dim printers As New List(Of String)
    2.         For Each p In PrinterSettings.InstalledPrinters 'gets all printers currently installed
    3.             printers.Add(p)
    4.         Next
    5.  
    6.         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!"

    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!

  18. #18
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,956

    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
  •