0

I am trying to print a text file with tab characters in it. The problem is that these tabs are not shown when printing. Here is my code:

Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
    Dim linesPerPage As Single = 0
    Dim yPos As Single = 0
    Dim count As Integer = 0
    Dim leftMargin As Single = ev.MarginBounds.Left
    Dim topMargin As Single = ev.MarginBounds.Top
    Dim line As String = Nothing

    ' Calculate the number of lines per page.
    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

    ' Iterate over the file, printing each line.
    While count < linesPerPage
      line = streamToPrint.ReadLine()
      If line Is Nothing Then
        Exit While
      End If
      yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
      ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _
        yPos, New StringFormat())
      count += 1
    End While

    ' If more lines exist, print another page.
    If Not (line Is Nothing) Then
      ev.HasMorePages = True
    Else
      ev.HasMorePages = False
    End If
End Sub

What do I need to do to make it support tabs?

ckittel
  • 6,478
  • 3
  • 41
  • 71
serializer
  • 1,003
  • 2
  • 13
  • 27
  • Have you tried using the `\t` for a tab? – Tom Gullen Apr 19 '11 at 10:44
  • Can you be more specific what you mean by not showing tabs? How do tab-separated characters appear on the printed output: with no whitespace, with a substitution character, etc? How do you want them to show? What is the runtime type of "streamToPrint"? Have you inspected the return from ReadLine() to see if it contains tab chars? – mcw Apr 19 '11 at 14:08

1 Answers1

1

you did not give us a thourough specification of your problem.Also im assuming that the streamToPrint variable is of the StreamReader type.try this:

Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
Dim myStringFormat As New StringFormat
Dim tabStops As Single() = {150.0F, 100.0F, 100.0F}
myStringFormat.SetTabStops(0.0F, tabStops)
'The above two line can be changed to the following:
'myStringFormat.SetTabStops(0.0F, {150.0F, 100.0F, 100.0F})
'your call
' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

' Iterate over the file, printing each line.
While count < linesPerPage
  line = streamToPrint.ReadLine()
  If line Is Nothing Then
    Exit While
  End If
  yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
  ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _
    yPos, myStringFormat)
  count += 1
End While

' If more lines exist, print another page.
If Not (line Is Nothing) Then
  ev.HasMorePages = True
Else
  ev.HasMorePages = False
End If
End Sub
Qqbt
  • 802
  • 2
  • 8
  • 33