Printing text file

Chris-Ramsey

New member
Joined
Mar 28, 2008
Messages
4
Programming Experience
Beginner
Hi, Im sorry if there is a more specific place to put this but I am fairly new, both to this forum and to programming.

I have made an application very similar to MS notepad although you can have different fonts, colours etc. I have every button in my toolbar working apart from the print.

I managed to find some code on the web to help me with this. I am unable to print but it will print a blank page.

Here is the code:

VB.NET:
Private Sub PrintFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PrintToolStripButton.Click

PrintDialog1.Document = PrintDocument1
Dim strText As String = Me.TextBox1.Text
Dim myReader = New IO.StringReader(strText)
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
  Me.PrintDocument1.Print()
End If

End Sub

    Private Sub ThePrintDocument_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
        
Dim linesPerPage As Single = 0
Dim yPosition 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 printFont As Font = Me.TextBox1.Font
Dim myBrush As New SolidBrush(Color.Black)
Dim strText As String = Me.TextBox1.Text
Dim myReader = New IO.StringReader(strText)
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
While count < linesPerPage And Not ((line <= myReader.ReadLine()) Is Nothing) 
  ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, New StringFormat())
  count += 1
End While 
ev.HasMorePages = False
myBrush.Dispose()

End Sub

I am using textbox1 as my textbox. I also have a printdialog and printdocument control on my form. (PrintDialog1 and PrintDocument1)

Can anyone spot anything they think is wrong? Sorry, if i havent explained my problem clearly. :confused:

thanks in advance
 
Last edited:
There are several problems with that code, both with syntax, semantics and logic. Here's a "converted" version that works as what you posted where suppose to:
VB.NET:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
    Dim count As Integer = 0
    Dim leftMargin As Integer = ev.MarginBounds.Left
    Dim topMargin As Integer = ev.MarginBounds.Top
    Dim yPosition As Single = topMargin
    Dim printFont As Font = Me.TextBox1.Font
    Dim myBrush As New SolidBrush(Color.Black)
    Dim myReader = New IO.StringReader(Me.TextBox1.Text)
    Dim lineHeight As Single = printFont.GetHeight(ev.Graphics)
    Dim linesPerPage As Integer = ev.MarginBounds.Height \ lineHeight
    Dim line As String = myReader.ReadLine
    While count <= linesPerPage And Not line Is Nothing
        ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition)
        count += 1
        yPosition += lineHeight
        line = myReader.ReadLine
    End While
    myBrush.Dispose()
End Sub
Even though this "works" there are several problems with this also. It will not work for multiple pages. Texts lines doesn't wrap like they do in Textbox, but only draw to right end of paper and disappear (which also violates the page margin).

A better approach is to use the MeasureString method, it can return the number of characters of wrapped text that will fit in a rectangle (like MarginBounds), so it will be easy to index the text also for multiple pages. When drawing text to a rectangle instead of a location it will wrap within the given bounds.
VB.NET:
Private index As Integer 'set this to 0 before each new Print job!

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
    Dim measureString As String = Me.TextBox1.Text.Substring(index)
    Dim stringFont As Font = Me.TextBox1.Font
    Dim layoutSize As SizeF = e.MarginBounds.Size
    Dim sf As StringFormat = StringFormat.GenericTypographic
    Dim charactersFitted As Integer
    Dim linesFilled As Integer
    e.Graphics.MeasureString(measureString, stringFont, layoutSize, sf, charactersFitted, linesFilled)
    e.Graphics.DrawString(measureString.Substring(0, charactersFitted), stringFont, Brushes.Black, e.MarginBounds, sf)

    index += charactersFitted
    If index < Me.TextBox1.TextLength Then
        e.HasMorePages = True
    End If
End Sub
 
Thank you very much.

I can now see that my original code was quite a way off.

Thanks again for your help.
 
Last edited:
Back
Top