Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. printer from file [modified] ... resolved

printer from file [modified] ... resolved

Scheduled Pinned Locked Moved Visual Basic
graphicstutorialdiscussion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Engelkes
    wrote on last edited by
    #1

    When the following code is executed.

     Dim strPrint As New StringBuilder
            For Me.counter = 0 To 19
                strPrint.Append(writeline(Me.counter) & vbCrLf)
            Next Me.counter
    
            'e.Graphics.DrawString(strPrint.ToString, printFont, System.Drawing.Brushes.Black, 10, 10)
    
            If System.IO.File.Exists(SaveFileDialog1.FileName) Then
    
                printfile = File.OpenText(SaveFileDialog1.FileName)
                printfile = System.IO.File.OpenText(SaveFileDialog1.FileName)
                Do Until printfile.Peek = -1
                    menuitem(printcounter) = printfile.ReadLine
                    e.Graphics.DrawString(printfile.ReadToEnd, printFont, _
                    System.Drawing.Brushes.Black, 10, 10)
                    printcounter = printcounter + 1
                Loop
    
                printfile.Close()
    
    
            End If
    

    It does not print the in the same way on how it looks in notepad. Any Ideas. You can get writeline code on the forum discussion title "writing to text file" Any ideas how to resolve issues. issues as has been resolved by changing the font name..

    modified on Friday, March 12, 2010 9:16 PM

    L A T 3 Replies Last reply
    0
    • D Daniel Engelkes

      When the following code is executed.

       Dim strPrint As New StringBuilder
              For Me.counter = 0 To 19
                  strPrint.Append(writeline(Me.counter) & vbCrLf)
              Next Me.counter
      
              'e.Graphics.DrawString(strPrint.ToString, printFont, System.Drawing.Brushes.Black, 10, 10)
      
              If System.IO.File.Exists(SaveFileDialog1.FileName) Then
      
                  printfile = File.OpenText(SaveFileDialog1.FileName)
                  printfile = System.IO.File.OpenText(SaveFileDialog1.FileName)
                  Do Until printfile.Peek = -1
                      menuitem(printcounter) = printfile.ReadLine
                      e.Graphics.DrawString(printfile.ReadToEnd, printFont, _
                      System.Drawing.Brushes.Black, 10, 10)
                      printcounter = printcounter + 1
                  Loop
      
                  printfile.Close()
      
      
              End If
      

      It does not print the in the same way on how it looks in notepad. Any Ideas. You can get writeline code on the forum discussion title "writing to text file" Any ideas how to resolve issues. issues as has been resolved by changing the font name..

      modified on Friday, March 12, 2010 9:16 PM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, 1.

      Daniel Engelkes wrote:

      It does not print the in the same way on how it looks in notepad

      is not a symptom we can work on. What does it do, and how is that different from what you want/expect? 2. A do-loop containing printfile.ReadToEnd will iterate only once. Your first line goes to "menuitem", everything else goes to "DrawString", there isn't a second iteration. 3. Most often, the correct way to print a number of lines is by having a loop, where each iteration prints one line of text, at a different location; so you need to have coordinates (x,y) which change as you iterate your loop. 4. Very likely, I haven't checked that part of your code: in order to write a number of strings to a text file, there is no need to concatenate the text and write all in one go. One can: - either use a FileStream and the WriteLine method; - or fill a string array and use File.WriteAllLines(). :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


      1 Reply Last reply
      0
      • D Daniel Engelkes

        When the following code is executed.

         Dim strPrint As New StringBuilder
                For Me.counter = 0 To 19
                    strPrint.Append(writeline(Me.counter) & vbCrLf)
                Next Me.counter
        
                'e.Graphics.DrawString(strPrint.ToString, printFont, System.Drawing.Brushes.Black, 10, 10)
        
                If System.IO.File.Exists(SaveFileDialog1.FileName) Then
        
                    printfile = File.OpenText(SaveFileDialog1.FileName)
                    printfile = System.IO.File.OpenText(SaveFileDialog1.FileName)
                    Do Until printfile.Peek = -1
                        menuitem(printcounter) = printfile.ReadLine
                        e.Graphics.DrawString(printfile.ReadToEnd, printFont, _
                        System.Drawing.Brushes.Black, 10, 10)
                        printcounter = printcounter + 1
                    Loop
        
                    printfile.Close()
        
        
                End If
        

        It does not print the in the same way on how it looks in notepad. Any Ideas. You can get writeline code on the forum discussion title "writing to text file" Any ideas how to resolve issues. issues as has been resolved by changing the font name..

        modified on Friday, March 12, 2010 9:16 PM

        A Offline
        A Offline
        Andy_L_J
        wrote on last edited by
        #3

        I would load the file into a List or similar collection (coll) off String Array objects and then iterate through the collection, setting the x,y coordinates as appropriate:

        'Read each line into an array and then put each array into a Generic.List(Of Array)
        ' Then do something like this in the PrintPage Event...

        Dim l As Single = e.MarginBounds.Left ' Left margin
        Dim y as Single = e.MarginBounds.Top ' Top margin
        Dim h as Integer = 18 ' Row Height
        Dim ws() As Single = {100, 50, 50, 50) ' Column widths
        Dim sfs() As StringFormat = {StringFomat.Alignment.Far, StrngFormat.Alignment.Centre, _
        StringFormat.Alignment.Near, StringFormat.Far}
        Dim cuRow As Integer = 0
        Dim linesReq As Integer = coll.Count - 1

        Dim fnt as New Font("Courier New", 10, FontStype.Regular)
        Dim pageline as Integer = 1
        Dim linesPerPage as Integer = Math.Floor((e.MarginBounds.Bottom-y)/h)

        For i as Integer = curRow to coll.Count -1
        Do While (pageLine <= linesPerPage) And (curRow <= linesReq)
        PrintText(e, coll.Item(curRow), l, y, ws, h, fnt, Brushes.Black, sfs())
        y += h
        pageLine += 1
        curRow += 1
        Loop

        If (cuRow > linesReq)
        Exit For
        End If
        Next i
        ...

        Private Sub PrintText(ByVal e as PrintPageEventArgs, txtArray As Array, _
        ByVal l as Single, ByVal y as Single, ByVal ws As Array, _
        ByVal h As Single, ByVal fnt As Font, ByVal br as Brush, _
        ByVal sfs As Array)

        For i as Integer = 0 To txtArray.Count - 1
        e.Graphics.DrawString(txtArray(i), l, y, ws(i), h, fnt, br, sfs(i))
        l += ws(i)
        Next

        End Sub

        I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

        modified on Friday, March 12, 2010 3:28 AM

        1 Reply Last reply
        0
        • D Daniel Engelkes

          When the following code is executed.

           Dim strPrint As New StringBuilder
                  For Me.counter = 0 To 19
                      strPrint.Append(writeline(Me.counter) & vbCrLf)
                  Next Me.counter
          
                  'e.Graphics.DrawString(strPrint.ToString, printFont, System.Drawing.Brushes.Black, 10, 10)
          
                  If System.IO.File.Exists(SaveFileDialog1.FileName) Then
          
                      printfile = File.OpenText(SaveFileDialog1.FileName)
                      printfile = System.IO.File.OpenText(SaveFileDialog1.FileName)
                      Do Until printfile.Peek = -1
                          menuitem(printcounter) = printfile.ReadLine
                          e.Graphics.DrawString(printfile.ReadToEnd, printFont, _
                          System.Drawing.Brushes.Black, 10, 10)
                          printcounter = printcounter + 1
                      Loop
          
                      printfile.Close()
          
          
                  End If
          

          It does not print the in the same way on how it looks in notepad. Any Ideas. You can get writeline code on the forum discussion title "writing to text file" Any ideas how to resolve issues. issues as has been resolved by changing the font name..

          modified on Friday, March 12, 2010 9:16 PM

          T Offline
          T Offline
          The Man from U N C L E
          wrote on last edited by
          #4

          How you read or write the file has nothing to do with how it prints. Are you using the same font? How are you handling the PrintDocument events for new Pages etc.? Do you have the margins set up the same as notepad does? The questions are endless.

          If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

          D 1 Reply Last reply
          0
          • T The Man from U N C L E

            How you read or write the file has nothing to do with how it prints. Are you using the same font? How are you handling the PrintDocument events for new Pages etc.? Do you have the margins set up the same as notepad does? The questions are endless.

            If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

            D Offline
            D Offline
            Daniel Engelkes
            wrote on last edited by
            #5

            when I write to the file this is the format

               Menu Items	Qty	Unit Cost	Total Cost
               ----------	---	---------	----------
            

            cheeseburger 0 $5.00 $0.00
            onion rings 0 $6.00 $0.00
            chicken sdwch 0 $5.00 $0.00
            milk shake 0 $2.00 $0.00
            mexican rice 0 $5.00 $0.00
            quasidella 0 $2.00 $0.00
            shrimp cocktail 0 $5.00 $0.00
            lasagne 0 $3.00 $0.00
            spagehtti 0 $5.00 $0.00
            cake 1 $2.00 $2.00
            cookies 1 $6.00 $6.00

            			 OrderTotal:$8.00
            			 DrinkTotal:$0.00
            			 ---------------
            
            			   SubTotal:$8.00
            			 Tax Amount:$0.60
            			 ---------------
            
            			Grand Total:$8.60
            

            but when I print the document from the program it end up like this

               MenuItemsQty	Unit Cost	Total Cost
               ----------   ---	--------- ----------
            

            cheeseburger 0 $5.00 $0.00
            onion rings 0 $6.00 $0.00
            chicken sdwch 0 $5.00 $0.00
            milk shake 0 $2.00 $0.00
            mexican rice 0 $5.00 $0.00
            quasidella 0 $2.00 $0.00
            shrimp cocktail 0 $5.00 $0.00
            lasagne 0 $3.00 $0.00
            spagehtti 0 $5.00 $0.00
            cake 1 $2.00 $2.00
            cookies 1 $6.00 $6.00

            it needs the same format. any ideas.

            L W 2 Replies Last reply
            0
            • D Daniel Engelkes

              when I write to the file this is the format

                 Menu Items	Qty	Unit Cost	Total Cost
                 ----------	---	---------	----------
              

              cheeseburger 0 $5.00 $0.00
              onion rings 0 $6.00 $0.00
              chicken sdwch 0 $5.00 $0.00
              milk shake 0 $2.00 $0.00
              mexican rice 0 $5.00 $0.00
              quasidella 0 $2.00 $0.00
              shrimp cocktail 0 $5.00 $0.00
              lasagne 0 $3.00 $0.00
              spagehtti 0 $5.00 $0.00
              cake 1 $2.00 $2.00
              cookies 1 $6.00 $6.00

              			 OrderTotal:$8.00
              			 DrinkTotal:$0.00
              			 ---------------
              
              			   SubTotal:$8.00
              			 Tax Amount:$0.60
              			 ---------------
              
              			Grand Total:$8.60
              

              but when I print the document from the program it end up like this

                 MenuItemsQty	Unit Cost	Total Cost
                 ----------   ---	--------- ----------
              

              cheeseburger 0 $5.00 $0.00
              onion rings 0 $6.00 $0.00
              chicken sdwch 0 $5.00 $0.00
              milk shake 0 $2.00 $0.00
              mexican rice 0 $5.00 $0.00
              quasidella 0 $2.00 $0.00
              shrimp cocktail 0 $5.00 $0.00
              lasagne 0 $3.00 $0.00
              spagehtti 0 $5.00 $0.00
              cake 1 $2.00 $2.00
              cookies 1 $6.00 $6.00

              it needs the same format. any ideas.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Ha, we finally got a symptom. Seems you want columnar results without using one DrawString for each cell. So you are relying on tabs (very difficult) or on spaces to get data formatted in columns. Unless you print individual cells, you need to choose a non-proportional or monospaced font, such as Courier New, and NOT a proportional font such as Arial. (If you choose Arial in Notepad, the data will look bad too!) :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


              1 Reply Last reply
              0
              • D Daniel Engelkes

                when I write to the file this is the format

                   Menu Items	Qty	Unit Cost	Total Cost
                   ----------	---	---------	----------
                

                cheeseburger 0 $5.00 $0.00
                onion rings 0 $6.00 $0.00
                chicken sdwch 0 $5.00 $0.00
                milk shake 0 $2.00 $0.00
                mexican rice 0 $5.00 $0.00
                quasidella 0 $2.00 $0.00
                shrimp cocktail 0 $5.00 $0.00
                lasagne 0 $3.00 $0.00
                spagehtti 0 $5.00 $0.00
                cake 1 $2.00 $2.00
                cookies 1 $6.00 $6.00

                			 OrderTotal:$8.00
                			 DrinkTotal:$0.00
                			 ---------------
                
                			   SubTotal:$8.00
                			 Tax Amount:$0.60
                			 ---------------
                
                			Grand Total:$8.60
                

                but when I print the document from the program it end up like this

                   MenuItemsQty	Unit Cost	Total Cost
                   ----------   ---	--------- ----------
                

                cheeseburger 0 $5.00 $0.00
                onion rings 0 $6.00 $0.00
                chicken sdwch 0 $5.00 $0.00
                milk shake 0 $2.00 $0.00
                mexican rice 0 $5.00 $0.00
                quasidella 0 $2.00 $0.00
                shrimp cocktail 0 $5.00 $0.00
                lasagne 0 $3.00 $0.00
                spagehtti 0 $5.00 $0.00
                cake 1 $2.00 $2.00
                cookies 1 $6.00 $6.00

                it needs the same format. any ideas.

                W Offline
                W Offline
                Wayne Gaylard
                wrote on last edited by
                #7

                This is how I do my report printing. I have adapted one of my reports to sort of get what you are looking for. I used a set of arrays to input the data, whereas you would obviously get it from the app.

                Private items() As String = {"Cheeseburger", "Onion Rings", "Chicken Sandwich", "Milk Shake"}
                Private costs() As Integer = {5, 6, 5, 2}
                Private quantities() As Integer = {1, 2, 2, 2}
                Private intLinesPerPage As Integer = 25
                Private intLinesPrinted As Integer
                
                Private Sub PrintDocument1\_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
                
                    PrintHeadings(e)
                    PrintDetails(e)
                
                End Sub
                
                Private Sub PrintHeadings(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
                
                    Dim recMenu As New System.Drawing.Rectangle(40, 20, 100, 40)
                    Dim recQuantity As New System.Drawing.Rectangle(120, 20, 60, 40)
                    Dim recCost As New System.Drawing.Rectangle(180, 20, 60, 40)
                    Dim recTotal As New System.Drawing.Rectangle(260, 20, 80, 40)
                    Dim fntDefault As New System.Drawing.Font("Arial", 8, FontStyle.Underline)
                    Dim fmtCentre As New StringFormat
                    fmtCentre.Alignment = StringAlignment.Center
                    e.Graphics.DrawString("Menu Items", fntDefault, Brushes.Black, recMenu, fmtCentre)
                    e.Graphics.DrawString("Quantity", fntDefault, Brushes.Black, recQuantity, fmtCentre)
                    e.Graphics.DrawString("Cost Price", fntDefault, Brushes.Black, recCost, fmtCentre)
                    e.Graphics.DrawString("Total", fntDefault, Brushes.Black, recTotal, fmtCentre)
                
                End Sub
                
                Private Sub PrintDetails(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
                
                    Dim strMenu As New StringBuilder("")
                    Dim strQuantity As New StringBuilder("")
                    Dim strCost As New StringBuilder("")
                    Dim strTotal As New StringBuilder("")
                    Dim recMenu As New System.Drawing.Rectangle(20, 80, 100, 600)
                    Dim recQuantity As New System.Drawing.Rectangle(120, 80, 60, 600)
                    Dim recCost As New System.Drawing.Rectangle(180, 80, 60, 600)
                    Dim recTotal As New System.Drawing.Rectangle(240, 80, 80, 600)
                    Dim intLastLine As Integer = intLinesPrinted + intLinesPerPage
                    If intLastLine >= items.Length - 1 Then
                        intLastLine = items.Length - 1
                        e.HasMorePages = False
                    Else
                        e.HasMorePages = True
                    End If
                    Dim i As Integer
                    For i = intL
                
                L 1 Reply Last reply
                0
                • W Wayne Gaylard

                  This is how I do my report printing. I have adapted one of my reports to sort of get what you are looking for. I used a set of arrays to input the data, whereas you would obviously get it from the app.

                  Private items() As String = {"Cheeseburger", "Onion Rings", "Chicken Sandwich", "Milk Shake"}
                  Private costs() As Integer = {5, 6, 5, 2}
                  Private quantities() As Integer = {1, 2, 2, 2}
                  Private intLinesPerPage As Integer = 25
                  Private intLinesPrinted As Integer
                  
                  Private Sub PrintDocument1\_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
                  
                      PrintHeadings(e)
                      PrintDetails(e)
                  
                  End Sub
                  
                  Private Sub PrintHeadings(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
                  
                      Dim recMenu As New System.Drawing.Rectangle(40, 20, 100, 40)
                      Dim recQuantity As New System.Drawing.Rectangle(120, 20, 60, 40)
                      Dim recCost As New System.Drawing.Rectangle(180, 20, 60, 40)
                      Dim recTotal As New System.Drawing.Rectangle(260, 20, 80, 40)
                      Dim fntDefault As New System.Drawing.Font("Arial", 8, FontStyle.Underline)
                      Dim fmtCentre As New StringFormat
                      fmtCentre.Alignment = StringAlignment.Center
                      e.Graphics.DrawString("Menu Items", fntDefault, Brushes.Black, recMenu, fmtCentre)
                      e.Graphics.DrawString("Quantity", fntDefault, Brushes.Black, recQuantity, fmtCentre)
                      e.Graphics.DrawString("Cost Price", fntDefault, Brushes.Black, recCost, fmtCentre)
                      e.Graphics.DrawString("Total", fntDefault, Brushes.Black, recTotal, fmtCentre)
                  
                  End Sub
                  
                  Private Sub PrintDetails(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
                  
                      Dim strMenu As New StringBuilder("")
                      Dim strQuantity As New StringBuilder("")
                      Dim strCost As New StringBuilder("")
                      Dim strTotal As New StringBuilder("")
                      Dim recMenu As New System.Drawing.Rectangle(20, 80, 100, 600)
                      Dim recQuantity As New System.Drawing.Rectangle(120, 80, 60, 600)
                      Dim recCost As New System.Drawing.Rectangle(180, 80, 60, 600)
                      Dim recTotal As New System.Drawing.Rectangle(240, 80, 80, 600)
                      Dim intLastLine As Integer = intLinesPrinted + intLinesPerPage
                      If intLastLine >= items.Length - 1 Then
                          intLastLine = items.Length - 1
                          e.HasMorePages = False
                      Else
                          e.HasMorePages = True
                      End If
                      Dim i As Integer
                      For i = intL
                  
                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Aha, filling columns each with a string inside a rectangle. Interesting. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups