[Original] solutions for printing RichTextBox

Source: Internet
Author: User

In practical applications, we often encounter the situation of printing text boxes. Self-wrapping is a primary problem that must be solved. At first, I didn't quite understand the measurestring function and took a lot of detours, the regular expression is used for single-word matching, and the execution efficiency is low and the effect is not ideal.
Later, I posted a post to solve the problem, and I was familiar with it. So I wrote the code for later users to study.
'================================================ =====
'Power by: landlordh
'Print RichTextBox or mulitline textbox
'================================================ =====

'Put the pdoc (printdocument control), word (RichTextBox Control or Textbox Control) in the form first)
Private intcurrentchar as integer

Private sub pdoc_printpage (byval sender as object, byval e as system. Drawing. Printing. printpageeventargs) handles pdoc. printpage
Try
Dim G as graphics
G = E. Graphics
Dim pen as new drawing. Pen (color. Black, 3)
Dim font as new font ("Arial", 9)
Dim font1 as new font ("Arial", 12)
Dim font2 as new font ("Arial", 10)
Dim intprintareaheight as integer
Dim intprintareawidth as integer
Intprintareaheight = pdoc. defaultpagesettings. papersize. Height-pdoc. defaultpagesettings. margins. Top-pdoc. defaultpagesettings. margins. Bottom
Intprintareawidth = pdoc. defaultpagesettings. papersize. Width-pdoc. defaultpagesettings. margins. Left-pdoc. defaultpagesettings. margins. Right
If pdoc. defaultpagesettings. Landscape then
Dim inttemp as integer = intprintareaheight
Intprintareaheight = intprintareawidth
Intprintareawidth = inttemp
End if
'Dim intlinecount as integer = ctype (intprintareaheight/font. Height), integer)
Dim rectprintingarea as rectanglef = new rectanglef (pdoc. defaultpagesettings. margins. Left, pdoc. defaultpagesettings. margins. Top, intprintareawidth, intprintareaheight)
Dim FMT as stringformat = new stringformat (stringformatflags. linelimit)
Dim intlinesfilled as integer
Dim intcharsfitted as integer
'Draw page title
G. drawstring ("topic:" & topic. Text, font1, brushes. Black, 0, 10)
G. drawline (pen, 0, 12 + font1.height, intprintareawidth, 17 + font1.height)
G. measurestring (word. Text. substring (intcurrentchar), Font, new sizef (intprintareawidth, intprintareaheight), FMT, intcharsfitted, intlinesfilled)
G. drawstring (word. Text. substring (intcurrentchar), Font, brushes. Black, rectprintingarea)
Intcurrentchar + = intcharsfitted
Intlinecount + = intlinesfilled
G. drawline (pen, 0, 12 + font1.height + intprintareaheight + 6, intprintareawidth, 12 + font1.height + intprintareaheight + 11)
G. drawstring ("power by: landlordaspx", font2, brushes. Black, 625, 12 + font1.height + intprintareaheight + 12)
'G. drawstring ("Total (" & pdoc. printersettings. topage & ") page", font2, brushes. Black, 320, 12 + font1.height + intprintareaheight + 12)
If intcurrentchar <(word. Text. Length-1) then
E. hasmorepages = true
Else
E. hasmorepages = false
Intcurrentchar = 0
End if
Catch ex as exception
MessageBox. show ("the following error occurs during system execution:" & CHR (10) & "Error Source:" & Ex. source. tostring & CHR (10) & "error message:" & Ex. message. tostring & CHR (10) & CHR (10) & "Please record the error message and contact the administrator. "," System prompt ")
End try
End sub

Private sub printing ()
Dim printdialog1 as new printdialog
Printdialog1.document = pdoc
Printdialog1.allowselection = true
Printdialog1.allowprinttofile = true
Printdialog1.allowsomepages = true
Printdialog1.printtofile = false
If printdialog1.showdialog () = dialogresult. OK then
Pdoc. Print ()
End if
End sub

Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
If trim (word. Text) <> "" then
Try
Pdoc. documentname = "RichTextBox printing"
Pdoc. defaultpagesettings. margins. Top = 34
Pdoc. defaultpagesettings. margins. Bottom = 70
Pdoc. defaultpagesettings. margins. Left = 0
Pdoc. defaultpagesettings. margins. Right = 18
Printing ()
Catch ex as exception
End try
End if
End sub

Preview effect:

Subject: 123456

Sdfsdfdsfsd print RichTextBox
Print RichTextBox print RichTextBox

Print RichTextBox
Fdsfdsfsdfdsf

Power by: landlordaspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.