Is there any easy way to print an multiline rich textbox to an printer?
Printable View
Is there any easy way to print an multiline rich textbox to an printer?
Ive just done something similar, and let me tell you
there is no easy way
Add a PrintDocument to your program, also add PrintPreviewDialog so you can view a preview without actually printing it ( saves alot of paper and ink ).
On your print preview button, add this code
VB Code:
PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog()
And that will call
VB Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
which is where you do all your printing.
The most basic thing you could do is
VB Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage e.Graphics.DrawString(RichTextBox1.Text, RichTextBox1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top)
But that wont take into account the text fitting on the page. You have to manully determine when your text reaches the end of the paper width and height, to start printing on a new page.
And if your RichTextBox contains formmated text, you got some serious work involved.
Printing is no easy task IMO (Ive spent the last 2 days non stop coding working out a way to print a richtextbox when the contents was just plain text ( all the characters in there was the same font ) and my print function is about 100 lines!
Do a google search for printing in VB.NET, because printing is a big subject
Heres a picture of my printing a rich text box
The RichTextBox contained just normal text. I wrote a function to convert it into a layout so you could see the HEX values of each caracter.
With that I had to determine how many characters would fit in the width of the page, if it was too many, I had to reformat the layout to make sure everything fitted.
Once i worked that out I start printing the contents one line at a time, when I reached the bottom of the page, I had to goto a new page, reset any position variables I was using, and continue printing off where I left off.
All that while taking into account, paper size, margin size, landscape or portrait and fotn size.
My print function now is an extremly complicated function and was very hard to do. But after all my problems fitted everything on, I think the headaches have paid off :)
If your still interested, i just came across this which looks like an easy way of doing it
http://msdn.microsoft.com/library/de...ichTextBox.asp
Code:'print the document
On Error Resume Next
If ActiveForm Is Nothing Then Exit Sub
With dlgCommonDialog
.DialogTitle = "Print"
.CancelError = True
.Flags = cdlPDReturnDC + cdlPDNoPageNums
If ActiveForm.rtfText.SelLength = 0 Then
.Flags = .Flags + cdlPDAllPages
Else
.Flags = .Flags + cdlPDSelection
End If
.ShowPrinter
Me.ActiveForm.Timer10.Enabled = True
If Err <> MSComDlg.cdlCancel Then
ActiveForm.rtfText.SelPrint .hdc
End If
End With