PrintDocument

Hi there.

I am working on a VB.Net application (WinForms) and all is working fine. I'm just putting the finishing touches to it, and am now working on a Print function.

The application is a code editor, and of course, one of the facilities is to be able to print the code that one has written. I have consulted the MSDN documentation, and, with it's help, I have managed to write a well behaved routine that does indeed print to the printer, using the PrintDocument_PrintPage event.

However, there is one problem - some lines that are longer than the page width are 'clipped' and thus there is test missing from the printout.

I need someway to know if a line of text is too long, and if so, at which point do I split the string?

I have seen a reference to MeasureString on this page: http://www.startvbdotnet.com/controls/printdialog1.aspx

However, the code as presented on the above site is broken (there is part of a line missing) and I can't reconstruct his code.

Can anyone suggest a method providing a printout without text being clipped? The text should ust wrap to the next line.

Many thanks

Mark Wills UK

[1135 byte] By [MarkWills] at [2007-12-24]
# 1
The code on that webpage looks okay and should do what you want to do. It is however badly formatted. Fix it like this:

- Three lines end with a trailing underscore, put a space before the underscore
- The code after the 'advancing the current char comment is mangled. Fix it like this:

'advancing the current char to the last char printed on this page
If intCurrentChar < RichTextBox1.Text.Length Then
e.HasMorePages = True
'HasMorePages tells the printing module whether another PrintPage event should be fired
Else
e.HasMorePages = False
intCurrentChar = 0
End If

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

To get printing to wordwrap you need to pass an fRectange and string.format

to the e.graphics.drawstring.

Private Sub PrintDocument1_PrintPage1(ByVal sender As Object, _

ByVal e As System.Drawing.Printing.PrintPageEventArgs) _

Handles PrintDocument1.PrintPage

Dim charactersOnPage As Integer = 0

Dim linesPerPage As Integer = 0

' Sets the value of charactersOnPage to the number of characters

' of stringToPrint that will fit within the bounds of the page.

e.Graphics.MeasureString(stringToPrint, myfont, e.MarginBounds.Size, _

StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

' Draws the string within the bounds of the page

e.Graphics.DrawString(stringToPrint, myfont, Brushes.Black, _

e.MarginBounds, StringFormat.GenericTypographic)

' Remove the portion of the string that has been printed.

stringToPrint = stringToPrint.Substring(charactersOnPage)

' Check to see if more pages are to be printed.

e.HasMorePages = stringToPrint.Length > 0

End Sub

'See also:

ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/cpref8/html/M_System_Drawing_Graphics_DrawString_1_8c429b2b.htm

http://www.msusenet.com/f-dotnet-languages-csharp-13/t-word-wrap-while-printing-1801348.html

TallDude at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...