Printing from a rich text box
For this you can use the following code-snippet.
'VB
'Open file to RTB and print RTB
'To print multiple pages we have to create virtual
'page of text called
PrintPage and then add text to it
'until page is full
'Controls: OpenFileDialog1, PrintDocument1,
'PrintDialog1
----------------------------------------------------
Imports System.IO 'for FileStream class
Imports System.Drawing.Printing
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private PrintPageSettings As New PageSettings()
Private StringToPrint As String
Private PrintFont As New Font("Arial", 10)
Private Sub btnOpen_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles btnOpen.Click
Dim FilePath As String
'Display Open dialog box and select text file
OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"
OpenFileDialog1.ShowDialog()
'If Cancel button not selected, load FilePath var
If OpenFileDialog1.FileName <> "" Then
FilePath = OpenFileDialog1.FileName
Try
'Read text file and load into RichTextBox1
Dim MyFileStream As New _
FileStream(FilePath, FileMode.Open)
RichTextBox1.LoadFile(MyFileStream, _
RichTextBoxStreamType.PlainText)
MyFileStream.Close()
'Initialize string to print
StringToPrint = RichTextBox1.Text
'Enable Print button
btnPrint.Enabled = True
Catch ex As Exception
'display error messages if they appear
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Private Sub btnPrint_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles btnPrint.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = _
PrintPageSettings
'Specify document for print dialog box
'and show
StringToPrint = RichTextBox1.Text
PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = _
PrintDialog1.ShowDialog()
'If click OK, print document to printer
If result = DialogResult.OK Then
PrintDocument1.Print()
End If
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(ByVal _
sender As System.Object, ByVal e As _
System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
Dim numChars As Integer
Dim numLines As Integer
Dim stringForPage As String
Dim strFormat As New StringFormat()
'Based on page setup, define drawable rectangle
Dim rectDraw As New _
RectangleF(e.MarginBounds.Left, _
e.MarginBounds.Top, e.MarginBounds.Width, _
e.MarginBounds.Height)
'Define area to determine how much text can fit
'on a page
'Make height one line shorter to ensure text
'doesn't clip
Dim sizeMeasure As New _
SizeF(e.MarginBounds.Width, e.MarginBounds.Height _
- PrintFont.GetHeight(e.Graphics))
'When drawing long strings, break between words
strFormat.Trimming = StringTrimming.Word
'Compute how many chars and lines can fit
'based on sizeMeasure
e.Graphics.MeasureString(StringToPrint, _
PrintFont, sizeMeasure, strFormat, numChars, _
numLines)
'Compute string that will fit on a page
stringForPage = StringToPrint.Substring(0, _
numChars)
'Print string on current page
e.Graphics.DrawString(stringForPage, PrintFont, _
Brushes.Black, rectDraw, strFormat)
'If there is more text, indicate there are more
'pages
If numChars < StringToPrint.Length Then
'Subtract text from string that has been
'printed
StringToPrint = _
StringToPrint.Substring(numChars)
e.HasMorePages = True
Else
e.HasMorePages = False
'All text has been printed, so restore string
StringToPrint = RichTextBox1.Text
End If
End Sub
End Class
Posted by Xander Zelders

3 Comments:
i am sorry but if the((texttoprint)=richtextbox1.text)then bold, coloured etc. format are lost in printing.
Please give me solution to this problem
Thanx
Many thanks, worked great for what I needed.
The Alignment of the Text will be Lost.
For example I wrote a Poem Application that the odd lines are left aligned, and others are Right.
I want to print it exactly like my RichText Box in windows form.
Post a Comment
<< Home