469,306 Members | 1,901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

printing from a rich text box

can anybody help me print from a rich text box? i tried the way they showed
on the MSDN web page, but it did not work. I am using VB.net 2003...any help
would be appreciated
Nov 21 '05 #1
4 13127
hi this should help,

make a new class, call it TextPrint and then copy paste this:

'to actualy print something use this:
'Dim x As New TextPrint("my text")
'x.Print()

Imports System.Windows.Forms
Imports System.Drawing
Public Class TextPrint
Inherits Printing.PrintDocument
Private fntPrintFont As Font
Private strText As String
Public Sub New(ByVal Text As String)
MyBase.New()
strText = Text
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal Value As String)
strText = Value
End Set
End Property
Protected Overrides Sub OnBeginPrint(ByVal ev _
As Printing.PrintEventArgs)

MyBase.OnBeginPrint(ev)
If fntPrintFont Is Nothing Then
fntPrintFont = New Font("Times New Roman", 12)
End If
End Sub
Public Property Font() As Font
Get
Return fntPrintFont
End Get
Set(ByVal Value As Font)
fntPrintFont = Value
End Set
End Property
Protected Overrides Sub OnPrintPage(ByVal ev _
As Printing.PrintPageEventArgs)

MyBase.OnPrintPage(ev)
Static intCurrentChar As Integer
Dim intPrintAreaHeight, intPrintAreaWidth, _
intMarginLeft, intMarginTop As Integer
With MyBase.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - _
.Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - _
.Margins.Left - .Margins.Right
intMarginLeft = .Margins.Left 'X
intMarginTop = .Margins.Top 'Y
End With
If MyBase.DefaultPageSettings.Landscape Then
Dim intTemp As Integer
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = _
CInt(intPrintAreaHeight / Font.Height)
Dim rectPrintingArea As New RectangleF(intMarginLeft, _
intMarginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim objSF As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
ev.Graphics.MeasureString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
New SizeF(intPrintAreaWidth, _
intPrintAreaHeight), objSF, _
intCharsFitted, intLinesFilled)
ev.Graphics.DrawString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
Brushes.Black, rectPrintingArea, objSF)
intCurrentChar += intCharsFitted
If intCurrentChar < strText.Length Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Public Function UpgradeZeros(ByVal Input As Integer) As Integer
If Input = 0 Then
Return 1
Else
Return Input
End If
End Function
End Class
hth Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"iwdu15" <iw****@discussions.microsoft.com> schreef in bericht
news:02**********************************@microsof t.com...
can anybody help me print from a rich text box? i tried the way they showed on the MSDN web page, but it did not work. I am using VB.net 2003...any help would be appreciated

Nov 21 '05 #2
ng
Peter,

I found this very helpful, and interesting! But I have another problem
- I can't figure out how to send formatted text from a richtextbox.rtf
to the printer without getting the formatting codes printed. I remember
when sending columnar data to a printer was so easy! And Microsoft
documentation seems to be silent on this. Go figure!

Thanks for whatever you can offer.

T

Peter Proost wrote:
hi this should help,

make a new class, call it TextPrint and then copy paste this:

'to actualy print something use this:
'Dim x As New TextPrint("my text")
'x.Print()

Imports System.Windows.Forms
Imports System.Drawing
Public Class TextPrint
Inherits Printing.PrintDocument
Private fntPrintFont As Font
Private strText As String
Public Sub New(ByVal Text As String)
MyBase.New()
strText = Text
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal Value As String)
strText = Value
End Set
End Property
Protected Overrides Sub OnBeginPrint(ByVal ev _
As Printing.PrintEventArgs)

MyBase.OnBeginPrint(ev)
If fntPrintFont Is Nothing Then
fntPrintFont = New Font("Times New Roman", 12)
End If
End Sub
Public Property Font() As Font
Get
Return fntPrintFont
End Get
Set(ByVal Value As Font)
fntPrintFont = Value
End Set
End Property
Protected Overrides Sub OnPrintPage(ByVal ev _
As Printing.PrintPageEventArgs)

