Printing Text - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This tutorial will show you how to print the text for the notepad project.

This tutorial will continue the notepad tutorial

Open the notepad project and two menu items to the File Menu: Print Preview and Print:

Add PrintPreviewDialog , PrintDialog and PrintDocument from the toolbox to the form.

The following is the Print Preview click event code:

    Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
        PrintPreviewDialog1.Document = PrintDocument1
    End Sub

The following code handles the Print ment click event:

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        End If
    End Sub

Then add the following code that handles the printing document event:

 Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Static currentChar As Integer
        Static currentLine As Integer
        Dim textfont As Font = TextBox1.Font
        Dim h, w As Integer
        Dim left, top As Integer
        With PrintDocument1.DefaultPageSettings
            h = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            w = .PaperSize.Width - .Margins.Left - .Margins.Right
            left = PrintDocument1.DefaultPageSettings.Margins.Left
            top = PrintDocument1.DefaultPageSettings.Margins.Top
        End With
        e.Graphics.DrawRectangle(Pens.Blue, New Rectangle(left, top, w, h))
        If PrintDocument1.DefaultPageSettings.Landscape Then
            Dim a As Integer
            a = h
            h = w
            w = a
        End If
        Dim lines As Integer = CInt(Math.Round(h / textfont.Height))
        Dim b As New Rectangle(left, top, w, h)
        Dim format As StringFormat
        If Not TextBox1.WordWrap Then
            format = New StringFormat(StringFormatFlags.NoWrap)
            format.Trimming = StringTrimming.EllipsisWord
            Dim i As Integer
            For i = currentLine To Math.Min(currentLine + lines, TextBox1.Lines.Length - 1)
                e.Graphics.DrawString(TextBox1.Lines(i), textfont, Brushes.Black, New RectangleF(left, top + textfont.Height * (i - currentLine), w, textfont.Height), format)
            currentLine += lines
            If currentLine >= TextBox1.Lines.Length Then
                e.HasMorePages = False
                currentLine = 0
                e.HasMorePages = True
            End If
            Exit Sub
        End If
        format = New StringFormat(StringFormatFlags.LineLimit)
        Dim line, chars As Integer
        e.Graphics.MeasureString(Mid(TextBox1.Text, currentChar + 1), textfont, New SizeF(w, h), format, chars, line)
        If currentChar + chars < TextBox1.Text.Length Then
            If TextBox1.Text.Substring(currentChar + chars, 1) <> " " And TextBox1.Text.Substring(currentChar + chars, 1) <> vbLf Then
                While chars > 0
                    TextBox1.Text.Substring(currentChar + chars, 1)
                    TextBox1.Text.Substring(currentChar + chars, 1)
                    chars -= 1
                End While
                chars += 1
            End If
        End If
        e.Graphics.DrawString(TextBox1.Text.Substring(currentChar, chars), textfont, Brushes.Black, b, format)
        currentChar = currentChar + chars
        If currentChar < TextBox1.Text.Length Then
            e.HasMorePages = True
            e.HasMorePages = False
            currentChar = 0
        End If
    End Sub

Try the program and enjoy!!

Share This

Home | About | Contact | Privacy Policy

Copyright 2017 - All Rights Reserved.