Results 1 to 15 of 15

Thread: [RESOLVED] Text File Column Alignment While Printing

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] Text File Column Alignment While Printing

    I have got my text file sorted into 5 columns and it looks good



    using this code
    Code:
                Dim sw As New IO.StreamWriter("C:\StickFrameExpress\Jobs\" & Me.lblJobNumber.Text & ".txt", False)
                'write blanks lines
                sw.WriteLine("")
                sw.WriteLine("")
                'write job number and date
                sw.WriteLine(ControlChars.Tab & "Job Number:" & "  " & Me.lblJobNumber.Text & "                                     " & Now)
                sw.WriteLine("")
                sw.WriteLine("")
                sw.WriteLine("")
                'header
                sw.WriteLine(ControlChars.Tab & "Name".PadRight(20, " "c) & _
                                  ControlChars.Tab & "Material".PadRight(50, " "c) & _
                                  ControlChars.Tab & "Length".PadRight(15, " "c) & _
                                  ControlChars.Tab & "Qty".PadRight(8, " "c) & _
                                  ControlChars.Tab & "Units".PadRight(8, " "c)) 
                   sw.WriteLine("")
                Dim i As Int16
                With Me.ListView1
                    For i = 0 To CShort(.Items.Count - 1)
                        If .Items(i).Text <> "" Then
                            sw.WriteLine(ControlChars.Tab & .Items(i).Text.PadRight(20, " "c) & _
                                              ControlChars.Tab & .Items(i).SubItems(1).Text.PadRight(50, " "c) & _
                                              ControlChars.Tab & .Items(i).SubItems(2).Text.PadRight(15, " "c) & _
                                              ControlChars.Tab & .Items(i).SubItems(3).Text.PadRight(8, " "c) & _
                                              ControlChars.Tab & .Items(i).SubItems(4).Text.PadRight(8, " "c)) 
                          sw.writeLine("")
    When i print the file using the file > print from the notepad program the columns do not appear as they do on the screen..

    They no longer line up.
    How can i overcome this?

    regards
    toe
    Last edited by toecutter; Apr 13th, 2008 at 01:01 AM. Reason: Title

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Text File Column Alignment While Printing

    I should have said this in the other thread. Don't mix tabs and padding.

    I'd switch the font to mono-spaced (Courier) with padding or figure out the correct tab stops, which won't be the same in a textbox and printer.

    Courier is pretty common in printers.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Text File Column Alignment While Printing

    Am i correct in saying PadRight will move the next string so many spaces to the right?

    If so i dont think think this method will work due to when the string increases in chartacters the columns do not line up which seems to be happing no matter how i try to implment it.
    Code:
                sw.WriteLine("".PadRight(20, " "c) & "Name".PadRight(20, " "c) & _
                            "Material".PadRight(120, " "c) & _
                            "Length".PadRight(15, " "c) & _
                            "Qty".PadRight(8, " "c) & _
                            "Units".PadRight(8, " "c)) ' & _
             
                sw.WriteLine("")
                Dim i As Int16
                With Me.ListView1
                    For i = 0 To CShort(.Items.Count - 1)
                        If .Items(i).Text <> "" Then
                            sw.WriteLine("".PadRight(20, " "c) & .Items(i).Text.PadRight(20, " "c) & _
                            .Items(i).SubItems(1).Text.PadRight(120, " "c) & _
                            .Items(i).SubItems(2).Text.PadRight(15, " "c) & _
                            .Items(i).SubItems(3).Text.PadRight(8, " "c) & _
                            .Items(i).SubItems(4).Text.PadRight(8, " "c)) ' & _
                             sw.WriteLine("")
                        End If
                    Next
                End With

  4. #4

    Re: Text File Column Alignment While Printing

    Code:
            Dim aString As String = "1234", eString As String = "END"
            Debug.WriteLine("Debug Output Follows")
            For padCtr As Integer = 1 To 10
                Debug.WriteLine(aString.PadRight(aString.Length + padCtr, "*") & eString)
            Next
    Debug Output Follows
    1234*END
    1234**END
    1234***END
    1234****END
    1234*****END
    1234******END
    1234*******END
    1234********END
    1234*********END
    1234**********END

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Text File Column Alignment While Printing

    This is the description for String.PadRight(Int32) method from MSDN: Left-aligns the characters in this string, padding with spaces on the right, for a specified total length.

    So if you want your data written to a text file to line up in columns, you'd define the size of each column (in term of character count) then use String.PadRight() on each and every field (it does looks like you're already doing this). The next step is to use a fix-width font. Since this is a text file, you simply open it in notepad and change the font to Courrier or Courrier New. You'll see that you columns are lined up nice and neat. Now you're ready to print. However, if the length of your lines are longer than the width of the printable area, some wrapping will occur.

  6. #6

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Text File Column Alignment While Printing

    I tried what you have said and it still wont line up



    As you can see, as the text string increase in characters the columns are moved out of alignment
    Last edited by toecutter; Apr 15th, 2008 at 06:26 AM.

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Text File Column Alignment While Printing

    Quote Originally Posted by toecutter
    I tried what you have said and it still wont line up



    As you can see, as the text string increase in characters the columns are moved out of alignment
    You are NOT using fixed width fonts, are you?

  8. #8

    Re: Text File Column Alignment While Printing

    Lucida
    Courier New

    Test > < ? / (:987654321 Courier
    Last edited by ttensabd; Apr 15th, 2008 at 08:16 AM.

  9. #9
    Lively Member
    Join Date
    Jan 2007
    Location
    Constanta,Romania
    Posts
    71

    Re: Text File Column Alignment While Printing

    Quote Originally Posted by toecutter
    I have got my text file sorted into 5 columns and it looks good
    When i print the file using the file > print from the notepad program the columns do not appear as they do on the screen..

    They no longer line up.
    How can i overcome this?

    regards
    toe
    Just an idea : why don' you use RichTextBox control to display your text file,set RTB font to "Courier" and a button to print ... use .NET for that too :
    vb.NET Code:
    1. Dim printFont As New Font("Courier New", 9)
    2. .....
    3. e.Graphics.DrawString(mytextString, printFont, Brushes.Black, leftMargin, yPos,  _
    4.   New StringFormat())
    Last edited by ionut_y; Apr 15th, 2008 at 11:32 PM.

  10. #10

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Text File Column Alignment While Printing

    Quote Originally Posted by stanav
    You are NOT using fixed width fonts, are you?
    I have set the font of the listview to courier, but it seems thats not what you mean?
    When i open the textfile it is set to couier as well...

    Code:
    What are monospaced fonts you ask? From Xerox:
    > Monospace fonts (Such as Courier or LetterGothic), or "fixed pitch" fonts, contain characters that all have the same character width
    @ ionut_y, thanks for the idea but ill try and get this to work first and then ill give a RTB a go.

  11. #11

    Re: Text File Column Alignment While Printing

    You are writing the data to a text file?

    if yes, Have you opened it, did a select all, set font to Courier? Then printed?

  12. #12

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Text File Column Alignment While Printing

    Quote Originally Posted by ttensabd
    You are writing the data to a text file?

    if yes, Have you opened it, did a select all, set font to Courier? Then printed?
    Yes, i only had to set the font to courier once now all the text is automatically set to courier even when creating a new txt file.

  13. #13

    Re: Text File Column Alignment While Printing

    The screenshots you have posted aren't courier.

    Are the fields lined up in the text file? They should be if it is courier.

  14. #14
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Text File Column Alignment While Printing

    Quote Originally Posted by toecutter
    Yes, i only had to set the font to courier once now all the text is automatically set to courier even when creating a new txt file.
    That's right. Text files do not contains any format information so there's no way you can tell a text file to use a specific font family. The font setting you set in notepad is for the notepad application itself and that's why when you change the font, every other subsequence text files open in notepad will be displayed with that font.

  15. #15

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Text File Column Alignment While Printing

    Thanks all for the help on this but i have found it to difficult and decided to print from a richtextbox with a excellent Link Hack provided in this thread

    Code:
    Option Explicit On
    Option Strict On
    Imports System.IO
    Imports System.Drawing.Printing
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Dim sw As IO.StreamWriter
        Dim sr As IO.StreamReader
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'read a text file into richtextbox
            Dim myOutput As StreamReader
            Dim sValues As String
            myOutput = New StreamReader("C:\StickFrameExpress\Jobs\adadad.txt")
            sValues = myOutput.ReadToEnd
            Me.RichTextBox2.Text = sValues
            myOutput.Close()
        End Sub
    
        Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
            Me.PrintDocument1.DefaultPageSettings.Landscape = True
            If PrintDialog1.ShowDialog = DialogResult.OK Then
                'showDialog method makes the dialog box visible at run time
                With Me.PrintDocument1
                    .PrinterSettings.DefaultPageSettings.Landscape = True
                    .Print()
                End With
            End If
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            'PrintPage is the foundational printing event. This event gets fired for every
            ' page that will be printed
            Static intCurrentChar As Int32
            ' declaring a static variable to hold the position of the last printed char
            Dim font As New Font("Courier New", 8)
            ' initializing the font to be used for printing
            Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As Int32
            With PrintDocument1.DefaultPageSettings
                .Landscape = True
                ' initializing local variables that contain the bounds of the printing area rectangle
                PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
                PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
                ' initializing local variables to hold margin values that will serve
                ' as the X and Y coordinates for the upper left corner of the printing
                ' area rectangle.
                marginLeft = 20
                marginTop = 10
                ' X and Y coordinate
            End With
            Me.PrintDocument1.DefaultPageSettings.Landscape = True
            If PrintDocument1.DefaultPageSettings.Landscape Then
                Dim intTemp As Int32
                intTemp = PrintAreaHeight
                PrintAreaHeight = PrintAreaWidth
                PrintAreaWidth = intTemp
                ' if the user selects landscape mode, swap the printing area height and width
            End If
            Dim intLineCount As Int32 = CInt(PrintAreaHeight / font.Height)
            ' calculating the total number of lines in the document based on the height of
            ' the printing area and the height of the font
            Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, PrintAreaWidth, PrintAreaHeight)
            ' initializing the rectangle structure that defines the printing area
            Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
            'instantiating the StringFormat class, which encapsulates text layout information
            Dim intLinesFilled, intCharsFitted As Int32
            e.Graphics.MeasureString(Mid(RichTextBox2.Text, intCurrentChar + 1), font, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, intCharsFitted, intLinesFilled)
            ' calling MeasureString to determine the number of characters that will fit in
            ' the printing area rectangle
            e.Graphics.DrawString(Mid(RichTextBox2.Text, intCurrentChar + 1), font, Brushes.Black, rectPrintingArea, fmt)
            ' print the text to the page
            intCurrentChar += intCharsFitted
            Me.Close()
        End Sub
    End Class
    Hope it can help someone else

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width