MyBase.OnPrintPage(ev)
Static intCurrentChar As Integer
Dim intPrintAreaHeight, intPrintAreaWidth, _
intMarginLeft, intMarginTop As Integer
With MyBase.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - _
.Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - _
.Margins.Left - .Margins.Right
intMarginLeft = .Margins.Left 'X
intMarginTop = .Margins.Top 'Y
End With
If MyBase.DefaultPageSettings.Landscape Then
Dim intTemp As Integer
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = _
CInt(intPrintAreaHeight / Font.Height)
Dim rectPrintingArea As New RectangleF(intMarginLeft, _
intMarginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim objSF As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
ev.Graphics.MeasureString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
New SizeF(intPrintAreaWidth, _
intPrintAreaHeight), objSF, _
intCharsFitted, intLinesFilled)
ev.Graphics.DrawString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
Brushes.Black, rectPrintingArea, objSF)
intCurrentChar += intCharsFitted
If intCurrentChar < strText.Length Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Public Function UpgradeZeros(ByVal Input As Integer) As Integer
If Input = 0 Then
Return 1
Else
Return Input
End If
End Function
End Class
hth Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"iwdu15" <iw****@discussions.microsoft.com> schreef in bericht
news:02**********************************@microso ft.com...

can anybody help me print from a rich text box? i tried the way they

showed

on the MSDN web page, but it did not work. I am using VB.net 2003...any

help

would be appreciated


Nov 21 '05 #3
Hi these links should help,

the first one has a nice working example, but it's C sharp, the second one does the same thing in VB, but no example

http://www.codeguru.com/Csharp/Cshar...icle.php/c4781

http://msdn.microsoft.com/library/de...ichTextBox.asp

hth Greetz Peter


--
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

"ng" <t_*****@yahoo.com> schreef in bericht news:6l******************@bignews5.bellsouth.net.. .
Peter,

I found this very helpful, and interesting! But I have another problem - I can't figure out how to send formatted text from a richtextbox.rtf to the printer without getting the formatting codes printed. I remember when sending columnar data to a printer was so easy! And Microsoft documentation seems to be silent on this. Go figure!

Thanks for whatever you can offer.

T

Peter Proost wrote:

hi this should help,

make a new class, call it TextPrint and then copy paste this:

'to actualy print something use this:
'Dim x As New TextPrint("my text")
'x.Print()

Imports System.Windows.Forms
Imports System.Drawing
Public Class TextPrint
Inherits Printing.PrintDocument
Private fntPrintFont As Font
Private strText As String
Public Sub New(ByVal Text As String)
MyBase.New()
strText = Text
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal Value As String)
strText = Value
End Set
End Property
Protected Overrides Sub OnBeginPrint(ByVal ev _
As Printing.PrintEventArgs)

MyBase.OnBeginPrint(ev)
If fntPrintFont Is Nothing Then
fntPrintFont = New Font("Times New Roman", 12)
End If
End Sub
Public Property Font() As Font
Get
Return fntPrintFont
End Get
Set(ByVal Value As Font)
fntPrintFont = Value
End Set
End Property
Protected Overrides Sub OnPrintPage(ByVal ev _
As Printing.PrintPageEventArgs)

MyBase.OnPrintPage(ev)
Static intCurrentChar As Integer
Dim intPrintAreaHeight, intPrintAreaWidth, _
intMarginLeft, intMarginTop As Integer
With MyBase.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - _
.Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - _
.Margins.Left - .Margins.Right
intMarginLeft = .Margins.Left 'X
intMarginTop = .Margins.Top 'Y
End With
If MyBase.DefaultPageSettings.Landscape Then
Dim intTemp As Integer
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = _
CInt(intPrintAreaHeight / Font.Height)
Dim rectPrintingArea As New RectangleF(intMarginLeft, _
intMarginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim objSF As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
ev.Graphics.MeasureString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
New SizeF(intPrintAreaWidth, _
intPrintAreaHeight), objSF, _
intCharsFitted, intLinesFilled)
ev.Graphics.DrawString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
Brushes.Black, rectPrintingArea, objSF)
intCurrentChar += intCharsFitted
If intCurrentChar < strText.Length Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Public Function UpgradeZeros(ByVal Input As Integer) As Integer
If Input = 0 Then
Return 1
Else
Return Input
End If
End Function
End Class
hth Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"iwdu15" <iw****@discussions.microsoft.com> schreef in bericht
news:02**********************************@microsof t.com...
can anybody help me print from a rich text box? i tried the way they
showed
on the MSDN web page, but it did not work. I am using VB.net 2003...any
help
would be appreciated




