I have written software that creates a text file, the text file contains colums of data. When I view and print the text file using notepad all the colums line up correctly. I am now trying to print the text file using a button on my form but every time I print it all the colums are no longer lined up :-(

This is the code that I'm using to do the print.

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
       
        Dim LinesPerPage As Single = 0
        Dim yPosition As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = e.MarginBounds.Left
        Dim topMargin As Single = e.MarginBounds.Top
        Dim strLine As String = Nothing
        Dim printFont As Font = Me.lblFadeRate.Font
        Dim myBrush As New SolidBrush(Color.Black)

        'Work out the number of lines per page, using the MarginBounds.
        LinesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)

        'Iterate over the string using the StringBuilder, printing each line.
        strLine = MyStreamReader.ReadLine
        While count < LinesPerPage
            'Calculate the next line position based on the height on the font according
            'to the printing device
            yPosition = (topMargin + (count * printFont.GetHeight(e.Graphics)))

            'draw the next line in the stream
            e.Graphics.DrawString(strLine, printFont, myBrush, leftMargin, yPosition, New StringFormat())
            count += 1
            strLine = MyStreamReader.ReadLine
        End While

        'check to see if there is another page left to print
        If MyStreamReader.EndOfStream = False Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            MyStreamReader.Close()  'Close stream reader
        End If
    End Sub

Am I missing a setting somewhere???


Thanks for any help you can give me

Simon

Dani AI

Generated

had the right symptom: text looks column-aligned in Notepad but shifts when printed from code. That usually comes down to font metrics and tab/space handling. correctly pointed toward a monospaced font — monospaced characters keep columns stable — and was right to check that printing is happening on the same system/printer (different drivers or DPI can also change spacing).

Why it happens (brief): proportional fonts give different horizontal widths to different characters, and tab expansion can differ between Notepad and GDI+ drawing. The quickest, most reliable fix is to print with a monospaced font. If you need to control layout more precisely, either set explicit tab stops or measure-and-place each column yourself.

Example approaches (VB.NET):

  • Force a monospaced font when printing:
Dim printFont As New Font("Courier New", 10)
  • For tab-delimited text, split and draw each column at measured X offsets:
Dim parts As String() = line.Split(vbTab)
Dim x As Single = leftMargin
For Each p As String In parts
    e.Graphics.DrawString(p, printFont, Brushes.Black, x, yPosition)
    x += e.Graphics.MeasureString(p, printFont).Width + 6
Next

If tabs are important and you prefer a single DrawString call, define tab stops with StringFormat.SetTabStops (see the docs at StringFormat.SetTabStops). For more accurate width measurements, use Graphics.MeasureString or the typographic string format options as described in the Graphics.DrawString documentation. For stable, production-ready output consider generating a fixed-layout (PDF/report) if exact alignment across printers is required.

Recommended Answers

All 6 Replies

Are you printing from the same system on which your code resides.

Are you printing from the same system on which your code resides.

Are yo sure the printer is not a network printer and the font of notepad is same every where.

Hi Simon,

make the Font of the NotePad "Courier New", it is a UniDistant font (all fonts acquire same space on screen/media), ur texts will look like Columns..

REgards
Veena

Hi Simon,

make the Font of the NotePad "Courier New", it is a UniDistant font (all fonts acquire same space on screen/media), ur texts will look like Columns..

REgards
Veena

Thanks for that :) It worked a treat

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.