How NOT to print 'White Spaces' - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How NOT to print 'White Spaces'

marius40marius40 Posts: 42Member
Here is the code i using to print a report. The report is print fine. BUT , the problem is that the 3rd column is not indent correctly. I assume that it has to do with WHITE SPACES. I tried to TRIM and PAD, but not luck. How can i make sure that my columns are indented corectly?

[code] private Sub Doc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim doc As TextDocument = DirectCast(sender, TextDocument)
Dim lineHeight As Single = Font.GetHeight(e.Graphics)
Dim x As Single = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
doc.PageNumber += 1
Dim fnt4 As New Font("Arial", 9, FontStyle.Italic)
e.Graphics.DrawString(TextBox1.Text & vbTab & vbTab & vbTab & DateTimePicker1.Text, fnt4, Brushes.Black, x, y)
y += lineHeight
y += lineHeight
Dim fnt2 As New Font("Arial", 14, FontStyle.Underline)
e.Graphics.DrawString("TEST RESULT : " & Trim(PUBMYLECTURER) & vbTab & " CAMPUS: " & PUBMYCOLLEGE, fnt2, Brushes.Black, x, y)
y += lineHeight
y += lineHeight
Dim fnt3 As New Font("Arial", 12, FontStyle.Bold)
e.Graphics.DrawString(" GROUP :" & PUBCLASSGROUP & " FOR: " & pagetesttitle, fnt3, Brushes.Black, x, y)
y += lineHeight
Using fnt As New Font("Arial", 10)
While ((y + lineHeight) < e.MarginBounds.Bottom And doc.Offset <= doc.Text.GetUpperBound(0))
e.Graphics.DrawString("______________________________________________________________________________________", Font, Brushes.Black, x, y)
y += lineHeight
Dim R As New RectangleF(x, y, 80, 80)
e.Graphics.DrawString(doc.Text(doc.Offset), Font, Brushes.Black, x, y)
doc.Offset += 1
y += lineHeight
End While
y += lineHeight
y += lineHeight
Dim fnt5 As New Font("Arial", 9, FontStyle.Italic)
e.Graphics.DrawString(TextBox2.Text, fnt5, Brushes.Black, x, y)
If doc.Offset < doc.Text.GetUpperBound(0) Then
e.HasMorePages = True ' There is still at least one more page. Signal this event to fire again.
Else
doc.Offset = 0 ' Printing is complete.
End If
End Using
End Sub [/code]


[code]
Sub PRINTTEST(ByVal CLASSGROUP, ByVal testtype)
' Create a document with 100 lines.
Dim studentarr, studentcode2 As String
Dim a As Integer = 1
Dim testamount As Integer
PUBCLASSGROUP = CLASSGROUP
'con.Open()
If testtype = "test1" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test1 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest1
End If
If testtype = "test2" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test2 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest2
End If
If testtype = "test3" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test3 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest3
End If
If testtype = "test4" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test4 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest4
End If
If testtype = "test5" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test5 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest5
End If
If testtype = "test6" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test6 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest6
End If
If testtype = "test7" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test7 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest7
End If
If testtype = "test8" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test8 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest8
End If
If testtype = "test9" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test9 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest9
End If
If testtype = "test10" Then
adp2 = New OleDbDataAdapter("select studentname, studentcode,test10 from tests where studentclass = '" & CLASSGROUP & "' order by studentname ", con)
pagetesttitle = pubmytest10
End If
ds2 = New DataSet
adp2.Fill(ds2, "s")
Dim dr As DataRow
Dim amountrows As Integer = ds2.Tables(0).Rows.Count
Dim printText As String() = New String(amountrows) {}
'' Find the longest name
Dim longname As String
Dim maxname As Integer = 0
For Each dr In ds2.Tables(0).Rows()
longname = dr.Item(0)
If maxname < Len(longname) Then maxname = Len(longname)
Next

For Each dr In ds2.Tables(0).Rows()
printText(a) = a.ToString.PadRight(3, " "c)

studentarr = dr.Item(0)
studentcode2 = dr.Item(1)
testamount = Val(dr.Item(2))
printText(a) += studentcode2 & vbTab
printText(a) += (Trim(studentarr) + Space(maxname - Len(studentarr) + 1)).PadRight(2)
printText(a) += vbTab & vbTab & testamount
a = a + 1
Next
' The Printing Starts Here
Dim doc As New TextDocument(printText)
AddHandler doc.PrintPage, AddressOf Doc_PrintPage
Dim dlgSettings As New PrintDialog
dlgSettings.Document = doc
Dim dlgPreview As New PrintPreviewDialog
dlgPreview.Document = doc
If dlgSettings.ShowDialog = Windows.Forms.DialogResult.OK Then
dlgPreview.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowOnly
If dlgPreview.ShowDialog = Windows.Forms.DialogResult.OK Then
doc.Print()
End If
End If
End Sub [/code]


Sign In or Register to comment.