Nov 21 '05 #4
ng
Thank you, Peter. These are both very instructional. I'm learning C#
and VB.Net.

T
Peter Proost wrote:
Hi these links should help,

the first one has a nice working example, but it's C sharp, the second
one does the same thing in VB, but no example

http://www.codeguru.com/Csharp/Cshar...icle.php/c4781

http://msdn.microsoft.com/library/de...ichTextBox.asp

hth Greetz Peter


--
Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe trying
to produce bigger and better idiots. So far, the Universe is winning.

"ng" <t_*****@yahoo.com <mailto:t_*****@yahoo.com>> schreef in
bericht news:6l******************@bignews5.bellsouth.net.. .
Peter,

I found this very helpful, and interesting! But I have another
problem - I can't figure out how to send formatted text from a
richtextbox.rtf to the printer without getting the formatting
codes printed. I remember when sending columnar data to a printer
was so easy! And Microsoft documentation seems to be silent on
this. Go figure!

Thanks for whatever you can offer.

T

Peter Proost wrote:
hi this should help,

make a new class, call it TextPrint and then copy paste this:

'to actualy print something use this:
'Dim x As New TextPrint("my text")
'x.Print()

Imports System.Windows.Forms
Imports System.Drawing
Public Class TextPrint
Inherits Printing.PrintDocument
Private fntPrintFont As Font
Private strText As String
Public Sub New(ByVal Text As String)
MyBase.New()
strText = Text
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal Value As String)
strText = Value
End Set
End Property
Protected Overrides Sub OnBeginPrint(ByVal ev _
As Printing.PrintEventArgs)

MyBase.OnBeginPrint(ev)
If fntPrintFont Is Nothing Then
fntPrintFont = New Font("Times New Roman", 12)
End If
End Sub
Public Property Font() As Font
Get
Return fntPrintFont
End Get
Set(ByVal Value As Font)
fntPrintFont = Value
End Set
End Property
Protected Overrides Sub OnPrintPage(ByVal ev _
As Printing.PrintPageEventArgs)

MyBase.OnPrintPage(ev)
Static intCurrentChar As Integer
Dim intPrintAreaHeight, intPrintAreaWidth, _
intMarginLeft, intMarginTop As Integer
With MyBase.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - _
.Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - _
.Margins.Left - .Margins.Right
intMarginLeft = .Margins.Left 'X
intMarginTop = .Margins.Top 'Y
End With
If MyBase.DefaultPageSettings.Landscape Then
Dim intTemp As Integer
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = _
CInt(intPrintAreaHeight / Font.Height)
Dim rectPrintingArea As New RectangleF(intMarginLeft, _
intMarginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim objSF As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
ev.Graphics.MeasureString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
New SizeF(intPrintAreaWidth, _
intPrintAreaHeight), objSF, _
intCharsFitted, intLinesFilled)
ev.Graphics.DrawString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
Brushes.Black, rectPrintingArea, objSF)
intCurrentChar += intCharsFitted
If intCurrentChar < strText.Length Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Public Function UpgradeZeros(ByVal Input As Integer) As Integer
If Input = 0 Then
Return 1
Else
Return Input
End If
End Function
End Class
hth Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"iwdu15" <iw****@discussions.microsoft.com> schreef in bericht
news:02**********************************@micros oft.com...

can anybody help me print from a rich text box? i tried the way they

showed

on the MSDN web page, but it did not work. I am using VB.net 2003...any

help

would be appreciated


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by PC User | last post: by
1 post views Thread by Peter | last post: by
4 posts views Thread by ng | last post: by
3 posts views Thread by Stanley | last post: by
reply views Thread by Howard | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.