Dim MyArr() As String
Dim font As Font = New Font("Arial", 12, FontStyle.Regular)
If TextBox5.Text <> Nothing Then
MyArr = Split(TextBox5.Text, vbCrLf, -1, 1)
Else
MsgBox("Please open a file frist! ", MsgBoxStyle.OkOnly)
Exit Sub
End If
If MyArr.Length > 0 Then
For i As Integer = 0 To MyArr.Length - 2
Trim(MyArr(i))
If (MyArr(i).Length > 8) Then
widthOfprint = (ev.PageBounds.Width - MyArr(i).Length * font.Size) / 2
Else
widthOfprint = (ev.PageBounds.Width - MyArr(i).Length * font.Size * 2.3) / 2
End If
ev.Graphics.DrawString(MyArr(i), font, Brushes.Black, widthOfprint, sngTopMargin)
sngTopMargin = sngTopMargin + font.GetHeight(ev.Graphics) * 1.5
ev.HasMorePages = False
Next
widthOfprint = (ev.PageBounds.Width - MyArr(MyArr.Length - 1).Length * font.Size)
ev.Graphics.DrawString(MyArr(MyArr.Length - 1), font, Brushes.Black, widthOfprint, sngTopMargin)
sngTopMargin = sngTopMargin + font.GetHeight(ev.Graphics) * 1.5
ev.HasMorePages = False
End If
End Sub
相应调试界面如下: