如何打印带有标签的文本文件?

时间:2011-04-19 10:40:21

标签: .net vb.net tabs drawstring

我正在尝试打印带有制表符的文本文件。问题是打印时不显示这些选项卡。这是我的代码:

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

要使其支持标签,我需要做些什么?

1 个答案:

答案 0 :(得分:1)

您没有给我们详细说明您的问题。我也假设streamToPrint变量属于StreamReader类型。:

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